Download presentation
Presentation is loading. Please wait.
Published bySiska Oesman Modified over 6 years ago
1
Department of Computer and Information Science, School of Science, IUPUI
Quicksort Dale Roberts, Lecturer Computer Science, IUPUI
5
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.
6
Notes on Quicksort Quicksort has withstood the test of time. It has been thoroughly analyzed. The analysis has been verified through extensive empirical experience. Quicksort is not stable. Quicksort performance can degenerate under special circumstances. It is possible modify the algorithm to handle these cases, but at the expense of making the algorithm more complicated. Sedgewick states that “tuning Quicksort is the better 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.
7
Quicksort Algorithm Quicksort is a divide-and-conquer method for sorting. It works by partitioning an array into parts, then sorting each part independently. The crux of the problem is how to partition the array such that the following conditions are true: There is some element, a[i], where a[i] is in its final position. For all l < i, a[l] < a[i]. For all i < r, a[i] < a[r].
8
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); }
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 Q U I C K S O R T L partitioned partition element left right unpartitioned
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 S O R T I S C O O L partitioned partition element left right unpartitioned
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 S O R T I S C O O L partitioned partition element left right unpartitioned
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 from right for smaller element exchange repeat until pointers cross swap me Q U I C K S O R T I S C O O L partitioned partition element left right unpartitioned
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 swap me swap me Q U I C K S O R T I S C O O L partitioned partition element left right unpartitioned
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 C U I C K S O R T I S Q O O L partitioned partition element left right unpartitioned
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 S O R T I S Q O O L partitioned partition element left right unpartitioned
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 C K S O R T I S Q O O L partitioned partition element left right unpartitioned
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 swap me swap me C U I C K S O R T I S Q O O L partitioned partition element left right unpartitioned
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 S O R T U S Q O O L partitioned partition element left right unpartitioned
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 S O R T U S Q O O L partitioned partition element left right unpartitioned
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 S O R T U S Q O O L partitioned partition element left right unpartitioned
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 C I I C K S O R T U S Q O O L partitioned partition element left right unpartitioned
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 S O R T U S Q O O L partitioned partition element left right unpartitioned
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 S O R T U S Q O O L partitioned partition element left right unpartitioned
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 S O R T U S Q O O L partitioned partition element left right unpartitioned
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 from right for smaller element exchange repeat until pointers cross swap me C I I C K S O R T U S Q O O L partitioned partition element left right unpartitioned
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 swap with partitioning element pointers cross C I I C K S O R T U S Q O O L partitioned partition element left right unpartitioned
29
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 L O R T U S Q O O S partitioned partition element left right unpartitioned
30
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;
31
Correct formula is N(N+1)/2
33
Sedgewick Formula 7.2
36
Sidebar – Nonrecursive Implementation
When you learn about compilers, you will learn that function calls create local variables on a stack. Any recursive program can be written as a nonrecursive program with an explicit stack. #define push2(A, B) push(B); push(A); void quicksort(Item a[], int l, int r) { int i; stackinit(); push2(l, r); while (!stackempty()) { l = pop(); r = pop(); if (r <= l) continue; i = partition(a, l, r); if (i-l > r-i) { push2(l, i-1); push2(i+1, r); } else { push2(i+1, r); push2(l, i-1); } }
37
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.
38
Sort Summary
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.