![Page 2: Intro to CS – Honors I Basic Sorting GEORGIOS PORTOKALIDIS GPORTOKA@STEVENS.EDU](https://reader036.vdocuments.net/reader036/viewer/2022082710/56649dc55503460f94ab90d3/html5/thumbnails/2.jpg)
Sorting Arranging a collection of items into a particular order
If we are talking about integers◦ a[0] ≤ a[1] ≤ a[2] ≤ ... ≤ a[a.length - 1]
The simplest algorithm in pseudocode
for (index = 0; index < a.length; index++)
Place the (index + 1)th smallest element in a[index]
Can you expand this algorithm? What considerations should you take into account?
![Page 3: Intro to CS – Honors I Basic Sorting GEORGIOS PORTOKALIDIS GPORTOKA@STEVENS.EDU](https://reader036.vdocuments.net/reader036/viewer/2022082710/56649dc55503460f94ab90d3/html5/thumbnails/3.jpg)
Selection Sort Some requirements
◦ We want the algorithm to use only this one array a◦ There are other algorithms we could use, if we could use more memory
How can we move array elements under these conditions?
We will swap elements
All sorting algorithms that swap elements are called interchange sorting algorithm
![Page 4: Intro to CS – Honors I Basic Sorting GEORGIOS PORTOKALIDIS GPORTOKA@STEVENS.EDU](https://reader036.vdocuments.net/reader036/viewer/2022082710/56649dc55503460f94ab90d3/html5/thumbnails/4.jpg)
Find the smallest element and move it to its rightful place
Can you write an algorithm in pseudocode to implement this?
![Page 5: Intro to CS – Honors I Basic Sorting GEORGIOS PORTOKALIDIS GPORTOKA@STEVENS.EDU](https://reader036.vdocuments.net/reader036/viewer/2022082710/56649dc55503460f94ab90d3/html5/thumbnails/5.jpg)
Pseudocode for SelectionSortfor (index = 0; index <a.length − 1; index++){
// Place the correct value in a[index]:indexOfNextSmallest = the index of the smallest value among
a[index], a[index+1],..., a[a.length - 1]
Interchange the values of a[index] and a[indexOfNextSmallest].//Assertion: a[0] <= a[1] <= ... <= a[index] and these//are the smallest of the original array elements.//The remaining positions contain the rest of the//original array elements.
}
![Page 6: Intro to CS – Honors I Basic Sorting GEORGIOS PORTOKALIDIS GPORTOKA@STEVENS.EDU](https://reader036.vdocuments.net/reader036/viewer/2022082710/56649dc55503460f94ab90d3/html5/thumbnails/6.jpg)
/**Precondition: i and j are valid indices for the array a.Postcondition: Values of a[i] and a[j] have been interchanged.*/private static void interchange(int i, int j, int[] a){
int temp = a[i];a[i] = a[j];a[j] = temp; //original value of a[i]
}
private static int getIndexOfSmallest(int startIndex, int[] a){
int min = a[startIndex];int indexOfMin = startIndex;for (int index = startIndex + 1; index < a.length;
index++){
if (a[index] < min){
min = a[index];indexOfMin = index;//min is smallest of a[startIndex]
through a[index]}
}return indexOfMin;
}
public static void selectionSort(int[] anArray){
for (int index = 0; index < anArray.length − 1; index++){ // Place the correct value in anArray[index]
int indexOfNextSmallest = getIndexOfSmallest(index, anArray);
interchange(index, indexOfNextSmallest, anArray);}
}
![Page 7: Intro to CS – Honors I Basic Sorting GEORGIOS PORTOKALIDIS GPORTOKA@STEVENS.EDU](https://reader036.vdocuments.net/reader036/viewer/2022082710/56649dc55503460f94ab90d3/html5/thumbnails/7.jpg)
Selection Sort in Action
![Page 8: Intro to CS – Honors I Basic Sorting GEORGIOS PORTOKALIDIS GPORTOKA@STEVENS.EDU](https://reader036.vdocuments.net/reader036/viewer/2022082710/56649dc55503460f94ab90d3/html5/thumbnails/8.jpg)
Why Use Selection Sort It is simple!
Not very efficient
Simple implies it is easier to implement and less prone to errors
![Page 9: Intro to CS – Honors I Basic Sorting GEORGIOS PORTOKALIDIS GPORTOKA@STEVENS.EDU](https://reader036.vdocuments.net/reader036/viewer/2022082710/56649dc55503460f94ab90d3/html5/thumbnails/9.jpg)
Complexity What is the worst-case scenario?
What is the maximum number of comparisons that selection sort may require?
For example to swap the first element you require (n – 1) comparisons
(n − 1) + (n − 2) + ... + 2 + 1 = n(n − 1) / 2 ≈ n2 ◦ Arithmetic progression
This is indicated as O(n2)
![Page 10: Intro to CS – Honors I Basic Sorting GEORGIOS PORTOKALIDIS GPORTOKA@STEVENS.EDU](https://reader036.vdocuments.net/reader036/viewer/2022082710/56649dc55503460f94ab90d3/html5/thumbnails/10.jpg)
Searching Arrays What is the simplest way to look for an item in an array?
You can sequentially check each item
However, sorted arrays enable other algorithms◦ Example: binary search
![Page 11: Intro to CS – Honors I Basic Sorting GEORGIOS PORTOKALIDIS GPORTOKA@STEVENS.EDU](https://reader036.vdocuments.net/reader036/viewer/2022082710/56649dc55503460f94ab90d3/html5/thumbnails/11.jpg)