Bubble Sort
First of all, and this is very important, bubble sort is the best sort. Not in terms of
performance or any of that. I mean it's pretty ok at all of that, but no, where it
really shines is in looking pretty.
Anyway, with that out of the way, how does bubble sort work? Well very basically we're
going to make a bunch of passes through our array, and each time were going to let the
biggest numbers "bubble" to the top. In practice what this really mean is we go through
the array and look at each pair of numbers. If the number on the left(towards the start)
is bigger than the number on the right(towards the end) then we swap them. Simply repeat
this until the whole array is sorted.
Pseudo code
Giving you the actual code would take all the fun out of the struggle, but here's some
pseudo code to get you started. If you're lame you can also just google it, but don't do
that, because again, lame.
for(0 < end of array){That's pretty much it. Bubble sort has a relatively simple implementation, though please note that there is room for improvement in the above code. Take some time and think about how many times each for loop actually needs to be run (hint, they don't necesarily need to run through the whole array every time.)
if(NotSorted){
for(0 < end of array){
if(array[leftElement] > array[rightElement]){
swap(leftElement,rightElement)
}
}
}else{
break
}
}