Title: Quicksort Algorithm
1Quicksort Algorithm
Department of Computer and Information
Science,School of Science, IUPUI
Dale Roberts, Lecturer Computer Science,
IUPUI E-mail droberts_at_cs.iupui.edu
2(No Transcript)
3(No Transcript)
4(No Transcript)
5Notes 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(nlog n) time, and O(log n)
additional space due to recursion.
6Notes 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 its 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.
7Quicksort 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, ai, where ai is in its
final position. - For all l lt i, al lt ai.
- For all i lt r, ai lt ar.
8Quicksort 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 lt l) return
- i partition(a, l, r)
- quicksort(a, l, i-1)
- quicksort(a, i1, r)
-
9(No Transcript)
10(No Transcript)
11Partitioning 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
12Partitioning 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
I
S
C
O
O
L
13Partitioning 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
I
S
C
O
O
L
14Partitioning 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
I
S
C
O
O
L
15Partitioning 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
I
S
C
O
O
L
16Partitioning 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
17Partitioning 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
18Partitioning 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
19Partitioning 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
20Partitioning 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
21Partitioning 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
22Partitioning 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
23Partitioning 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
24Partitioning 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
25Partitioning 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
26Partitioning 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
27Partitioning 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
28Partitioning 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
29Partitioning 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
30Partitioning in Quicksort
- int partition(Item a, int l, int r)
- int i l-1, j r Item v ar
- for ()
-
- while (less(ai, v))
- while (less(v, a--j)) if (j l)
break - if (i gt j) break
- exch(ai, aj)
-
- exch(ai, ar)
- return i
31Quicksort 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.