This is an example of a terminal-based selection sort visualization. Here we've used dashses and asterisks to create a horizontal bar graph that depicts the relative sizes of the values in the list. Selection sort "selects" the value that should appear at each index in the final, sorted list. We describe each of these selections by listing the values being considered, and describing the swap that needs to occur. Notice that we use both text (e.g. "The minimum value in ... is ...") and "visuals" (the bar graphs). These visuals show the values about to be swapped with asterisks instead of dashes. The user can see how, with each selection, the lines of the graph get closer to being ordered from shortest to longest.
The original list was created by randomly generating 5 integers in the range from 1 to 20, inclusive.
$ python visualize-sort.py
Original list: [4, 10, 8, 2, 2]
The minimum value in [4, 10, 8, 2, 2] is 2
Swap 2 and 4
0:**** (4)
1:---------- (10)
2:-------- (8)
3:** (2)
4:-- (2)
0:-- (2)
1:---------- (10)
2:-------- (8)
3:---- (4)
4:-- (2)
The minimum value in [10, 8, 4, 2] is 2
Swap 2 and 10
0:-- (2)
1:********** (10)
2:-------- (8)
3:---- (4)
4:** (2)
0:-- (2)
1:-- (2)
2:-------- (8)
3:---- (4)
4:---------- (10)
The minimum value in [8, 4, 10] is 4
Swap 4 and 8
0:-- (2)
1:-- (2)
2:******** (8)
3:**** (4)
4:---------- (10)
0:-- (2)
1:-- (2)
2:---- (4)
3:-------- (8)
4:---------- (10)
The minimum value in [8, 10] is 8
No swap needed
0:-- (2)
1:-- (2)
2:---- (4)
3:-------- (8)
4:---------- (10)
The minimum value in [10] is 10
No swap needed
0:-- (2)
1:-- (2)
2:---- (4)
3:-------- (8)
4:---------- (10)
Sorted list: [2, 2, 4, 8, 10]
Return to Lab 09