Penguin
Note: You are viewing an old revision of this page. View the current version.

BubbleSort is the cannonical example of an inefficient SortingAlgorithm?.

It works by moving through the array comparing each element with the next element and swapping them if they're in the wrong order. The processes is repeated until all the elements are in order.

The source code looks a little like

int [] array; boolean sorted = false; while(!sorted) {

sorted = true; for (int i;i < array.length-1;i++)

if (array[i?>array[i+1?) {

sorted = false; int tmp = array[i?; array[i? = array[i+1?; array[i+1? = tmp;

}

}

BubbleSort does have some good features:

  1. If you know that ``a'' unsorted elements have been prefixed to an otherwise sorted list then the list and be sorted in time O(na).
  2. BubbleSort can use fine-grained Synchronisation to sort a list while other threads or processes are updating it or changing it