Understanding Selection Sort Algorithm
Learn about the selection sort method for sorting arrays by finding the smallest items and placing them in the correct order through swapping.
Understanding Selection Sort Algorithm
E N D
Presentation Transcript
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)
Selection Sort data small_pos smallest n 0 45 0 k 45 < smallest? F 0
Selection Sort data small_pos smallest n 0 45 1 k 32 < smallest? T 0
Selection Sort data small_pos smallest n 1 32 1 k 32 < smallest? T 0
Selection Sort data small_pos smallest n 1 32 2 k 56 < smallest? F 0
Selection Sort data small_pos smallest n 1 32 3 k 9 < smallest? T 0
Selection Sort data small_pos smallest n 3 9 3 k 9 < smallest? T 0
Selection Sort data small_pos smallest n 3 9 4 k 21 < smallest? F 0
Selection Sort data small_pos smallest n 3 9 5 k 77 < smallest? F 0
Selection Sort data small_pos smallest n 3 9 6 k 17 < smallest? F 0
Selection Sort—SWAP! data small_pos smallest n 3 9 6 k Swap(data[k], data[small_pos]); 0
Selection Sort data small_pos smallest n 1 32 1 k 32 < smallest? F 1
Selection Sort data small_pos smallest n 1 32 2 k 32 < smallest? F 1
Selection Sort data small_pos smallest n 1 32 3 k 45 < smallest? F 1
Selection Sort data small_pos smallest n 1 32 4 k 21 < smallest? T 1
Selection Sort data small_pos smallest n 4 21 4 k 21 < smallest? T 1
Selection Sort data small_pos smallest n 4 21 5 k 77 < smallest? F 1
Selection Sort data small_pos smallest n 4 21 6 k 18 < smallest? T 1
Selection Sort data small_pos smallest n 6 18 6 k 18 < smallest? T 1
Selection Sort data small_pos smallest n 6 18 7 k 17 < smallest? T 1
Selection Sort data small_pos smallest n 7 17 7 k 17 < smallest? T 1
Selection Sort—SWAP! data small_pos smallest n 7 17 7 k Swap(data[k], data[small_pos]); 1
Selection Sort—and so on data small_pos smallest n 2 56 2 k 56 < smallest? F 2