#15 Quick Sort

# #15 Quick Sort - DATA STRUCTURES Quicksort Algorithm...

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: DATA STRUCTURES Quicksort Algorithm Algorithm 1 2 3 4 Notes on Quicksort Quicksort was invented in 1960 by C. A. R. Quicksort Hoare. Hoare. Quicksort is more widely used than any other Quicksort sort. sort. Quicksort is well-studied, not difficult to Quicksort implement, works well on a variety of data, and consumes fewer resources that other sorts in nearly all situations. nearly Quicksort is O(n*log n) time, and O(log n) Quicksort additional space due to recursion. additional 5 Notes on Quicksort Quicksort has withstood the test of time. It has been Quicksort thoroughly analyzed. The analysis has been verified through extensive empirical experience. through Quicksort is not stable. Quicksort performance can degenerate under special Quicksort circumstances. It is possible modify the algorithm to handle these cases, but at the expense of making the algorithm more complicated. algorithm Sedgewick states that “tuning Quicksort is the better Sedgewick mousetrap of computer science.” Many ideas have been tried, but it’s easy to be deceived because the algorithm is so balanced that a perceived improvement in one area can be more than offset by poor performance in another area. area. 6 Quicksort Algorithm Quicksort is a divide-and-conquer method for Quicksort sorting. It works by partitioning an array into parts, then sorting each part independently. parts, The crux of the problem is how to partition the The array such that the following conditions are true: true: There is some element, a[i], where a[i] is There in its final position. in For all l < i, a[l] < a[i]. For all i < r, a[i] < a[r]. 7 Quicksort Algorithm (cont) As is typical with a recursive program, once you figure As out how to divide your problem into smaller subproblems, the implementation is amazingly simple. subproblems, int partition(Item a, int l, int r); void quicksort(Item a, int l, int r) { int i; int if (r <= l) return; if i = partition(a, l, r); partition(a, quicksort(a, l, i-1); quicksort(a, quicksort(a, i+1, r); quicksort(a, } 8 9 10 Partitioning in Quicksort How do we partition the array efficiently? choose partition element to be rightmost element scan from left for larger element scan scan from right for smaller element scan exchange repeat until pointers cross Q U I C K partition element S O R T I S C O O unpartitioned left partitioned L right 11 Partitioning in Quicksort How do we partition the array efficiently? choose partition element to be rightmost element scan from left for larger element scan from right for smaller element exchange repeat until pointers cross swap me Q U I C K partition element S O R T I S C O O unpartitioned left partitioned L right 12 Partitioning in Quicksort How do we partition the array efficiently? choose partition element to be rightmost element scan from left for larger element scan from right for smaller element exchange repeat until pointers cross swap me Q U I C K partition element S O R T I S C O O unpartitioned left partitioned L right 13 Partitioning in Quicksort How do we partition the array efficiently? choose partition element to be rightmost element scan from left for larger element scan from right for smaller element exchange repeat until pointers cross swap me Q U I C K partition element S O R T I S C O O unpartitioned left partitioned L right 14 Partitioning in Quicksort How do we partition the array efficiently? choose partition element to be rightmost element scan from left for larger element scan scan from right for smaller element scan exchange repeat until pointers cross swap me Q swap me U I C K partition element S O R T I S C O O unpartitioned left partitioned L right 15 Partitioning in Quicksort How do we partition the array efficiently? choose partition element to be rightmost element scan from left for larger element scan from right for smaller element exchange repeat until pointers cross C U I C K partition element S O R T I S Q O O unpartitioned left partitioned L right 16 Partitioning in Quicksort How do we partition the array efficiently? choose partition element to be rightmost element scan from left for larger element scan from right for smaller element exchange repeat until pointers cross swap me C U I C K partition element S O R T I S Q O O unpartitioned left partitioned L right 17 Partitioning in Quicksort How do we partition the array efficiently? choose partition element to be rightmost element scan from left for larger element scan from right for smaller element exchange repeat until pointers cross swap me C U I C K partition element S O R T I S Q O O unpartitioned left partitioned L right 18 Partitioning in Quicksort How do we partition the array efficiently? choose partition element to be rightmost element scan from left for larger element scan from right for smaller element exchange repeat until pointers cross swap me C U I swap me C K partition element S O R T I S Q O O unpartitioned left partitioned L right 19 Partitioning in Quicksort How do we partition the array efficiently? choose partition element to be rightmost element scan from left for larger element scan from right for smaller element exchange repeat until pointers cross C I I C K partition element S O R T U S Q O O unpartitioned left partitioned L right 20 Partitioning in Quicksort How do we partition the array efficiently? choose partition element to be rightmost element scan from left for larger element scan from right for smaller element exchange repeat until pointers cross C I I C K partition element S O R T U S Q O O unpartitioned left partitioned L right 21 Partitioning in Quicksort How do we partition the array efficiently? choose partition element to be rightmost element scan from left for larger element scan from right for smaller element exchange repeat until pointers cross C I I C K partition element S O R T U S Q O O unpartitioned left partitioned L right 22 Partitioning in Quicksort How do we partition the array efficiently? choose partition element to be rightmost element scan from left for larger element scan from right for smaller element exchange repeat until pointers cross C I I C K partition element S O R T U S Q O O unpartitioned left partitioned L right 23 Partitioning in Quicksort How do we partition the array efficiently? choose partition element to be rightmost element scan from left for larger element scan from right for smaller element exchange repeat until pointers cross swap me C I I C K partition element S O R T U S Q O O unpartitioned left partitioned L right 24 Partitioning in Quicksort How do we partition the array efficiently? choose partition element to be rightmost element scan from left for larger element scan from right for smaller element exchange repeat until pointers cross swap me C I I C K partition element S O R T U S Q O O unpartitioned left partitioned L right 25 Partitioning in Quicksort How do we partition the array efficiently? choose partition element to be rightmost element scan from left for larger element scan from right for smaller element exchange repeat until pointers cross swap me C I I C K partition element S O R T U S Q O O unpartitioned left partitioned L right 26 Partitioning in Quicksort How do we partition the array efficiently? choose partition element to be rightmost element scan from left for larger element scan from right for smaller element exchange repeat until pointers cross swap me C I I C K partition element S O R T U S Q O O unpartitioned left partitioned L right 27 Partitioning in Quicksort How do we partition the array efficiently? choose partition element to be rightmost element scan from left for larger element scan scan from right for smaller element scan exchange repeat until pointers cross swap with partitioning element pointers cross C I I C K partition element S O R T U S Q O O unpartitioned left partitioned L right 28 Partitioning in Quicksort How do we partition the array efficiently? choose partition element to be rightmost element scan from left for larger element scan from right for smaller element exchange repeat until pointers cross partition is complete C I I C K partition element L O R T U S Q O O unpartitioned left partitioned S right 29 Partitioning in Quicksort int partition(Item a, int l, int int r) r) { int i = l-1, j = r; Item v = a[r]; a[r]; for (;;) for { while (less(a[++i], v)) ; while while (less(v, a[--j])) if (j == l) break; (j if (i >= j) break; if exch(a[i], a[j]); exch(a[i], } 30 Quicksort Demo Quicksort illustrates the operation of the basic algorithm. When the array is partitioned, one element is in place on the diagonal, the left subarray has its upper corner at that element, and the right subarray has its lower corner at that element. The original file is divided into two smaller parts that are sorted independently. The left subarray is always sorted first, so the sorted result emerges as a line of black dots moving right and up the diagonal. right 31 ...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online