Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
2 9 4 7 6 2 4 6 7 9
2 4 2 4
9 7 7 9
22
99
Quick-Sort
Quick-sort is a sorting
algorithm based on the
divide-and-conquer
paradigm:
x
L
x
2
Quick-Sort (cont.)
A Quick-Sort algorithm consists of 2
functions/algorithms: a recursive function
called quicksort, and a non-recursive function
called partition.
quicksort calls partition.
quicksort Function
Algorithm quicksort (S, p, r)
Input Array S, start index p, end index
r.
Output S is sorted.
if p < r
pi = partition (S, p, r)
quicksort (S, p, pi-1)
quicksort (S, pi+1, r)
4
Partition
We partition an input
sequence as follows:
We remove, in turn,
each element y from S
and
We insert y into L, E or
G, depending on the
result of the
comparison with the
pivot x
Algorithm partition(S)
Input sequence S.
Output All elements less than or equal to
pivot are placed before pivot, and all elements
greater than pivot are placed after pivot.
L, E, G empty sequences
x S.last() // pivot
while S.isEmpty()
y S.remove(S.first())
if y < x
L.insertLast(y)
else if y = x
E.insertLast(y)
else // y > x
G.insertLast(y)
while L.isEmpty()
S.insertLast(L.remove(L.first()))
pi = S.size() // pivot index
while E.isEmpty()
S.insertLast(E.remove(E.first()))
while G.isEmpty()
S.insertLast(G.remove(G.first()))
return pi
5
Quick-Sort Tree
An execution of quick-sort is depicted by a
binary tree
2 4 2 4
22
9 7 7 9
99
6
4 3 1 2
7 9 7 8
4 3 1 2
11
7 9 7 8
4 3
4 3 1 2
11
7 9 7 8
4 3
10
4 3 1 2
11
7 9 7 8
4 3
44
11
4 3 1 2
11
7 9 7 8
4 3 3 4
44
12
4 3 1 2 1 2 3 4
11
7 9 7 8
4 3 3 4
44
13
4 3 1 2 1 2 3 4
11
7 9 7 8
4 3 3 4
44
14
4 3 1 2 1 2 3 4
11
4 3 3 4
7 9 7 8
99
7 7
44
15
4 3 1 2 1 2 3 4
11
4 3 3 4
44
7 9 7 8
99
7 7
77
16
4 3 1 2 1 2 3 4
11
4 3 3 4
44
7 9 7 8
99
7 7 7 7
77
17
4 3 1 2 1 2 3 4
11
4 3 3 4
44
7 9 7 8
99
7 7 7 7
77
18
4 3 1 2 1 2 3 4
11
4 3 3 4
44
7 9 7 8 7 7 8 9
99
7 7 7 7
77
19
Array Output
The previous Quick-Sort tree and the
following array intermediate result are
equivalent.
1. 7 9 4 3 7 1 2 8 6
2. 4 3 1 2 6* 7 9 7 8
3. 1 2* 4 3 6* 7 9 7 8
4. 1 2* 3* 4 6* 7 9 7 8
5. 1 2* 3* 4 6* 7 7 8* 9
6. 1 2* 3* 4 6* 7 7* 8* 9
20
Time Complexities
Best: O(n log n)
Average: O(n log n)
Worst: O(n2) if pivot is not randomized, O(n
log n) if pivot is randomized.
The average case usually dominates over
the worst case in sorting algorithms, and
in Quicksort, the coefficient of the n log n
term is small, makes Quicksort one of the
most popular general-purpose sorting
algorithms.
21
Stability of Quick-Sort
The partition algorithm shown earlier is
stable and easy to understand. However
it is not space efficient because it
requires auxiliary storages to store L, E,
and G.
Space efficient implementations of QuickSort are unstable.
22
23
Unstable Quick-Sort
Output
If we follow the unstable partition
algorithm given earlier strictly, the
intermediate results would seem more
unpredictable as follow:
1.7 9 4 3 7 1 2 8 6
2.4 3 1 2 6* 7 9 8 7
(not 7 9 7 8 anymore)
3.1 2* 4 3 6* 7 9 8 7
4.1 2* 3* 4 6* 7 9 8 7
5.1 2* 3* 4 6* 7 7* 8 9
6.1 2* 3* 4 6* 7 7* 8 9*
24