100 likes | 335 Views
Quick Sort: Array-Based Lists. The quick sort algorithm uses the divide-and-conquer technique to sort a list The list is partitioned into two sublists, and the two sublists are then sorted and combined into one list in such a way that the combined list is sorted. Quick Sort: Array-Based Lists.
E N D
Quick Sort: Array-Based Lists • The quick sort algorithm uses the divide-and-conquer technique to sort a list • The list is partitioned into two sublists, and the two sublists are then sorted and combined into one list in such a way that the combined list is sorted
Quick Sort: Array-Based Lists • The general algorithm is: if (list size is greater than 1) { 1. Partition the list into two sublists, say lowerSublist and upperSublist. 2. Quick sort lowerSublist. 3. Quick sort upperSublist. 4. Combine the sorted lowerSublist and sorted upperSublist. }
Program 7.1 (modified) template <class Item, class Item2, class Item3> void Table<Item, Item2, Item3>::QuickSort () { DoQuickSort (0, used-1); } template <class Item, class Item2, class Item3> void Table<Item, Item2, Item3>::DoQuickSort(int first, int last) { int splitPoint; if (last > first) { splitPoint = Split (first, last); DoQuickSort (first, splitPoint - 1); DoQuickSort (splitPoint + 1, last); } }
Program 7.2 template <class Item, class Item2, class Item3> int Table<Item, Item2, Item3>:: Partition (int first, int last) { int right = first + 1; int left = last; Item v = data[first]; Item2 k2; do { v.GetKey(k2); while ((right <= left) && (data[right].CompareKeys(k2)!= 1)) right++; while ((right <= left) && (data[left].CompareKeys(k2)==1)) left--; if (right < left) Swap(data[left], data[right]); } while (right <= left); Swap(data[left], data[first]); return left; }
Merge Sort: Linked List-Based • Merge sort uses the divide-and-conquer technique to sort a list • It partitions the list into two sublists, and then combines the sorted sublists into one sorted list • It partitions the list into nearly equal sizes • For example, consider the list: List: 35 28 18 45 62 48 30 38 • Merge sort partitions this list into two sublists as follows first sublist: 35 28 18 45 second sublist: 62 48 30 38
Program 8.3 template <class Item, class Item2, class Item3> void Table<Item, Item2, Item3>::MergeSort () { DoMergeSort (0, used-1); } template <class Item, class Item2, class Item3> void Table<Item, Item2, Item3>::DoMergeSort(int first, int last) { if (last > first) { int m = (last +first ) /2; DoMergeSort (first, m); DoMergeSort(m + 1, last); Merge (first, m, last); } }
Program 8.2 template <class Item, class Item2, class Item3> int Table<Item, Item2, Item3>:: Merge (int first, int m, int last) { int i, j; //need LCVs after loop Item * aux = new [last-first+1]; //need a temp array //copy first half of data into aux, leaves i at first for (i = m+1; i > first; i--) aux[i-1] = data[i-1]; //copy second half of data into aux, leaves j at last for (j = m; j < last; j++) aux[last+m-j] = data[j+1]; //merge: compare item in first half of aux with item in second, // copy smallest to data array for (int k= first; k<= last; k++) { if (aux[j] < aux[i]) data[k] = aux[j--]; else data[k] = aux[i++]; } }
Quick Sort and Merge Sort Tradeoffs • Quick Sort • Benefit • Easy to understand • Disadvantage • Worst case on ordered array is O(N2) • Recursion overhead, but there are iterative versions • Merge Sort • Benefit • O(NlogN) no matter what the input • Faster than Quicksort in best case • Disadvantage • temporary array (there are improved versions, but more complex and costly)