1 / 10

Quick Sort: Array-Based Lists

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.

rue
Download Presentation

Quick Sort: Array-Based Lists

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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. }

  3. 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); } }

  4. 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; }

  5. Analysis of Quick Sort Algorithm for List of length n

  6. 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

  7. Merge sort algorithm

  8. 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); } }

  9. 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++]; } }

  10. 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)

More Related