Selection Sort - Swarthmore Collegealinen/cs21/f18/w10/... · Selection Sort 0 1 2 3 4 5 start 0...
Transcript of Selection Sort - Swarthmore Collegealinen/cs21/f18/w10/... · Selection Sort 0 1 2 3 4 5 start 0...
![Page 1: Selection Sort - Swarthmore Collegealinen/cs21/f18/w10/... · Selection Sort 0 1 2 3 4 5 start 0 Find minimum element idx between start to end 10 4 3 0 11 8 What next? minValIdx 3](https://reader034.fdocuments.net/reader034/viewer/2022050214/5f60800b7120be625045b85d/html5/thumbnails/1.jpg)
Selection Sort
![Page 2: Selection Sort - Swarthmore Collegealinen/cs21/f18/w10/... · Selection Sort 0 1 2 3 4 5 start 0 Find minimum element idx between start to end 10 4 3 0 11 8 What next? minValIdx 3](https://reader034.fdocuments.net/reader034/viewer/2022050214/5f60800b7120be625045b85d/html5/thumbnails/2.jpg)
Selection Sort
10 4 3 0 11 8
0 1 2 3 4 5
What do we do first?
![Page 3: Selection Sort - Swarthmore Collegealinen/cs21/f18/w10/... · Selection Sort 0 1 2 3 4 5 start 0 Find minimum element idx between start to end 10 4 3 0 11 8 What next? minValIdx 3](https://reader034.fdocuments.net/reader034/viewer/2022050214/5f60800b7120be625045b85d/html5/thumbnails/3.jpg)
Selection Sort0 1 2 3 4 5
start0
Find minimum element idx between start to end
10 4 3 0 11 8
What next?
minValIdx3
![Page 4: Selection Sort - Swarthmore Collegealinen/cs21/f18/w10/... · Selection Sort 0 1 2 3 4 5 start 0 Find minimum element idx between start to end 10 4 3 0 11 8 What next? minValIdx 3](https://reader034.fdocuments.net/reader034/viewer/2022050214/5f60800b7120be625045b85d/html5/thumbnails/4.jpg)
Selection Sort0 1 2 3 4 5
start0
Swap the elements at start and minValIdx
0 4 3 10 11 8
What next?
minValIdx3
![Page 5: Selection Sort - Swarthmore Collegealinen/cs21/f18/w10/... · Selection Sort 0 1 2 3 4 5 start 0 Find minimum element idx between start to end 10 4 3 0 11 8 What next? minValIdx 3](https://reader034.fdocuments.net/reader034/viewer/2022050214/5f60800b7120be625045b85d/html5/thumbnails/5.jpg)
Selection Sort0 1 2 3 4 5
start1
Decrease the interval.
0 4 3 10 11 8
What next?
![Page 6: Selection Sort - Swarthmore Collegealinen/cs21/f18/w10/... · Selection Sort 0 1 2 3 4 5 start 0 Find minimum element idx between start to end 10 4 3 0 11 8 What next? minValIdx 3](https://reader034.fdocuments.net/reader034/viewer/2022050214/5f60800b7120be625045b85d/html5/thumbnails/6.jpg)
Selection Sort0 1 2 3 4 5
start1
Find minimum element between start to end
0 4 3 10 11 8
What next?
minValIdx2
![Page 7: Selection Sort - Swarthmore Collegealinen/cs21/f18/w10/... · Selection Sort 0 1 2 3 4 5 start 0 Find minimum element idx between start to end 10 4 3 0 11 8 What next? minValIdx 3](https://reader034.fdocuments.net/reader034/viewer/2022050214/5f60800b7120be625045b85d/html5/thumbnails/7.jpg)
Selection Sort0 1 2 3 4 5
start1
Swap the elements at start and minValIdx
0 3 4 10 11 8
What next?
minValIdx2
![Page 8: Selection Sort - Swarthmore Collegealinen/cs21/f18/w10/... · Selection Sort 0 1 2 3 4 5 start 0 Find minimum element idx between start to end 10 4 3 0 11 8 What next? minValIdx 3](https://reader034.fdocuments.net/reader034/viewer/2022050214/5f60800b7120be625045b85d/html5/thumbnails/8.jpg)
Selection Sort0 1 2 3 4 5
start2
Decrease the interval.
0 3 4 10 11 8
What next?
![Page 9: Selection Sort - Swarthmore Collegealinen/cs21/f18/w10/... · Selection Sort 0 1 2 3 4 5 start 0 Find minimum element idx between start to end 10 4 3 0 11 8 What next? minValIdx 3](https://reader034.fdocuments.net/reader034/viewer/2022050214/5f60800b7120be625045b85d/html5/thumbnails/9.jpg)
Selection Sort0 1 2 3 4 5
start2
Find minimum element idx between start to end
0 3 4 10 11 8
What next?
minValIdx2
![Page 10: Selection Sort - Swarthmore Collegealinen/cs21/f18/w10/... · Selection Sort 0 1 2 3 4 5 start 0 Find minimum element idx between start to end 10 4 3 0 11 8 What next? minValIdx 3](https://reader034.fdocuments.net/reader034/viewer/2022050214/5f60800b7120be625045b85d/html5/thumbnails/10.jpg)
Selection Sort0 1 2 3 4 5
start2
Swap the elements at start and minValIdx
0 3 4 10 11 8
What next?
minValIdx2
![Page 11: Selection Sort - Swarthmore Collegealinen/cs21/f18/w10/... · Selection Sort 0 1 2 3 4 5 start 0 Find minimum element idx between start to end 10 4 3 0 11 8 What next? minValIdx 3](https://reader034.fdocuments.net/reader034/viewer/2022050214/5f60800b7120be625045b85d/html5/thumbnails/11.jpg)
Selection Sort0 1 2 3 4 5
start3
Decrease the interval.
0 3 4 10 11 8
What next?
![Page 12: Selection Sort - Swarthmore Collegealinen/cs21/f18/w10/... · Selection Sort 0 1 2 3 4 5 start 0 Find minimum element idx between start to end 10 4 3 0 11 8 What next? minValIdx 3](https://reader034.fdocuments.net/reader034/viewer/2022050214/5f60800b7120be625045b85d/html5/thumbnails/12.jpg)
Selection Sort0 1 2 3 4 5
start3
Find minimum element idx between start to end
0 3 4 10 11 8
What next?
minValIdx5
![Page 13: Selection Sort - Swarthmore Collegealinen/cs21/f18/w10/... · Selection Sort 0 1 2 3 4 5 start 0 Find minimum element idx between start to end 10 4 3 0 11 8 What next? minValIdx 3](https://reader034.fdocuments.net/reader034/viewer/2022050214/5f60800b7120be625045b85d/html5/thumbnails/13.jpg)
Selection Sort0 1 2 3 4 5
Swap the elements at start and minValIdx
0 3 4 8 11 10
What next?
start3
minValIdx5
![Page 14: Selection Sort - Swarthmore Collegealinen/cs21/f18/w10/... · Selection Sort 0 1 2 3 4 5 start 0 Find minimum element idx between start to end 10 4 3 0 11 8 What next? minValIdx 3](https://reader034.fdocuments.net/reader034/viewer/2022050214/5f60800b7120be625045b85d/html5/thumbnails/14.jpg)
Selection Sort0 1 2 3 4 5
start4
Decrease the interval.
0 3 4 8 11 10
What next?
![Page 15: Selection Sort - Swarthmore Collegealinen/cs21/f18/w10/... · Selection Sort 0 1 2 3 4 5 start 0 Find minimum element idx between start to end 10 4 3 0 11 8 What next? minValIdx 3](https://reader034.fdocuments.net/reader034/viewer/2022050214/5f60800b7120be625045b85d/html5/thumbnails/15.jpg)
Selection Sort0 1 2 3 4 5
start4
Find minimum element idx between start to end
0 3 4 8 11 10
What next?
minValIdx5
![Page 16: Selection Sort - Swarthmore Collegealinen/cs21/f18/w10/... · Selection Sort 0 1 2 3 4 5 start 0 Find minimum element idx between start to end 10 4 3 0 11 8 What next? minValIdx 3](https://reader034.fdocuments.net/reader034/viewer/2022050214/5f60800b7120be625045b85d/html5/thumbnails/16.jpg)
Selection Sort0 1 2 3 4 5
Swap the elements at start and minValIdx
0 3 4 8 10 11
What next?
start4
minValIdx5
![Page 17: Selection Sort - Swarthmore Collegealinen/cs21/f18/w10/... · Selection Sort 0 1 2 3 4 5 start 0 Find minimum element idx between start to end 10 4 3 0 11 8 What next? minValIdx 3](https://reader034.fdocuments.net/reader034/viewer/2022050214/5f60800b7120be625045b85d/html5/thumbnails/17.jpg)
Selection Sort0 1 2 3 4 5
start5
Decrease the interval.
0 3 4 8 10 11
We’re done!
![Page 18: Selection Sort - Swarthmore Collegealinen/cs21/f18/w10/... · Selection Sort 0 1 2 3 4 5 start 0 Find minimum element idx between start to end 10 4 3 0 11 8 What next? minValIdx 3](https://reader034.fdocuments.net/reader034/viewer/2022050214/5f60800b7120be625045b85d/html5/thumbnails/18.jpg)
Selection sortselectionSort(L):
for startIdx in range(len(L)):
minIdx = findMinimum(startIdx, L)
swap(startIdx, minIdx, L)
![Page 19: Selection Sort - Swarthmore Collegealinen/cs21/f18/w10/... · Selection Sort 0 1 2 3 4 5 start 0 Find minimum element idx between start to end 10 4 3 0 11 8 What next? minValIdx 3](https://reader034.fdocuments.net/reader034/viewer/2022050214/5f60800b7120be625045b85d/html5/thumbnails/19.jpg)
Selection sortfindMinimum(startIdx, L):
minIdx = startIdx
for i in range(startIdx, len(L)):
if L[i] < L[minIdx]:
minIdx = i
return minIdx
![Page 20: Selection Sort - Swarthmore Collegealinen/cs21/f18/w10/... · Selection Sort 0 1 2 3 4 5 start 0 Find minimum element idx between start to end 10 4 3 0 11 8 What next? minValIdx 3](https://reader034.fdocuments.net/reader034/viewer/2022050214/5f60800b7120be625045b85d/html5/thumbnails/20.jpg)
Selection sortswap(i, j, L):
temp = L[i] # step 1
L[i] = L[j] # step 2
L[j] = temp # step 3
Temp i j
Temp i j
Temp i j
Temp i j
![Page 21: Selection Sort - Swarthmore Collegealinen/cs21/f18/w10/... · Selection Sort 0 1 2 3 4 5 start 0 Find minimum element idx between start to end 10 4 3 0 11 8 What next? minValIdx 3](https://reader034.fdocuments.net/reader034/viewer/2022050214/5f60800b7120be625045b85d/html5/thumbnails/21.jpg)
![Page 22: Selection Sort - Swarthmore Collegealinen/cs21/f18/w10/... · Selection Sort 0 1 2 3 4 5 start 0 Find minimum element idx between start to end 10 4 3 0 11 8 What next? minValIdx 3](https://reader034.fdocuments.net/reader034/viewer/2022050214/5f60800b7120be625045b85d/html5/thumbnails/22.jpg)
![Page 23: Selection Sort - Swarthmore Collegealinen/cs21/f18/w10/... · Selection Sort 0 1 2 3 4 5 start 0 Find minimum element idx between start to end 10 4 3 0 11 8 What next? minValIdx 3](https://reader034.fdocuments.net/reader034/viewer/2022050214/5f60800b7120be625045b85d/html5/thumbnails/23.jpg)