|Silverlight Sorting Lab|
|Written by Ian Elliot|
|Tuesday, 12 October 2010|
Page 5 of 6
The most obvious improvement that can be made to this simple bubble sort is to restrict the scan to just the un-sorted portion of the array. Given that you can see the way that at each scan the next largest element is “bubbled” into its correct position you can also see that after each scan the size of the ordered part of the array increases by exactly one.
Translating this into code gives -
The final bubble sort method is the bi-directional or Shaker sort.
If you watch the bubble sort in action you can see quite easily that one small value stuck at the bottom of the array can keep the scan going right to the bitter end just because it has to be bubbled to the top. An improvement might be to perform a reverse scan after each forward scan. This would sweep small values to the top as quickly as the standard bubble sort sweeps big values to the bottom. This is easy to implement and we might as well retain the “reducing” aspect of the sorting process and draw the lines to show the active portion of the array.
This looks like a complicated subroutine but you should be able to see that it is just the reducing bubble sort written twice in opposite directions!
The next sorting method that it is worth adding to the lab is the Shell sort.
This isn’t really one single sorting method but a technique that can be applied to improve many basic sorting methods. When applied to the bubble sort however it makes good sense. The problem with the basic bubble sort is that no matter how hard it tries it only ever moves and element by one place and so it can take many moves for an element to reach its true location. The shell sort increases the range of the moves. If you call a bubble sort a 1-sort because it compares and swaps elements that are 1 unit appear then a D-sort compares and swaps elements that are D units apart.
To shell sort a list you perform a series of sorts of decreasing distance until you finish with a 1-sort and produce a fully sorted list. Usually D is taken to be half the number of elements and it is divided by two at each subsequent sorts, however there is a lot of evidence that binary D values are not the best choice.
To see how Shell sorting speeds up bubble sort add a new frame and a single button to the form. The button’s event handler is simply:
private void button4_Click(object sender,
and in the code module the Shell routine is:
This is a really interesting sort to watch in action. It starts off looking really good and it produces a “very nearly ordered” array in no time at all. Then it crawls along doing a 1-sort and finishes usually slower than the simple bubble sort! If you look closely you might agree that the reason is that the time for the final 1-sort is set by the time needed to bubble the smallest element to the top of the array. An obvious improvement would be to make the shell sort bi-directional. As they say in all the best books – this is left as a reader exercise!
|Last Updated ( Tuesday, 12 October 2010 )|