110 likes | 198 Views
Intro to CS – Honors I Basic Sorting. Georgios Portokalidis gportoka@stevens.edu. 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
E N D
Intro to CS – Honors IBasic Sorting Georgios Portokalidis gportoka@stevens.edu
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)thsmallest element in a[index] • Can you expand this algorithm? What considerations should you take into account?
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
Find the smallest element and move it to its rightful place Can you write an algorithm in pseudocode to implement this?
Pseudocode for SelectionSort for (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. }
/** 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(inti, int j, int[] a) { inttemp = a[i]; a[i] = a[j]; a[j] = temp; //original value of a[i] } public static void selectionSort(int[] anArray) { for (int index = 0; index < anArray.length − 1; index++) { // Place the correct value in anArray[index] intindexOfNextSmallest = getIndexOfSmallest(index, anArray); interchange(index, indexOfNextSmallest, anArray); } } private static intgetIndexOfSmallest(intstartIndex, int[] a) { intmin = a[startIndex]; intindexOfMin = 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; }
Why Use Selection Sort • It is simple! • Not very efficient • Simple implies it is easier to implement and less prone to errors
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)
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