Science Fair Project Encyclopedia
Double bubble sort
Double bubble sort is a sorting algorithm similar to insertion sort. It works relatively well on data that is already almost sorted, and can outperform quicksort in such conditions. If the data is already in order, only a single pass will be made through the list.
The essential difference from insertion sort is that the double bubble sort swaps adjacent pairs, while the insertion sort takes each value from the array and then puts it in place only once all elements ranking above it have been moved forward, this does decrease performance. However by using the xor swap algorithm, double bubble sort uses one fewer variable than insertion sort.
void doublebubbleSort(int *array, int length)
{
int i, j;
for(i = 0; i < length-1; i++)
{
if(array[i] > array[i+1]) /* compare neighboring elements */
{
for(j = i; j>=0 && (array[j] > array[j+1]); j--)
{
array[j] ^= array[j+1]; // Use Xor swap algorithm to increase efficiency
array[j+1] ^= array[j];
array[j] ^= array[j+1];
}
}
}
}
Double bubble sort is also used as a synonym for cocktail sort.
09-23-2007 01:00:40
The contents of this article is licensed from www.wikipedia.org under the GNU Free Documentation License. Click here to see the transparent copy and copyright details
The contents of this article is licensed from www.wikipedia.org under the GNU Free Documentation License. Click here to see the transparent copy and copyright details


