Bubble Sort generally uses two nested loops.  Values are sorted by comparing neighboring elements in a list and swapping their values if they are not in order.  Generally, Bubble Sort will start from the first element, compare to the second, then compare the second to the third, then the third to the fourth and so forth.

 

Here are some general steps to follow:

 

  1. Pass through the entire list, swapping values that are not in the correct order.  This has the effect of moving the largest number to itŐs final position in the sorted list.

 

  1. Go through the list again, this time bring up the second largest number to the correct position.

 

  1. Continue until no more swapping is necessary, in other words, all numbers are in correctly sorted order, from least to greatest.

 

HINT:  The inner loop should decrease list size that is being checked by 1 after each run.

 

Return to Computer Programming Page