ICOM 4015 Advanced Programming Lecture 5 Búsqueda y Ordenamiento III Reading: LNN Chapters 9, 10 & 16 Prof. Bienvenido Velez 5/25/2019 ICOM 4015
Busqueda y Ordenamiento III Outline Sorting Algorithms Insertion Sort Design Implementation Analysis QuickSort 5/25/2019 ICOM 4015
Insertion Sort The Algorithm 1 2 3 4 5 6 7 8 9 1 1 3 2 4 3 5 4 2 5 7 7 8 8 9 9 6 6 5/25/2019 ICOM 4015
Example 1 - main function // sort.cc // Implements insertion sort algorithm #include <iostream> // Forward declarations template <class TYPE> void insertionSort(TYPE list[], long numElements); void swap(TYPE& a, TYPE& b); void printArray(TYPE list[], long numElements); main() { int a[] = {34, 56, 28, 86, 52, 1, 92, 44, 19, 78}; int b[] = {19, 1, 28, 34, 44, 52, 56, 78, 86, 92}; const long elements = 10; cout << "Original array:" << endl; printArray(a, elements); insertionSort(a, elements); cout << "Sorted array:" << endl; printArray(b, elements); insertionSort(b, elements); return 0; } 5/25/2019 ICOM 4015
Sorting Function Contract Parameters An array of some type The number of valid elements in array Changes to parameters Array parameter sorted Return value (void) None Side effects 5/25/2019 ICOM 4015
Insertion Sort // Definitions of local auxiliary functions template <class TYPE> void printArray(TYPE list[], long numElements) { for (long i=0; i<numElements; i++) { cout << "list[" << i << "] = " << list[i] << endl; } void swap(TYPE& a, TYPE& b) TYPE temp = a; a = b; b = temp; void insertionSort(TYPE list[], long numElements) int comparisons = 0; for (long i=1; i<numElements; i++) { long j = i; while ((j>0) && (list[j] < list[j-1])) { comparisons ++; swap(list[j], list[j-1]); j--; cout << "Insertion sort comparisons = " << comparisons << endl; 5/25/2019 ICOM 4015
Inseertion Sort Output [bvelez@amadeus] ~/icom4015/lec13 >>./sort Original array: list[0] = 34 list[1] = 56 list[2] = 28 list[3] = 86 list[4] = 52 list[5] = 1 list[6] = 92 list[7] = 44 list[8] = 19 list[9] = 78 Insertion sort comparisons = 22 Sorted array: list[0] = 1 list[1] = 19 list[3] = 34 list[4] = 44 list[5] = 52 list[6] = 56 list[7] = 78 list[8] = 86 list[9] = 92 list[0] = 19 list[1] = 1 Insertion sort comparisons = 1 [bvelez@amadeus] ~/icom4015/lec13 >> 5/25/2019 ICOM 4015
Insertion Sort Analysis Case Number of Comparisons Description of Input Best Case O(N) Array Already Sorted Average Case O(N2) ?? Worst Case Array in Reverse Order 5/25/2019 ICOM 4015
QuickSort: The Algorithm pivot 5 9 1 8 3 6 7 4 2 5 9 1 8 3 6 7 4 2 partition partition 5/25/2019 ICOM 4015
Recursive Implementation of QuickSort template <class TYPE> void quickSort(TYPE list[], long numElements) { internalQuickSort(list, numElements, 0, numElements-1); } static void internalQuickSort(TYPE list[], long numElements, int low, int high) if (low < high) { long pivotLocation = partition(list, numElements, low, high); internalQuickSort(list, numElements, low, pivotLocation-1); internalQuickSort(list, numElements, pivotLocation+1, high); return; 5/25/2019 ICOM 4015
Partition 5/25/2019 ICOM 4015 template <class TYPE> static long partition(TYPE list[], long numElements, int low, int high) { cout << "low " << low << " high " << high << endl; TYPE pivot = list[low]; long pivotLocation = low; for (long i=low+1; i<=high; i++) { if (list[i] < pivot) { pivotLocation++; swap(list[i], list[pivotLocation]); } swap(list[low],list[pivotLocation]); return pivotLocation; 5/25/2019 ICOM 4015
Quicksort Best Case Analysis Tq(N) ~ time to quicksort an N element array Tq(N) = Tpartition(N) + 2Tq(N/2) Time to partition Time to sort 2 halves Tq(N) = O(NlogN) Demonstrated both experimentally and theoretically that average case is also O(NlogN) 5/25/2019 ICOM 4015
QuickSort Analysis and Comparison Insertion Sort Quicksort Case Number of Comparisons Description of Input Best Case O(N) Array Already Sorted O(NlogN) Pivot always in middle Average Case O(N2) See Algorithms Course Worst Case Array in Reverse Order Pivot always an extreme 5/25/2019 ICOM 4015
Sorting summary of concepts Insertion Sort Worst/average case O(N2) Sensitive to input entropy Examples of other O(N2) algorithms SelectionSort, BubbleSort,ShellSort Quicksort Worst case O(N2) Best/average case O(NlogN) Not sensitive to entropy Other O(NlogN) algorithms MergeSort O(NlogN) is an optimal bound on comparison based sorting algorithms 5/25/2019 ICOM 4015