Professional Documents
Culture Documents
Sorting and Order Statistics (1x1)
Sorting and Order Statistics (1x1)
Yonas Y.
School of Electrical and Computer Engineering
1
1 Introduction
2 Heapsort
Heaps
Maintaining the heap property
Building a heap
The heapsort algorithm
Priority queues
3 Quicksort
Quicksort
Performance of quicksort
Analysis of quicksort
4 Sorting in Linear Time
Lower bounds for sorting
Counting sort
Radix sort
Bucket sort
5 Medians and Order Statistics
Introduction
2
Introduction
3
There are a number of sorting algorithms with different expected
running time and applicability as shown below.
4
Heapsort
5
Heaps
Heaps
6
Heaps
Heap property
9
Maintaining the heap property
Algorithm 1 MAX-HEAPIFY(A, i)
1: l = Left(i)
2: r = Right(i)
3: if l ≤ A.heap − size and A[l] > A[i] then
4: largest = l
5: else
6: largest = i
7: end if
8: if r ≤ A.heap − size and A[r ] > A[largest] then
9: largest = r
10: end if
11: if largest 6= i then
12: exchange A[i] with A[largest]
13: MAX-HEAPIFY(A, largest)
14: end if
10
Maintaining the heap property
11
Maintaining the heap property
13
Maintaining the heap property
The worst case occurs when the bottom level of the tree is
exactly half full.
14
Building a heap
Building a heap
Algorithm 2 BUILD-MAX-HEAP(A)
1: A.heap − size = A.length
2: for i = bA.length/2c downto 1 do
3: MAX-HEAPIFY(A, i)
4: end for
15
Building a heap
16
Building a heap
17
Building a heap
At the start of each iteration of the for loop of lines 2-3, each
node i + 1, i + 2,..., n is the root of a max-heap.
18
Building a heap
19
Building a heap
Analysis
Simple bound:
20
Building a heap
Tighter analysis:
21
Building a heap
Evaluate the
P∞last summation by substituting x = 1/2 in the
k , which yields
formula k=0 kx
∞
!
X h 1/2
=
2h (1 − 1/2)2
h=0
=2
22
The heapsort algorithm
Algorithm 3 HEAPSORT(A)
1: BUILD-MAX-HEAP(A)
2: for i = A.length downto 2 do
3: exchange A[1] with A[i]
4: A.heap − size = A.heap − size − 1
5: MAX-HEAPIFY(A, 1)
6: end for
24
The heapsort algorithm
Analysis
BUILD-MAX-HEAP: O(n)
for loop: n - 1 times
exchange elements: O(1)
MAX-HEAPIFY: O(lg n)
26
Priority queues
Priority queues
27
Priority queues
A Priority queues:
28
Priority queues
Algorithm 4 HEAP-MAXIMUM(A)
1: return A[1]
Time: Θ(1).
29
Priority queues
30
Priority queues
Algorithm 5 HEAP-EXTRACT-MAX(A)
1: if A.heap − size < 1 then
2: error ”heap overflow”
3: end if
4: max = A[1]
5: A[1] = A[A.heap-size]
6: A.heap-size = A.heap-size - 1
7: MAX-HEAPIFY(A, 1)
8: return max
31
Priority queues
32
Priority queues
33
Priority queues
34
Priority queues
Insert a new node in the very last position in the tree with key
−∞.
35
Priority queues
36
Quicksort
Quicksort
Sorts in place.
37
Quicksort
Description of quicksort
38
Quicksort
Algorithm 8 QUICKSORT(A, p, r)
1: if p < r then
2: q = PARTITION(A, p, r)
3: QUICKSORT(A, p, q-1)
4: QUICKSORT(A, q+1, r)
5: end if
39
Quicksort
Partitioning
Algorithm 9 PARTITION(A, p, r)
1: x = A[r ]
2: i = p − 1
3: for j = p to r − 1 do
4: if A[j] ≤ x then
5: i =i +1
6: exchange A[i] with A[j]
7: end if
8: end for
9: exchange A[i + 1] with A[r ]
10: return i + 1
40
Quicksort
41
Quicksort
3 A[r ] = pivot.
42
Quicksort
Performance of quicksort
45
Performance of quicksort
Worst-case partitioning
Occurs when the subarrays are completely unbalanced.
Have 0 elements in one subarray and n - 1 elements in the
other subarray.
Thus we will get the recurrence
Best-case partitioning
47
Performance of quicksort
Balanced partitioning
48
Performance of quicksort
Every level of the tree has cost cn, until the recursion reaches
a boundary condition at depth log10 n = Θ(lg n).
50
Performance of quicksort
Figure: 2.8 Running time of two levels of a recursion tree for quicksort
(”worst partition” and ”best patition”) Vs. a single level of a recursion
tree that is very well balanced. 51
Performance of quicksort
Thus, the extra level in the left-hand figure only adds to the
constant hidden in the Θ - notation.
52
Performance of quicksort
53
Performance of quicksort
Algorithm 10 RANDOMIZED-PARTITION(A, p, r)
1: i = RANDOM(p, r)
2: exchange A[r ] with A[i]
3: return PARTITION(A, p, r)
54
Performance of quicksort
Algorithm 11 RANDOMIZED-QUICKSORT(A, p, r)
1: if p < r then
2: q = RANDOMIZED-PARTITION(A, p, r)
3: RANDOMIZED-QUICKSORT(A, p, q-1)
4: RANDOMIZED-QUICKSORT(A, q+1, r)
5: end if
55
Analysis of quicksort
Analysis of quicksort
We will analyze
56
Analysis of quicksort
Worst-case analysis
57
Analysis of quicksort
(q 2 + (n − q − 1)2 ) ≤ (n − 1)2
= n2 − 2n + 1
58
Analysis of quicksort
Therefore,
59
Analysis of quicksort
60
Analysis of quicksort
61
Analysis of quicksort
Define the set Zij = {zi , zi+1 , ..., zj } to be the set of elements
between zi and zj , inclusive.
62
Analysis of quicksort
63
Analysis of quicksort
Xn−1 X
n
E [X ] = E Xij
i=1 j=i+1
n−1
X n
X
= E [Xij ]
i=1 j=i+1
n−1
X n
X
= Pr {zi is compared to zj }.
i=1 j=i+1
64
Analysis of quicksort
Now all we have to do is find the probability that two elements are
compared.
65
Analysis of quicksort
66
Analysis of quicksort
Therefore,
67
Analysis of quicksort
Decision tree
70
Lower bounds for sorting
Figure: 2.9 The decision tree for insertion sort operating on three
elements.
71
Lower bounds for sorting
72
Lower bounds for sorting
73
Lower bounds for sorting
Theorem:
Any comparison sort algorithm requires Ω(n lgn) comparisons in
the worst case.
Proof: Consider a decision tree of height h with l reachable leaves
corresponding to a comparison sort on n elements.
Any binary tree of height h has ≤ 2h leaves.
Because each of the n! permutations of the input appears as
some leaf, we have n! ≤ l.
Thus,
n! ≤ l ≤ 2h
which, by taking logarithms, implies
h ≥ lg (n!) (then by using sterlings approximation : n! > (n/e)n )
= Ω(n lgn)
Heapsort and merge sort are asymptotically optimal comparison
sorts. 74
Counting sort
Counting sort
75
Counting sort
76
Counting sort
Algorithm 12 COUNTING-SORT(A, B, k)
1: let C [0 . . . k] be a new array
2: for i = 0 to k do
3: C [i] = 0
4: end for
5: for j = 1 to A.length do
6: C [A[j]] = C [A[j]] + 1
7: . C [i] now contains the number of elements equal to i.
8: end for
9: for i = 1 to k do
10: C [i] = C [i] + C [i − 1]
11: . C [i] contains elements less than or equal to i.
12: end for
13: for j = A.length downto 1 do
14: B[C [A[j]]] = A[j]
15: C [A[j]] = C [A[j]] − 1
16: end for 77
Counting sort
78
Counting sort
79
Radix sort
Radix sort
It’s the algorithm for using the machine that extends the
technique to multi-column sorting.
80
Radix sort
Algorithm 13 RADIX-SORT(A, d)
1: for i = 0 to d do
2: use a stable sort to sort array A on digit i
3: end for
Correctness:
82
Radix sort
d passes
83
Bucket sort
Bucket sort
84
Bucket sort
85
Bucket sort
Algorithm 14 BUCKET-SORT(A, n)
1: let B[0 . . . n − 1] be a new array
2: n = A.length
3: for i = 0 to n − 1 do
4: make B[i] an empty list
5: end for
6: for i = 1 to n do
7: insert A[i] into list B[bnA[i]c]
8: end for
9: for i = 0 to n − 1 do
10: sort list B[i] with insertion sort
11: end for
12: concatenate the lists B[0], B[1],. . . , B[n-1] together in order.
86
Bucket sort
87
Bucket sort
88
Bucket sort
89
Bucket sort
90
Bucket sort
1
Check the proof from the textbook 91
Bucket sort
Therefore:
n−1
X
E [T (n)] = Θ(n) + O(2 − (1/n))
i=0
= Θ(n) + O(n)
= Θ(n)
92
Introduction
93
Introduction
94
Introduction
95
Minimum and Maximum
This is the best we can do, because each element, except the
minimum, must be compared to a smaller element at least
once.
96
Minimum and Maximum
Algorithm 15 MINIMUM(A)
1: min = A[1]
2: for i = 2 to A.length do
3: if min > A[i] then
4: min = A[i]
5: end if
6: end for
7: return min
98
Minimum and Maximum
99
Minimum and Maximum
Setting up the initial values for the min and max depends on
whether n is odd or even.
If n is odd, set both min and max to the first element. Then
process the rest of the elements in pairs.
100
Minimum and Maximum
3(n − 2)
# of comparisons = +1
2
3n − 6
= +1
2
3n
= −2
2
If n is odd, we do 3(n − 1)/2 = 3bn/2c comparisons.
101
Selection
102
Selection
Algorithm 16 RANDOMIZED-SELECT(A, p, r, i)
1: if p == r then
2: return A[p]
3: end if
4: q = RANDOMIZED-PARTITION(A, p, r)
5: k = q - p + 1
6: if i == k then . the pivot value is the answer
7: return A[q]
8: else if i < k then
9: return RANDOMIZED-SELECT(A, p, q-1, i)
10: else
11: return RANDOMIZED-SELECT(A, q+1, r, i-k)
12: end if
103
Selection
104
Selection
105
Selection
106
Selection
End of Chapter 2
Questions?
107