Giter Club home page Giter Club logo

bubble_sort_ruby's Introduction

Bubble Sort Ruby

Run the tests in the spec folder, and write your code in bubble_sort.rb.


Keep in mind all the different ways you can explore your code:

From the Command Line:

ruby bubble_sort.rb # just make sure you're printing some output!

In the REPL:

irb
# or
pry
pry > require "bubble_sort.rb"
pry > bubble_sort([3,2,1])

Using Rspec Tests:

rspec
# or
rspec spec/bubble_sort_spec.rb
# or, run an individual test
rspec -e "handles zero"

Bubble Sort Refresher

Pre-work: First, some Hungarian ("Csángó") folk dance

Bubble sort is one of the first sorting algorithms you should try and master. It essentially forces larger elements to 'sink' to the bottom/back while inadvertently 'floating' smaller elements to the top/front of a list. This is done with numerous comparisons between one element in an array with its neighbor.

Sort the list using Bubble Sort: [5,4,2,3,1,6]

Iteration 1

Look at the first two elements in the list.

0: [5, 4,2,3,1,6]

Is 5 > 4 ? Yes! Swap!

If an element on the left (5) is greater than the element on the right (4), the two elements 'swap' locations

1: [4,5,2,3,1,6]

2: [4,2,5,3,1,6]

3: [4,2,3,5,1,6]

4: [4,2,3,1,5,6]

Important: We now know that the last element in the list is the largest element in the list. There's no need to do a comparison with that number ever again.

Iteration 2

0: [4,2,3,1,5,6]

1: [2,4,3,1,5,6]

2: [2,3,4,1,5,6]

3: [2,3,1,4,5,6]

Stop!

Remember: we know that last element is the largest number in the list. There is no need to compare against that number ever again. We also now know that the second to last number is the second largest number; no need to move that one ever again as well. (Detect a trend?)

Iteration 3

0: [2,3,1,4,5,6]

If an element on the left has met a larger or equal element, we look at its bigger neighbor and now compare the larger neighbor to it's neighbor on the right. The process is continued until our established end.

1: [2,3,1,4,5,6]

2: [2,1,3,4,5,6]

Stop!

We don't stop sorting until we hit the end. Even if we find

Iteration 4

0: [2,1,3,4,5,6]

1: [1,2,3,4,5,6]

Stop!

Iteration 5

0: [1,2,3,4,5,6]

Stop!

When there is only one element (the first element) left in our unsorted list, it is already sorted for us as a freebie!

List is now sorted using Bubble Sort: [1,2,3,4,5,6]

Now write your own bubble sort!

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.