Download Quicksort Algorithm: Notes, Partitioning, and Implementation and more Slides Computer Science in PDF only on Docsity! Quicksort Algorithm Docsity.com Notes on Quicksort • Quicksort was invented in 1960 by C. A. R. Hoare. • Quicksort is more widely used than any other sort. • Quicksort is well-studied, not difficult to implement, works well on a variety of data, and consumes fewer resources that other sorts in nearly all situations. • Quicksort is O(n*log n) time, and O(log n) additional space due to recursion. Docsity.com Quicksort Algorithm (cont) • As is typical with a recursive program, once you figure out how to divide your problem into smaller subproblems, the implementation is amazingly simple. int partition(Item a[], int l, int r); void quicksort(Item a[], int l, int r) { int i; if (r <= l) return; i = partition(a, l, r); quicksort(a, l, i-1); quicksort(a, i+1, r); } Docsity.com
Quicksort
Quicksort.
» Partition array so that:
- some partitioning element a[m] is in its final position , e
- no larger element to the left of m C. A. R, Hoare
- no smaller element to the right of m
partitioning
> element
U
gi/ujzjc{/K|sj/o|j[ri[Tri{ri/siclolo Ky
u|s|o|r|t{siolo
t/c([k|i|/c Ho
~—S _
<L T 2L
partitioned array
Docsity .com,
Quicksort
Quicksort.
» Partition array so that:
- some partitioning element a[m] is in its final position
- no larger element to the left of m
- no smaller element to the right of m
» Sort each "half" recursively. partitioning
element
U
gi/ujzjc{/K|sj/o|j[ri[Tri{ri/siclolo Ky
c Cc I I K LOO 0 Q9 R 8S 85 T U
\ Sort each "half." ¢
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 partitioned partition element left right unpartitioned swap me Q U I C K S O R T I S C O O L Docsity.com 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 partitioned partition element left right unpartitioned swap me Q U I C K S O R T I S C O O L Docsity.com 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 partitioned partition element left right unpartitioned swap me Q U I C K S O R T I S C O O L swap me Docsity.com 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 partitioned partition element left right unpartitioned swap me C U I C K S O R T I S Q O O L Docsity.com 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 partitioned partition element left right unpartitioned swap me C U I C K S O R T I S Q O O L swap me Docsity.com 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 partitioned partition element left right unpartitioned C I I C K S O R T U S Q O O L Docsity.com 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 partitioned partition element left right unpartitioned C I I C K S O R T U S Q O O L Docsity.com 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 partitioned partition element left right unpartitioned C I I C K S O R T U S Q O O L Docsity.com 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 partitioned partition element left right unpartitioned swap me C I I C K S O R T U S Q O O L Docsity.com 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 pointers cross swap with partitioning element partitioned partition element left right unpartitioned C I I C K S O R T U S Q O O L Docsity.com 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 partitioned partition element left right unpartitioned partition is complete C I I C K L O R T U S Q O O S Docsity.com Partitioning in Quicksort int partition(Item a[], int l, int r) { int i = l-1, j = r; Item v = a[r]; for (;;) { while (less(a[++i], v)) ; while (less(v, a[--j])) if (j == l) break; if (i >= j) break; exch(a[i], a[j]); } exch(a[i], a[r]); return i; Docsity.com