Categories
ruby

bubble sort

Understanding sorting algorithm by just reading text or still illustration takes a lot of mind gymnastic. Apparently, there are lot of videos online, trying to explain the concept. But not all explain it as good though, some with really boring animation, some with crappy background music, and some even both . Here are two that I find did a great job in explaining bubble sort,

This one from codegearguru. Instead of creating graphic, or cutting out paper to represent data in the sorting process, just use the poker cards! That seems obvious now, but I didn’t think about it when I try to illustrate sorting to myself initially. Ha. Think there is a series of video created for different type of sorting algorithm as well, so definitely a great tool to understand sorting algorithm. Bonus near the end of the video, another technique called shaker sort or cocktail sort is also explained, which is basically a bidirectional bubble sort which should speed up the sorting speed.

Another one from Miles Hauskaz, just plain simple and succinct explanation and a video nicely done as well.

Sorting Algorithms – Bubble Sort from Miles Hauskaz on Vimeo.

So, this is my implementation of bubble sort, admittedly not the best that you can find, but that will do for me now, until I have time to come back and do some refactoring.

arr = %w{g a c b j e e}
i = arr.length - 1
count = 0
n = arr.length - 1
def swap(m,n) 
  arr = []
  if m > n
    arr << n
	arr << m
  else
    arr << m
	arr << n   end end while n > 0
  while count < i
    puts "comparing #{arr[count]} to #{arr[count+1]}"
    a = swap(arr[count],arr[count+1])
    arr[count..count+1] = a
    count += 1
  end
  n -= 1
  i -= 1
  count = 0
end
p arr

By kahfei

A system admin by day while secretly trying to transform myself to a coding designer or a designing coder at night.

4 replies on “bubble sort”

yupe, and it is a great video explaining bubble sort, thanks for that

Leave a Reply

Your email address will not be published. Required fields are marked *