1 Sorting Animation Chapter 10
2 Selection Sort Another way of sorting is the selection sort The main idea is to keep finding the smallest (and next smallest) items in the array And move them into correct position (swap)
3 Selection Sort 45 < smallest? F 45 smallest 0 small_pos 0 k data n
4 Selection Sort 32 < smallest? T 45 smallest 0 small_pos 0 k data n
5 Selection Sort 32 < smallest? T 32 smallest 1 small_pos 0 k data n
6 Selection Sort 56 < smallest? F 32 smallest 1 small_pos 0 k data n
7 Selection Sort 9 < smallest? T 32 smallest 1 small_pos 0 k data n
8 Selection Sort 9 < smallest? T 9 smallest 3 small_pos 0 k data n
9 Selection Sort 21 < smallest? F 9 smallest 3 small_pos 0 k data n
10 Selection Sort 77 < smallest? F 9 smallest 3 small_pos 0 k data n
11 Selection Sort 17 < smallest? F 9 smallest 3 small_pos 0 k data n
12 Selection Sort—SWAP! 9 smallest 3 small_pos 0 k data n Swap(data[k], data[small_pos]);
13 Selection Sort 32 < smallest? F 32 smallest 1 small_pos 1 k data n
14 Selection Sort 32 < smallest? F 32 smallest 1 small_pos 1 k data n
15 Selection Sort 45 < smallest? F 32 smallest 1 small_pos 1 k data n
16 Selection Sort 21 < smallest? T 32 smallest 1 small_pos 1 k data n
Selection Sort 21 < smallest? T 21 smallest 4 small_pos 1 k data n
18 Selection Sort 77 < smallest? F 21 smallest 4 small_pos 1 k data n
19 Selection Sort 18 < smallest? T 21 smallest 4 small_pos 1 k data n
20 Selection Sort 18 < smallest? T 18 smallest 6 small_pos 1 k data n
21 Selection Sort 17 < smallest? T 18 smallest 6 small_pos 1 k data n
22 Selection Sort 17 < smallest? T 17 smallest 7 small_pos 1 k data n
23 Selection Sort—SWAP! 17 smallest 7 small_pos 1 k data n Swap(data[k], data[small_pos]);
24 Selection Sort—and so on 56 < smallest? F 56 smallest 2 small_pos 2 k data n