Bubble Sort Visualization

This is an example of a terminal-based bubble sort visualization. Notice that each pair being considered for a swap is visually "highlighted" with vertical bars, and there's an accompanying textual description. The text explains why the swap will or will not happen. Each pass of the list is denoted, until we get to a pass that does no swaps and the algorithm finishes.

The original list was created by randomly generating 5 integers in the range from 1 to 9, inclusive.

$ python visualize-sort.py
Original list: [5, 6, 5, 9, 1]

-- Pass #1 --

5 <= 6 -- no swap
|5||6| 5  9  1 
 5  6  5  9  1 

6 > 5 -- swap
 5 |6||5| 9  1 
 5  5  6  9  1 

6 <= 9 -- no swap
 5  5 |6||9| 1 
 5  5  6  9  1 

9 > 1 -- swap
 5  5  6 |9||1|
 5  5  6  1  9 

-- Pass #2 --

5 <= 5 -- no swap
|5||5| 6  1  9 
 5  5  6  1  9 

5 <= 6 -- no swap
 5 |5||6| 1  9 
 5  5  6  1  9 

6 > 1 -- swap
 5  5 |6||1| 9 
 5  5  1  6  9 

6 <= 9 -- no swap
 5  5  1 |6||9|
 5  5  1  6  9 

-- Pass #3 --

5 <= 5 -- no swap
|5||5| 1  6  9 
 5  5  1  6  9 

5 > 1 -- swap
 5 |5||1| 6  9 
 5  1  5  6  9 

5 <= 6 -- no swap
 5  1 |5||6| 9 
 5  1  5  6  9 

6 <= 9 -- no swap
 5  1  5 |6||9|
 5  1  5  6  9 

-- Pass #4 --

5 > 1 -- swap
|5||1| 5  6  9 
 1  5  5  6  9 

5 <= 5 -- no swap
 1 |5||5| 6  9 
 1  5  5  6  9 

5 <= 6 -- no swap
 1  5 |5||6| 9 
 1  5  5  6  9 

6 <= 9 -- no swap
 1  5  5 |6||9|
 1  5  5  6  9 

-- Pass #5 --

1 <= 5 -- no swap
|1||5| 5  6  9 
 1  5  5  6  9 

5 <= 5 -- no swap
 1 |5||5| 6  9 
 1  5  5  6  9 

5 <= 6 -- no swap
 1  5 |5||6| 9 
 1  5  5  6  9 

6 <= 9 -- no swap
 1  5  5 |6||9|
 1  5  5  6  9 

No swaps on pass 5, the list is sorted:
[1, 5, 5, 6, 9]
Return to Lab 09