A place for my personal musings, about programming, design or whatever come across my mind

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
4 Comments
2 Pings/Trackbacks
Tweets that mention kahfei » bubble sort -- Topsy.com
Tweets that mention kahfei » bubble sort -- Topsy.com

[…] This post was mentioned on Twitter by Hernan Garcia, Amir Barylko. Amir Barylko said: RT @theprogrammer bubble sort algorithm with explanatory videos http://is.gd/dLjSQ […]

Bubble sort in Ruby « Trapped In Magazines
Bubble sort in Ruby « Trapped In Magazines

[…] [1] kahfei’s blog with sorting videos http://www.kahfei.com/?p=117 […]

Hauskaz
Hauskaz

Oh cool, that’s my video.

kahfei
kahfei

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

<