Code Monkey home page Code Monkey logo

bubble-sort's Introduction

The Odin Project - Ruby Programming

Project: Bubble Sort

Introduction:

One of the simpler (but more processor-intensive) ways of sorting a group of items in an array is bubble sort, where each element is compared to the one next to it and they are swapped if the one on the left is larger than the one on the right. This continues until the array is eventually sorted.

Build a method #bubble_sort that takes an array and returns a sorted array. It must use the bubble sort methodology (using #sort would be pretty pointless, wouldn’t it?).

> bubble_sort([4,3,78,2,0,2])
=> [0,2,2,3,4,78]

Solution:

• The function is called with one argument, an array
with random numbers.
• Use the function `downto` to go from array size minus two to zero, this function iterates the given block, passing in decreasing values from int down to and including limit. #=> `int.downto(limit)`
• So, to go from (array.size - 2) to zero and I declare the parameter `i` in the callback, we need it to be like this so as not to exceed the size of the array in the tour, we will explain in more detail later.
• In the next line, use the function `upto` to go from zero to `i`, this function iterates the given block, passing in integer values from int up to and including limit, #=> `int.upto(limit)`
• Taking `i` as the limit of the` upto` function, we ensure the last ordered value.
• And I declare the parameter `j` in the callback, which we will use as the array index for our algorithm.
• At this point, we have consecutive elements, let's see:
  - We have an array => array = [5,4,3]
  - So array[j] is equal to array[0] = 5
  - And array[j+1] is equal to array[1] = 4
• Then asking if first value greater than the second value, if true the following will happen:
  - Save to first value in the varible `temp` #=> temp = array[j]
  - Replace the value of the first for the second #=> array[j] = array[j+1]
  - Then change the value of the second for the temp #=> array[j+1] = temp
  - Our array would be like this, in the first iteration inside `upto` function #=> [4,5,3]
• So our algorithm will advance in pairs of two ordering it until it reaches the position `i`, 
• When done it would be like this. #=> [4,3,5]
• I'm still inside the `downto` block so I repeat the process, but this time the last element is ignored because it is already ordered.
• And that is what is known as the bubble sort.

References:

bubble-sort's People

Contributors

paulotijero avatar

Watchers

James Cloos avatar  avatar

Forkers

bettysl20

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.