Bubble Sort





Definition: A process designed to sort an unordered set of linear data into either ascending order (lowest to highest) or descending order (highest to lowest). This is done by repeatedly comparing neighboring elements and swapping them if they are in the order opposite of how they're being sorted. For example, if a 2 and 1 are being compared and the list is being sorted in ascending order then they'd be switched to 1 and 2. See an example of the procedure below.


Procedure for ASCENDING Bubble Sort:

  1. Check if the first two elements are in ascending order. If they are not, swap them so that they are.
  2. Move ahead one space each and repeat step 1. Here, the two elements are not swapped because they are already in ascending order.
  3. Repeat the same process of comparing and swapping 3 more times.
  4. Starting from the beginning of the current list, repeat steps 1-3 until no more swaps are needed. This likely means going over the whole list from start to end multiple times. The list is guaranteed to be completely sorted after a maximum of 5 total end to end rounds of this process (1 less than the number of elements in the list). This calculation is true for all lists being processed by bubble sort.

  5. ...

Fun Fact: Bubble Sort is extremely slow and rarely used in real applications, however the algorithm is popular in introductory computer science classes because it teaches the general theory behind sorting algorithms very well.



Commonly Used In: