IAintaBlonde.com

SORTING ALGORITHM: BUBBLE SORT

Bubble Sort

This is probably the simplest way sort an array of objects. Not to ignore the fact that it is also the slowest way!

The goal is to compare two neighboring objects.

Step1:Swap them if they are in the wrong order.

Given an array a of numbers, with length n, here’s a snippet of C code for bubble sort:

for (i=0; i< a[j]) {  /* compare the two neighbors */
tmp = a[j];         /* swap a[j] and a[j+1]      */
a[j] = a[j+1];
a[j+1] = tmp;
}
}

This entry was posted on Monday, April 9th, 2007 at 10:08 pm and is filed under I Love Programming!, Sorting Algorithms. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply »»

Close
E-mail It