140 likes | 159 Vues
ICOM 4015 Advanced Programming. Lecture 5 B ú squeda y Ordenamiento III Reading: LNN Chapters 9, 10 & 16. Prof. Bienvenido Velez. Busqueda y Ordenamiento III Outline. Sorting Algorithms Insertion Sort Design Implementation Analysis QuickSort Design Implementation Analysis. 1. 2.
E N D
ICOM 4015 Advanced Programming Lecture 5 Búsqueda y Ordenamiento III Reading: LNN Chapters 9, 10 & 16 Prof. Bienvenido Velez ICOM 4015
Busqueda y Ordenamiento III Outline • Sorting Algorithms • Insertion Sort • Design • Implementation • Analysis • QuickSort • Design • Implementation • Analysis ICOM 4015
1 2 3 4 5 6 7 8 9 Insertion SortThe Algorithm 1 1 3 2 4 3 5 4 2 5 7 7 8 8 9 9 6 6 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); template <class TYPE> void swap(TYPE& a, TYPE& b); template <class TYPE> 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(a, elements); cout << "Original array:" << endl; printArray(b, elements); insertionSort(b, elements); cout << "Sorted array:" << endl; printArray(b, elements); return 0; } 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 • None 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; } } template <class TYPE> void swap(TYPE& a, TYPE& b) { TYPE temp = a; a = b; b = temp; } template <class TYPE> 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; } 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[2] = 28 list[3] = 34 list[4] = 44 list[5] = 52 list[6] = 56 list[7] = 78 list[8] = 86 list[9] = 92 Original array: list[0] = 19 list[1] = 1 list[2] = 28 list[3] = 34 list[4] = 44 list[5] = 52 list[6] = 56 list[7] = 78 list[8] = 86 list[9] = 92 Insertion sort comparisons = 1 Sorted array: list[0] = 1 list[1] = 19 list[2] = 28 list[3] = 34 list[4] = 44 list[5] = 52 list[6] = 56 list[7] = 78 list[8] = 86 list[9] = 92 [bvelez@amadeus] ~/icom4015/lec13 >> ICOM 4015
Insertion SortAnalysis ICOM 4015
5 2 1 1 8 4 3 3 9 9 7 7 4 8 2 5 6 6 QuickSort: The Algorithm pivot partition partition ICOM 4015
Recursive Implementation of QuickSort template <class TYPE> void quickSort(TYPE list[], long numElements) { internalQuickSort(list, numElements, 0, numElements-1); } template <class TYPE> 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; } ICOM 4015
Partition 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; } 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) ICOM 4015
QuickSortAnalysis and Comparison 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 ICOM 4015