Selection Sort - gorskicompsci.cagorskicompsci.ca/ICS4U/3_Algorithms/ppt_SelectionSort.pdf ·...
Transcript of Selection Sort - gorskicompsci.cagorskicompsci.ca/ICS4U/3_Algorithms/ppt_SelectionSort.pdf ·...
![Page 1: Selection Sort - gorskicompsci.cagorskicompsci.ca/ICS4U/3_Algorithms/ppt_SelectionSort.pdf · selection sort. Every October, Halloween candies are consumed from best to worst. Whether](https://reader034.fdocuments.net/reader034/viewer/2022050312/5f7405228b63bf4c08084abc/html5/thumbnails/1.jpg)
Selection
Sort
![Page 2: Selection Sort - gorskicompsci.cagorskicompsci.ca/ICS4U/3_Algorithms/ppt_SelectionSort.pdf · selection sort. Every October, Halloween candies are consumed from best to worst. Whether](https://reader034.fdocuments.net/reader034/viewer/2022050312/5f7405228b63bf4c08084abc/html5/thumbnails/2.jpg)
Selection Sort
• Repeatedly finds the largest
number and swaps it into place.
• O(n^2) in all cases. There is no
best case.
• It works for all kinds of data,
Strings, chars, doubles…
A card for you to write:
![Page 3: Selection Sort - gorskicompsci.cagorskicompsci.ca/ICS4U/3_Algorithms/ppt_SelectionSort.pdf · selection sort. Every October, Halloween candies are consumed from best to worst. Whether](https://reader034.fdocuments.net/reader034/viewer/2022050312/5f7405228b63bf4c08084abc/html5/thumbnails/3.jpg)
Selection Sort Tradeoff
(+) Selection Sort is simple to
understand and is simple to
code.
(-) It’s simplicity means it isn’t
very clever or efficient. That
means it is SLOW.
A card for you to write:
![Page 4: Selection Sort - gorskicompsci.cagorskicompsci.ca/ICS4U/3_Algorithms/ppt_SelectionSort.pdf · selection sort. Every October, Halloween candies are consumed from best to worst. Whether](https://reader034.fdocuments.net/reader034/viewer/2022050312/5f7405228b63bf4c08084abc/html5/thumbnails/4.jpg)
I posted a YouTube video Tracing Selection Sort:
https://www.youtube.com/watch?v=NiKCsHd2K70
![Page 5: Selection Sort - gorskicompsci.cagorskicompsci.ca/ICS4U/3_Algorithms/ppt_SelectionSort.pdf · selection sort. Every October, Halloween candies are consumed from best to worst. Whether](https://reader034.fdocuments.net/reader034/viewer/2022050312/5f7405228b63bf4c08084abc/html5/thumbnails/5.jpg)
“Children are perhaps the greatest advocates of selection sort. Every October, Halloween candies are consumed from best to worst. Whether daily sampling is limited or not, it is clear that choices of the next treat consumed are based on ‘the next biggest piece’ or ‘the next most favorite’ and so on. Children consume treats in decreasing order of acceptability.
Similarly, when we select plants from a greenhouse, check produce in the store or pick strawberries from the farm, we seek the best items first.”
Bailey, Duane. Java Structures. 1999. Pg 80.
A very important application of Selection Sort:
![Page 6: Selection Sort - gorskicompsci.cagorskicompsci.ca/ICS4U/3_Algorithms/ppt_SelectionSort.pdf · selection sort. Every October, Halloween candies are consumed from best to worst. Whether](https://reader034.fdocuments.net/reader034/viewer/2022050312/5f7405228b63bf4c08084abc/html5/thumbnails/6.jpg)
To translate it to English…
int a[] = {6, 4, 3, 7, 8, 1, 0, 2};
for (int left = a.length - 1 ; left > 0 ; left--)
{
int max = 0;
for (int i = 1 ; i < left ; i++)
{
if (a [max] < a [i])
max = i;
}
int temp = a [max];
a [max] = a [left - 1];
a [left - 1] = temp;
}
![Page 7: Selection Sort - gorskicompsci.cagorskicompsci.ca/ICS4U/3_Algorithms/ppt_SelectionSort.pdf · selection sort. Every October, Halloween candies are consumed from best to worst. Whether](https://reader034.fdocuments.net/reader034/viewer/2022050312/5f7405228b63bf4c08084abc/html5/thumbnails/7.jpg)
To translate it to English…
Declare the array
for (int left = a.length - 1 ; left > 0 ; left--)
{
int max = 0;
for (int i = 1 ; i < left ; i++)
{
if (a [max] < a [i])
max = i;
}
int temp = a [max];
a [max] = a [left - 1];
a [left - 1] = temp;
}
![Page 8: Selection Sort - gorskicompsci.cagorskicompsci.ca/ICS4U/3_Algorithms/ppt_SelectionSort.pdf · selection sort. Every October, Halloween candies are consumed from best to worst. Whether](https://reader034.fdocuments.net/reader034/viewer/2022050312/5f7405228b63bf4c08084abc/html5/thumbnails/8.jpg)
To translate it to English…
Declare the array
for (int left = a.length - 1 ; left > 0 ; left--)
{
Find the max
int temp = a [max];
a [max] = a [left - 1];
a [left - 1] = temp;
}
![Page 9: Selection Sort - gorskicompsci.cagorskicompsci.ca/ICS4U/3_Algorithms/ppt_SelectionSort.pdf · selection sort. Every October, Halloween candies are consumed from best to worst. Whether](https://reader034.fdocuments.net/reader034/viewer/2022050312/5f7405228b63bf4c08084abc/html5/thumbnails/9.jpg)
To translate it to English…
Declare the array
for (int left = a.length - 1 ; left > 0 ; left--)
{
Find the max
Swap the max into place
}
![Page 10: Selection Sort - gorskicompsci.cagorskicompsci.ca/ICS4U/3_Algorithms/ppt_SelectionSort.pdf · selection sort. Every October, Halloween candies are consumed from best to worst. Whether](https://reader034.fdocuments.net/reader034/viewer/2022050312/5f7405228b63bf4c08084abc/html5/thumbnails/10.jpg)
To translate it to English…
Declare the array
For the non-sorted part of the array
{
Find the max
Swap the max into place
}
![Page 11: Selection Sort - gorskicompsci.cagorskicompsci.ca/ICS4U/3_Algorithms/ppt_SelectionSort.pdf · selection sort. Every October, Halloween candies are consumed from best to worst. Whether](https://reader034.fdocuments.net/reader034/viewer/2022050312/5f7405228b63bf4c08084abc/html5/thumbnails/11.jpg)
Tracing
![Page 12: Selection Sort - gorskicompsci.cagorskicompsci.ca/ICS4U/3_Algorithms/ppt_SelectionSort.pdf · selection sort. Every October, Halloween candies are consumed from best to worst. Whether](https://reader034.fdocuments.net/reader034/viewer/2022050312/5f7405228b63bf4c08084abc/html5/thumbnails/12.jpg)
[0] [1] [2] [3] [4] [5] [6] [7]
6 4 3 7 8 1 0 2
![Page 13: Selection Sort - gorskicompsci.cagorskicompsci.ca/ICS4U/3_Algorithms/ppt_SelectionSort.pdf · selection sort. Every October, Halloween candies are consumed from best to worst. Whether](https://reader034.fdocuments.net/reader034/viewer/2022050312/5f7405228b63bf4c08084abc/html5/thumbnails/13.jpg)
[0] [1] [2] [3] [4] [5] [6] [7]
6 4 3 7 8 1 0 2
![Page 14: Selection Sort - gorskicompsci.cagorskicompsci.ca/ICS4U/3_Algorithms/ppt_SelectionSort.pdf · selection sort. Every October, Halloween candies are consumed from best to worst. Whether](https://reader034.fdocuments.net/reader034/viewer/2022050312/5f7405228b63bf4c08084abc/html5/thumbnails/14.jpg)
[0] [1] [2] [3] [4] [5] [6] [7]
6 4 3 7 8 1 0 2
![Page 15: Selection Sort - gorskicompsci.cagorskicompsci.ca/ICS4U/3_Algorithms/ppt_SelectionSort.pdf · selection sort. Every October, Halloween candies are consumed from best to worst. Whether](https://reader034.fdocuments.net/reader034/viewer/2022050312/5f7405228b63bf4c08084abc/html5/thumbnails/15.jpg)
[0] [1] [2] [3] [4] [5] [6] [7]
6 4 3 7 8 1 0 2
6 4 3 7 2 1 0 8
![Page 16: Selection Sort - gorskicompsci.cagorskicompsci.ca/ICS4U/3_Algorithms/ppt_SelectionSort.pdf · selection sort. Every October, Halloween candies are consumed from best to worst. Whether](https://reader034.fdocuments.net/reader034/viewer/2022050312/5f7405228b63bf4c08084abc/html5/thumbnails/16.jpg)
[0] [1] [2] [3] [4] [5] [6] [7]
6 4 3 7 8 1 0 2
6 4 3 7 2 1 0 8
![Page 17: Selection Sort - gorskicompsci.cagorskicompsci.ca/ICS4U/3_Algorithms/ppt_SelectionSort.pdf · selection sort. Every October, Halloween candies are consumed from best to worst. Whether](https://reader034.fdocuments.net/reader034/viewer/2022050312/5f7405228b63bf4c08084abc/html5/thumbnails/17.jpg)
[0] [1] [2] [3] [4] [5] [6] [7]
6 4 3 7 8 1 0 2
6 4 3 7 2 1 0 8
![Page 18: Selection Sort - gorskicompsci.cagorskicompsci.ca/ICS4U/3_Algorithms/ppt_SelectionSort.pdf · selection sort. Every October, Halloween candies are consumed from best to worst. Whether](https://reader034.fdocuments.net/reader034/viewer/2022050312/5f7405228b63bf4c08084abc/html5/thumbnails/18.jpg)
[0] [1] [2] [3] [4] [5] [6] [7]
6 4 3 7 8 1 0 2
6 4 3 7 2 1 0 8
![Page 19: Selection Sort - gorskicompsci.cagorskicompsci.ca/ICS4U/3_Algorithms/ppt_SelectionSort.pdf · selection sort. Every October, Halloween candies are consumed from best to worst. Whether](https://reader034.fdocuments.net/reader034/viewer/2022050312/5f7405228b63bf4c08084abc/html5/thumbnails/19.jpg)
[0] [1] [2] [3] [4] [5] [6] [7]
6 4 3 7 8 1 0 2
6 4 3 7 2 1 0 8
6 4 3 7 2 1 7 8
![Page 20: Selection Sort - gorskicompsci.cagorskicompsci.ca/ICS4U/3_Algorithms/ppt_SelectionSort.pdf · selection sort. Every October, Halloween candies are consumed from best to worst. Whether](https://reader034.fdocuments.net/reader034/viewer/2022050312/5f7405228b63bf4c08084abc/html5/thumbnails/20.jpg)
[0] [1] [2] [3] [4] [5] [6] [7]
6 4 3 7 8 1 0 2
6 4 3 7 2 1 0 8
6 4 3 0 2 1 7 8
![Page 21: Selection Sort - gorskicompsci.cagorskicompsci.ca/ICS4U/3_Algorithms/ppt_SelectionSort.pdf · selection sort. Every October, Halloween candies are consumed from best to worst. Whether](https://reader034.fdocuments.net/reader034/viewer/2022050312/5f7405228b63bf4c08084abc/html5/thumbnails/21.jpg)
[0] [1] [2] [3] [4] [5] [6] [7]
6 4 3 7 8 1 0 2
6 4 3 7 2 1 0 8
6 4 3 0 2 1 7 8
![Page 22: Selection Sort - gorskicompsci.cagorskicompsci.ca/ICS4U/3_Algorithms/ppt_SelectionSort.pdf · selection sort. Every October, Halloween candies are consumed from best to worst. Whether](https://reader034.fdocuments.net/reader034/viewer/2022050312/5f7405228b63bf4c08084abc/html5/thumbnails/22.jpg)
[0] [1] [2] [3] [4] [5] [6] [7]
6 4 3 7 8 1 0 2
6 4 3 7 2 1 0 8
6 4 3 0 2 1 7 8
![Page 23: Selection Sort - gorskicompsci.cagorskicompsci.ca/ICS4U/3_Algorithms/ppt_SelectionSort.pdf · selection sort. Every October, Halloween candies are consumed from best to worst. Whether](https://reader034.fdocuments.net/reader034/viewer/2022050312/5f7405228b63bf4c08084abc/html5/thumbnails/23.jpg)
[0] [1] [2] [3] [4] [5] [6] [7]
6 4 3 7 8 1 0 2
6 4 3 7 2 1 0 8
6 4 3 0 2 1 7 8
1 4 3 0 2 6 7 8
![Page 24: Selection Sort - gorskicompsci.cagorskicompsci.ca/ICS4U/3_Algorithms/ppt_SelectionSort.pdf · selection sort. Every October, Halloween candies are consumed from best to worst. Whether](https://reader034.fdocuments.net/reader034/viewer/2022050312/5f7405228b63bf4c08084abc/html5/thumbnails/24.jpg)
[0] [1] [2] [3] [4] [5] [6] [7]
6 4 3 7 8 1 0 2
6 4 3 7 2 1 0 8
6 4 3 0 2 1 7 8
1 4 3 0 2 6 7 8
![Page 25: Selection Sort - gorskicompsci.cagorskicompsci.ca/ICS4U/3_Algorithms/ppt_SelectionSort.pdf · selection sort. Every October, Halloween candies are consumed from best to worst. Whether](https://reader034.fdocuments.net/reader034/viewer/2022050312/5f7405228b63bf4c08084abc/html5/thumbnails/25.jpg)
[0] [1] [2] [3] [4] [5] [6] [7]
6 4 3 7 8 1 0 2
6 4 3 7 2 1 0 8
6 4 3 0 2 1 7 8
1 4 3 0 2 6 7 8
![Page 26: Selection Sort - gorskicompsci.cagorskicompsci.ca/ICS4U/3_Algorithms/ppt_SelectionSort.pdf · selection sort. Every October, Halloween candies are consumed from best to worst. Whether](https://reader034.fdocuments.net/reader034/viewer/2022050312/5f7405228b63bf4c08084abc/html5/thumbnails/26.jpg)
[0] [1] [2] [3] [4] [5] [6] [7]
6 4 3 7 8 1 0 2
6 4 3 7 2 1 0 8
6 4 3 0 2 1 7 8
1 4 3 0 2 6 7 8
![Page 27: Selection Sort - gorskicompsci.cagorskicompsci.ca/ICS4U/3_Algorithms/ppt_SelectionSort.pdf · selection sort. Every October, Halloween candies are consumed from best to worst. Whether](https://reader034.fdocuments.net/reader034/viewer/2022050312/5f7405228b63bf4c08084abc/html5/thumbnails/27.jpg)
[0] [1] [2] [3] [4] [5] [6] [7]
6 4 3 7 8 1 0 2
6 4 3 7 2 1 0 8
6 4 3 0 2 1 7 8
1 4 3 0 2 6 7 8
1 2 3 0 4 6 7 8
![Page 28: Selection Sort - gorskicompsci.cagorskicompsci.ca/ICS4U/3_Algorithms/ppt_SelectionSort.pdf · selection sort. Every October, Halloween candies are consumed from best to worst. Whether](https://reader034.fdocuments.net/reader034/viewer/2022050312/5f7405228b63bf4c08084abc/html5/thumbnails/28.jpg)
[0] [1] [2] [3] [4] [5] [6] [7]
6 4 3 7 8 1 0 2
6 4 3 7 2 1 0 8
6 4 3 0 2 1 7 8
1 4 3 0 2 6 7 8
1 2 3 0 4 6 7 8
![Page 29: Selection Sort - gorskicompsci.cagorskicompsci.ca/ICS4U/3_Algorithms/ppt_SelectionSort.pdf · selection sort. Every October, Halloween candies are consumed from best to worst. Whether](https://reader034.fdocuments.net/reader034/viewer/2022050312/5f7405228b63bf4c08084abc/html5/thumbnails/29.jpg)
[0] [1] [2] [3] [4] [5] [6] [7]
6 4 3 7 8 1 0 2
6 4 3 7 2 1 0 8
6 4 3 0 2 1 7 8
1 4 3 0 2 6 7 8
1 2 3 0 4 6 7 8
![Page 30: Selection Sort - gorskicompsci.cagorskicompsci.ca/ICS4U/3_Algorithms/ppt_SelectionSort.pdf · selection sort. Every October, Halloween candies are consumed from best to worst. Whether](https://reader034.fdocuments.net/reader034/viewer/2022050312/5f7405228b63bf4c08084abc/html5/thumbnails/30.jpg)
[0] [1] [2] [3] [4] [5] [6] [7]
6 4 3 7 8 1 0 2
6 4 3 7 2 1 0 8
6 4 3 0 2 1 7 8
1 4 3 0 2 6 7 8
1 2 3 0 4 6 7 8
![Page 31: Selection Sort - gorskicompsci.cagorskicompsci.ca/ICS4U/3_Algorithms/ppt_SelectionSort.pdf · selection sort. Every October, Halloween candies are consumed from best to worst. Whether](https://reader034.fdocuments.net/reader034/viewer/2022050312/5f7405228b63bf4c08084abc/html5/thumbnails/31.jpg)
[0] [1] [2] [3] [4] [5] [6] [7]
6 4 3 7 8 1 0 2
6 4 3 7 2 1 0 8
6 4 3 0 2 1 7 8
1 4 3 0 2 6 7 8
1 2 3 0 4 6 7 8
1 2 0 3 4 6 7 8
![Page 32: Selection Sort - gorskicompsci.cagorskicompsci.ca/ICS4U/3_Algorithms/ppt_SelectionSort.pdf · selection sort. Every October, Halloween candies are consumed from best to worst. Whether](https://reader034.fdocuments.net/reader034/viewer/2022050312/5f7405228b63bf4c08084abc/html5/thumbnails/32.jpg)
[0] [1] [2] [3] [4] [5] [6] [7]
6 4 3 7 8 1 0 2
6 4 3 7 2 1 0 8
6 4 3 0 2 1 7 8
1 4 3 0 2 6 7 8
1 2 3 0 4 6 7 8
1 2 0 3 4 6 7 8
![Page 33: Selection Sort - gorskicompsci.cagorskicompsci.ca/ICS4U/3_Algorithms/ppt_SelectionSort.pdf · selection sort. Every October, Halloween candies are consumed from best to worst. Whether](https://reader034.fdocuments.net/reader034/viewer/2022050312/5f7405228b63bf4c08084abc/html5/thumbnails/33.jpg)
[0] [1] [2] [3] [4] [5] [6] [7]
6 4 3 7 8 1 0 2
6 4 3 7 2 1 0 8
6 4 3 0 2 1 7 8
1 4 3 0 2 6 7 8
1 2 3 0 4 6 7 8
1 2 0 3 4 6 7 8
![Page 34: Selection Sort - gorskicompsci.cagorskicompsci.ca/ICS4U/3_Algorithms/ppt_SelectionSort.pdf · selection sort. Every October, Halloween candies are consumed from best to worst. Whether](https://reader034.fdocuments.net/reader034/viewer/2022050312/5f7405228b63bf4c08084abc/html5/thumbnails/34.jpg)
[0] [1] [2] [3] [4] [5] [6] [7]
6 4 3 7 8 1 0 2
6 4 3 7 2 1 0 8
6 4 3 0 2 1 7 8
1 4 3 0 2 6 7 8
1 2 3 0 4 6 7 8
1 2 0 3 4 6 7 8
![Page 35: Selection Sort - gorskicompsci.cagorskicompsci.ca/ICS4U/3_Algorithms/ppt_SelectionSort.pdf · selection sort. Every October, Halloween candies are consumed from best to worst. Whether](https://reader034.fdocuments.net/reader034/viewer/2022050312/5f7405228b63bf4c08084abc/html5/thumbnails/35.jpg)
[0] [1] [2] [3] [4] [5] [6] [7]
6 4 3 7 8 1 0 2
6 4 3 7 2 1 0 8
6 4 3 0 2 1 7 8
1 4 3 0 2 6 7 8
1 2 3 0 4 6 7 8
1 2 0 3 4 6 7 8
1 0 2 3 4 6 7 8
![Page 36: Selection Sort - gorskicompsci.cagorskicompsci.ca/ICS4U/3_Algorithms/ppt_SelectionSort.pdf · selection sort. Every October, Halloween candies are consumed from best to worst. Whether](https://reader034.fdocuments.net/reader034/viewer/2022050312/5f7405228b63bf4c08084abc/html5/thumbnails/36.jpg)
[0] [1] [2] [3] [4] [5] [6] [7]
6 4 3 7 8 1 0 2
6 4 3 7 2 1 0 8
6 4 3 0 2 1 7 8
1 4 3 0 2 6 7 8
1 2 3 0 4 6 7 8
1 2 0 3 4 6 7 8
1 0 2 3 4 6 7 8
![Page 37: Selection Sort - gorskicompsci.cagorskicompsci.ca/ICS4U/3_Algorithms/ppt_SelectionSort.pdf · selection sort. Every October, Halloween candies are consumed from best to worst. Whether](https://reader034.fdocuments.net/reader034/viewer/2022050312/5f7405228b63bf4c08084abc/html5/thumbnails/37.jpg)
[0] [1] [2] [3] [4] [5] [6] [7]
6 4 3 7 8 1 0 2
6 4 3 7 2 1 0 8
6 4 3 0 2 1 7 8
1 4 3 0 2 6 7 8
1 2 3 0 4 6 7 8
1 2 0 3 4 6 7 8
1 0 2 3 4 6 7 8
![Page 38: Selection Sort - gorskicompsci.cagorskicompsci.ca/ICS4U/3_Algorithms/ppt_SelectionSort.pdf · selection sort. Every October, Halloween candies are consumed from best to worst. Whether](https://reader034.fdocuments.net/reader034/viewer/2022050312/5f7405228b63bf4c08084abc/html5/thumbnails/38.jpg)
[0] [1] [2] [3] [4] [5] [6] [7]
6 4 3 7 8 1 0 2
6 4 3 7 2 1 0 8
6 4 3 0 2 1 7 8
1 4 3 0 2 6 7 8
1 2 3 0 4 6 7 8
1 2 0 3 4 6 7 8
1 0 2 3 4 6 7 8
![Page 39: Selection Sort - gorskicompsci.cagorskicompsci.ca/ICS4U/3_Algorithms/ppt_SelectionSort.pdf · selection sort. Every October, Halloween candies are consumed from best to worst. Whether](https://reader034.fdocuments.net/reader034/viewer/2022050312/5f7405228b63bf4c08084abc/html5/thumbnails/39.jpg)
[0] [1] [2] [3] [4] [5] [6] [7]
6 4 3 7 8 1 0 2
6 4 3 7 2 1 0 8
6 4 3 0 2 1 7 8
1 4 3 0 2 6 7 8
1 2 3 0 4 6 7 8
1 2 0 3 4 6 7 8
1 0 2 3 4 6 7 8
0 1 2 3 4 6 7 8
![Page 40: Selection Sort - gorskicompsci.cagorskicompsci.ca/ICS4U/3_Algorithms/ppt_SelectionSort.pdf · selection sort. Every October, Halloween candies are consumed from best to worst. Whether](https://reader034.fdocuments.net/reader034/viewer/2022050312/5f7405228b63bf4c08084abc/html5/thumbnails/40.jpg)
[0] [1] [2] [3] [4] [5] [6] [7]
6 4 3 7 8 1 0 2
6 4 3 7 2 1 0 8
6 4 3 0 2 1 7 8
1 4 3 0 2 6 7 8
1 2 3 0 4 6 7 8
1 2 0 3 4 6 7 8
1 0 2 3 4 6 7 8
0 1 2 3 4 6 7 8
![Page 41: Selection Sort - gorskicompsci.cagorskicompsci.ca/ICS4U/3_Algorithms/ppt_SelectionSort.pdf · selection sort. Every October, Halloween candies are consumed from best to worst. Whether](https://reader034.fdocuments.net/reader034/viewer/2022050312/5f7405228b63bf4c08084abc/html5/thumbnails/41.jpg)