cs717 - lecture 12
TRANSCRIPT
-
8/11/2019 CS717 - Lecture 12
1/122
Advanced Algorithm Analysis
CS-717Lecture No. 12
Muhammad Shahid
-
8/11/2019 CS717 - Lecture 12
2/122
-
8/11/2019 CS717 - Lecture 12
3/122
-
8/11/2019 CS717 - Lecture 12
4/122
-
8/11/2019 CS717 - Lecture 12
5/122
A RAM is an idealized machine with aninfinitely large random-access memory
Instructions are executed one-by-one
Each instruction involves performing some
basic operation on two values in themachines memory
Random Access Machine (RAM)
Advanced Algorithm Analysis (CS-717)5
-
8/11/2019 CS717 - Lecture 12
6/122
Basic operations include: assigning a value to a variable
computing any basic arithmetic operation (+,- ,,/)
on integer values of any size performing any comparison
accessing an element of an array
We assume that each basic operation takes
the same constant time to execute
Random Access Machine (RAM)
Advanced Algorithm Analysis (CS-717)6
-
8/11/2019 CS717 - Lecture 12
7/122
Let a point p in 2-dimensional space be givenby its integer coordinates, = (. , . )
A point p is said to be dominated by point q if. . and . .
Given a set of n points, = {, . . ., } in2-space a point is said to be maximal if it is
not dominated by any other point in
2-dimension maxima
Advanced Algorithm Analysis (CS-717)7
-
8/11/2019 CS717 - Lecture 12
8/122
Given a set of points = {, . . . , } in2-space, output the set of maximal points of
, i.e., those pointssuch that is not
dominated by any other point of
2-dimension maxima
Advanced Algorithm Analysis (CS-717)8
-
8/11/2019 CS717 - Lecture 12
9/122
Let = {, . . ., } be the initial set ofpoints
For each point , test it against all otherpoints
If is not dominated by any other point, thenoutput it
Brute-Force Algorithm
Advanced Algorithm Analysis (CS-717)9
-
8/11/2019 CS717 - Lecture 12
10/122
2-dimension maxima
Advanced Algorithm Analysis (CS-717)10
1, 8
2, 16
3, 14
4, 3
6, 10
8, 13
9, 8
11, 15
13, 13
14, 10
0
2
4
6
8
10
12
14
16
18
0 2 4 6 8 10 12 14 16
-
8/11/2019 CS717 - Lecture 12
11/122
Maximal(int n, Point p[1n])
1. fori = 1 to n
2. maximal = true
3. forj = 1 to n4. if(i != j) and (p[i].x
-
8/11/2019 CS717 - Lecture 12
12/122
Maximal(int n, Point p[1n])1. fori = 1 to n
2. maximal = true
3. forj = 1 to n4. if(i != j) and (p[i].x
-
8/11/2019 CS717 - Lecture 12
13/122
Thus we might express the worst-caserunning time as a pair of nested summations,
one for the i-loop and the other for thej-loop:
Analysis of the Brute-Force Maxima
Advanced Algorithm Analysis (CS-717)13
1 1
1
2
2
( ) (2 4)
( ) (4 2)
( ) (4 2)
( ) 4 2
Worst-case Running Time= (n )
n n
i j
n
i
T n
T n n
T n n n
T n n n
AsymptoticGrowth Rate
of the function
-
8/11/2019 CS717 - Lecture 12
14/122
We will sweep a vertical line across the planefrom left to right
As we sweep this line, we will build astructure holding the maximal points lying tothe left of the sweep line
When the sweep line reaches the rightmostpoint of P , then we will have constructed the
complete set of maxima This approach of solving geometric problems
by sweeping a line across the plane is called"Plane Sweep"
Plane-Sweep Algorithm
Advanced Algorithm Analysis (CS-717)14
-
8/11/2019 CS717 - Lecture 12
15/122
Sort the points in increasing order of their x-coordinates
For simplicity, let us assume that no twopoints have the same y-coordinate
Then we will advance the sweep-line frompoint to point in n discrete steps
As we encounter each new point, we will
update the current list of maximal points When the sweep line reaches the rightmost
point of P, we will have the complete set ofmaximal points
Plane-Sweep Algorithm
Advanced Algorithm Analysis (CS-717)15
-
8/11/2019 CS717 - Lecture 12
16/122
1, 8
2, 16
3, 14
4, 3
6, 10
8, 13
9, 8
11, 15
13, 13
14, 10
0
2
4
6
8
10
12
14
16
18
0 5 10 15
Plane-Sweep Algorithm
Advanced Algorithm Analysis (CS-717)16
-
8/11/2019 CS717 - Lecture 12
17/122
1, 8
2, 16
3, 14
4, 3
6, 10
8, 13
9, 8
11, 15
13, 13
14, 10
0
2
4
6
8
10
12
14
16
18
0 5 10 15
Plane-Sweep Algorithm
Advanced Algorithm Analysis (CS-717)17
1, 8
-
8/11/2019 CS717 - Lecture 12
18/122
1, 8
2, 16
3, 14
4, 3
6, 10
8, 13
9, 8
11, 15
13, 13
14, 10
0
2
4
6
8
10
12
14
16
18
0 5 10 15
Plane-Sweep Algorithm
Advanced Algorithm Analysis (CS-717)18
1, 8
-
8/11/2019 CS717 - Lecture 12
19/122
1, 8
2, 16
3, 14
4, 3
6, 10
8, 13
9, 8
11, 15
13, 13
14, 10
0
2
4
6
8
10
12
14
16
18
0 5 10 15
Plane-Sweep Algorithm
Advanced Algorithm Analysis (CS-717)19
2, 16
-
8/11/2019 CS717 - Lecture 12
20/122
1, 8
2, 16
3, 14
4, 3
6, 10
8, 13
9, 8
11, 15
13, 13
14, 10
0
2
4
6
8
10
12
14
16
18
0 5 10 15
Plane-Sweep Algorithm
Advanced Algorithm Analysis (CS-717)20
2, 16
3, 14
-
8/11/2019 CS717 - Lecture 12
21/122
1, 8
2, 16
3, 14
4, 3
6, 10
8, 13
9, 8
11, 15
13, 13
14, 10
0
2
4
6
8
10
12
14
16
18
0 5 10 15
Plane-Sweep Algorithm
Advanced Algorithm Analysis (CS-717)21
2, 16
3, 14
4, 3
-
8/11/2019 CS717 - Lecture 12
22/122
1, 8
2, 16
3, 14
4, 3
6, 10
8, 13
9, 8
11, 15
13, 13
14, 10
0
2
4
6
8
10
12
14
16
18
0 5 10 15
Plane-Sweep Algorithm
Advanced Algorithm Analysis (CS-717)22
2, 16
3, 14
6, 10
-
8/11/2019 CS717 - Lecture 12
23/122
1, 8
2, 16
3, 14
4, 3
6, 10
8, 13
9, 8
11, 15
13, 13
14, 10
0
2
4
6
8
10
12
14
16
18
0 5 10 15
Plane-Sweep Algorithm
Advanced Algorithm Analysis (CS-717)23
2, 16
3, 14
8, 13
-
8/11/2019 CS717 - Lecture 12
24/122
1, 8
2, 16
3, 14
4, 3
6, 10
8, 13
9, 8
11, 15
13, 13
14, 10
0
2
4
6
8
10
12
14
16
18
0 5 10 15
Plane-Sweep Algorithm
Advanced Algorithm Analysis (CS-717)24
2, 16
3, 14
8, 139, 8
-
8/11/2019 CS717 - Lecture 12
25/122
1, 8
2, 16
3, 14
4, 3
6, 10
8, 13
9, 8
11, 15
13, 13
14, 10
0
2
4
6
8
10
12
14
16
18
0 5 10 15
Plane-Sweep Algorithm
Advanced Algorithm Analysis (CS-717)25
2, 16
11, 15
-
8/11/2019 CS717 - Lecture 12
26/122
1, 8
2, 16
3, 14
4, 3
6, 10
8, 13
9, 8
11, 15
13, 13
14, 10
0
2
4
6
8
10
12
14
16
18
0 5 10 15
Plane-Sweep Algorithm
Advanced Algorithm Analysis (CS-717)26
2, 16
11, 15
13, 1314, 10
-
8/11/2019 CS717 - Lecture 12
27/122
1, 8
2, 16
3, 14
4, 3
6, 10
8, 13
9, 8
11, 15
13, 13
14, 10
0
2
4
6
8
10
12
14
16
18
0 5 10 15
Plane-Sweep Algorithm
Advanced Algorithm Analysis (CS-717)27
11, 15
13, 13
14, 10
-
8/11/2019 CS717 - Lecture 12
28/122
PLANE-SWEEP-MAXIMA(n, P[1..n])1. Sort P in increasing order by x;
2. stack stk;
3. fori 1 to n
4. do
5. while (!stk.Empty() && stk.Top().y P[i].y)
6. do stk.Pop();
7. stk.Push(P[i]);
8. output the contents of stack stk;
Plane-Sweep Algorithm
Advanced Algorithm Analysis (CS-717)28
-
8/11/2019 CS717 - Lecture 12
29/122
We pop an element off the stack each timewe go through the inner while-loop
The for-loop iterates n times and the inner
while-loop also iterates n time for a total of(n)
Combined with the sorting, the runtime of
entire plane-sweep algorithm is ()
Analysis of Plane-Sweep Algorithm
Advanced Algorithm Analysis (CS-717)29
-
8/11/2019 CS717 - Lecture 12
30/122
notation: asymptotic "equality": f(n)= (g(n)) implies: f(n) "=" g(n)
O notation(Big-O): asymptotic "less than": f(n)=O(g(n)) implies: f(n) " "g(n)
notation: asymptotic "greater than": f(n)= (g(n)) implies: f(n) "" g(n)
Asymptotic Notation
Advanced Algorithm Analysis (CS-717)30
-
8/11/2019 CS717 - Lecture 12
31/122
-notation
Asymptotic notations (cont.)
Advanced Algorithm Analysis (CS-717)31
(g(n)) is the set of functions
with the same order of growth
as g(n)
-
8/11/2019 CS717 - Lecture 12
32/122
Example
Advanced Algorithm Analysis (CS-717)32
3 2 3f(n) = 10n + 5n + 17 (n )
3 3
3 3
1
2
0
10 ( ) (10 5 17)
10 ( ) 32
c 10
321
f n n
f n n
cn
-
8/11/2019 CS717 - Lecture 12
33/122
Example
Advanced Algorithm Analysis (CS-717)33
3 3f(n)=10n log ( )n n n
3 3
1 2 0
10 ( ) 11
c 10 11 1
f n n
c n
-
8/11/2019 CS717 - Lecture 12
34/122
O-notation:
Asymptotic notations
Advanced Algorithm Analysis (CS-717)34
( )
-
8/11/2019 CS717 - Lecture 12
35/122
- notation
Asymptotic notations (cont.)
Advanced Algorithm Analysis (CS-717)35
(g(n)) is the set of functions
with larger or same order of
growth as g(n)
-
8/11/2019 CS717 - Lecture 12
36/122
M S t
-
8/11/2019 CS717 - Lecture 12
37/122
Merge Sort
Advanced Algorithm Analysis (CS-717)37
8 6 3 5 2 9 4 1
8 6 3 5 2 9 4 1
2 9 4 1
8 6 3 5 2 9 4 1
3 58 6
M S t
-
8/11/2019 CS717 - Lecture 12
38/122
Merge Sort
Advanced Algorithm Analysis (CS-717)38
1 2 3 4 5 6 8 9
3 5 6 8
1 2 4 9
2 9 1 4
8 6 3 5 2 9 4 1
3 56 8
Al ith M S t
-
8/11/2019 CS717 - Lecture 12
39/122
Merge-sort(array A, int p, int r)1. if(p
-
8/11/2019 CS717 - Lecture 12
40/122
Algorithm Merge-Sort
Advanced Algorithm Analysis (CS-717)40
1. Merge(array A, int p, int q,int r)
2. int B[pr];3. int i =k =p;
4. intj =q+1
5. while(i
-
8/11/2019 CS717 - Lecture 12
41/122
Recurrence Relation
Advanced Algorithm Analysis (CS-717)41
1 1( )
( / 2 ( / 2 ( )
if nT n
T n T n n otherwise
This kind of equation is called RecurrenceEquation orRecurrence
R R l ti
-
8/11/2019 CS717 - Lecture 12
42/122
Recurrence Relation
Advanced Algorithm Analysis (CS-717)42
( ) ( / 2 ( / 2 ( )
(1) 1
(2) (1) (1) 2 1 1 2 4
(3) (2) (1) 3 4 1 3 8
(4) (2) (2) 4 4 4 4 12
(5) (3) (2) 5 8 4 5 17
(6) (3) (3) 6 8 8 6 22
(7) (4) (3) 7 12 8 7 27
(8) (4) (4) 8
T n T n T n n
T
T T T
T T T
T T T
T T T
T T T
T T T
T T T
12 12 8 32
-
8/11/2019 CS717 - Lecture 12
43/122
-
8/11/2019 CS717 - Lecture 12
44/122
-
8/11/2019 CS717 - Lecture 12
45/122
-
8/11/2019 CS717 - Lecture 12
46/122
The Iteration Method
-
8/11/2019 CS717 - Lecture 12
47/122
If n is a power of 2 then let n = 2k
or k = log n
The Iteration Method
Advanced Algorithm Analysis (CS-717)47
k
k
k
k
logn
logn
logn
nT(n) = 2 T( ) ( )
2
nT(n) = 2 T( )2
nT(n) = 2 T( ) (logn)
2
nT(n) = 2 T( ) (logn)n
T(n) = nT(1) (logn)
T(n) = n (logn)
n n n n
kn
n
n
n
n
Recurrence Tree
-
8/11/2019 CS717 - Lecture 12
48/122
Recurrence Tree
Advanced Algorithm Analysis (CS-717)48
n
n/8
n/2 n/2
n/4 n/4
n/8 n/8 n/8 n/8
n/4 n/4
n/8 n/8 n/8
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Time to Merge
2(n/2) = n
= n
+
+
4(n/4) = n
+
8(n/8) = n
+n(n/n) = n
Recurrence Tree
-
8/11/2019 CS717 - Lecture 12
49/122
Recurrence Tree
Advanced Algorithm Analysis (CS-717)49
n
n/8
n/2 n/2
n/4 n/4
n/8 n/8 n/8 n/8
n/4 n/4
n/8 n/8 n/8
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Time to Merge
2(n/2) = n
= n
+
+
4(n/4) = n
+
8(n/8) = n
+n(n/n) = n
log(n)+1
levels
n(log(n)+1)
-
8/11/2019 CS717 - Lecture 12
50/122
SORTING
Advanced Algorithm Analysis (CS-717)50
Elementary Sorting Techniques
-
8/11/2019 CS717 - Lecture 12
51/122
Bubble Sort
Elementary Sorting Techniques
Advanced Algorithm Analysis (CS-717)51
Scan the array
Whenever two consecutive items are found
that are out of order, swap them Repeat until all consecutive items are in order
Elementary Sorting Techniques
-
8/11/2019 CS717 - Lecture 12
52/122
Insertion Sort
Elementary Sorting Techniques
Advanced Algorithm Analysis (CS-717)52
Assume that A[1...i 1] have already been
sorted
Insert A[i] into its proper position in this subarray
Create this position by shifting all larger
elements to the right
Elementary Sorting Techniques
-
8/11/2019 CS717 - Lecture 12
53/122
Selection Sort
Elementary Sorting Techniques
Advanced Algorithm Analysis (CS-717)53
Assume that A[1...i 1] contain the i 1
smallest elements in sorted order
Find the smallest element in A[i...n] Swap it with A[i]
Heap
-
8/11/2019 CS717 - Lecture 12
54/122
A heap is a left-complete binary tree thatconforms to the heap order
The heap order property: in a (min) heap, forevery node X, the key in the parent is smallerthan or equal to the key in X
In other words, the parent node has keysmaller than or equal to both of its childrennodes
Heap
Advanced Algorithm Analysis (CS-717)54
Heap
-
8/11/2019 CS717 - Lecture 12
55/122
Similarly, in a max heap, the parent has a keylarger than or equal both of its children
Thus the smallest key is in the root in a minheap; in the max heap, the largest is in the
root
Heap
Advanced Algorithm Analysis (CS-717)55
Heap
-
8/11/2019 CS717 - Lecture 12
56/122
Heap
Advanced Algorithm Analysis (CS-717)56
16
378
1014
9
142
16 14 10 8 7 9 3 2 4 1
1 2 3 4 5 6 7 8 9 10
1
2 3
4 5 6 7
8 9 10
Basic Functions
-
8/11/2019 CS717 - Lecture 12
57/122
Access to nodes involves simple arithmeticoperations:
parent(i): returns /2 , the parent of . Theroot is at position 1 of the array
left(i): returns 2, index of left child of node
right(i): returns 2 + 1, the right child
Basic Functions
Advanced Algorithm Analysis (CS-717)57
Heapsort Algorithm
-
8/11/2019 CS717 - Lecture 12
58/122
We build a max heap out of the given array ofnumbers A[1..n]
We repeatedly extract the maximum item fromthe heap
Once the max item is removed, we are left with ahole at the root
To fix this, we will replace it with the last leaf intree
But now the heap order will very likely bedestroyed
We will apply a heapify procedure to the root torestore the heap
Heapsort Algorithm
Advanced Algorithm Analysis (CS-717)58
Heap Sort
-
8/11/2019 CS717 - Lecture 12
59/122
HEAPSORT( array A, int n)1. BUILD-HEAP(A, n)
2. m n
3. while (m 2)4. do SWAP(A[1],A[m])
5. m m 1
6. HEAPIFY(A, 1,m)
Heap Sort
Advanced Algorithm Analysis (CS-717)59
Heapsort Trace
-
8/11/2019 CS717 - Lecture 12
60/122
Heapsort Trace
Advanced Algorithm Analysis (CS-717)60
87
12
4457
15 19 23
87 57 44 12 15 19 23
1 2 3 4 5 6 7
Heapsort Trace
-
8/11/2019 CS717 - Lecture 12
61/122
Heapsort Trace
Advanced Algorithm Analysis (CS-717)61
23
12
4457
15 19 87
23 57 44 12 15 19 87
1 2 3 4 5 6 7
sorted
-
8/11/2019 CS717 - Lecture 12
62/122
Heapsort Trace
-
8/11/2019 CS717 - Lecture 12
63/122
Heapsort Trace
Advanced Algorithm Analysis (CS-717)63
23
12
4457
15 19
23 57 44 12 15 19 87
1 2 3 4 5 6 7
sorted
Heapsort Trace
-
8/11/2019 CS717 - Lecture 12
64/122
Heapsort Trace
Advanced Algorithm Analysis (CS-717)64
57
12
4423
15 19
57 23 44 12 15 19 87
1 2 3 4 5 6 7
sorted
Heapsort Trace
-
8/11/2019 CS717 - Lecture 12
65/122
Heapsort Trace
Advanced Algorithm Analysis (CS-717)65
19
12
4423
15 57
19 23 44 12 15 57 87
1 2 3 4 5 6 7
sorted
Heapsort Trace
-
8/11/2019 CS717 - Lecture 12
66/122
Heapsort Trace
Advanced Algorithm Analysis (CS-717)66
19
12
4423
15
19 23 44 12 15 57 87
1 2 3 4 5 6 7
sorted
heap violated
Heapsort Trace
-
8/11/2019 CS717 - Lecture 12
67/122
Heapsort Trace
Advanced Algorithm Analysis (CS-717)67
19
12
4423
15
19 23 44 12 15 57 87
1 2 3 4 5 6 7
sorted
Heapsort Trace
-
8/11/2019 CS717 - Lecture 12
68/122
Heapsort Trace
Advanced Algorithm Analysis (CS-717)68
44
12
1923
15
44 23 19 12 15 57 87
1 2 3 4 5 6 7
sorted
Heapsort Trace
-
8/11/2019 CS717 - Lecture 12
69/122
Heapsort Trace
Advanced Algorithm Analysis (CS-717)69
15
12
1923
15 23 19 12 44 57 87
1 2 3 4 5 6 7
sorted
heap violated
Heapsort Trace
-
8/11/2019 CS717 - Lecture 12
70/122
Heapsort Trace
Advanced Algorithm Analysis (CS-717)70
15
12
1923
15 23 19 12 44 57 87
1 2 3 4 5 6 7
sorted
Heapsort Trace
-
8/11/2019 CS717 - Lecture 12
71/122
Heapsort Trace
Advanced Algorithm Analysis (CS-717)71
23
12
1915
23 15 19 12 44 57 87
1 2 3 4 5 6 7
sorted
Heapsort Trace
-
8/11/2019 CS717 - Lecture 12
72/122
Heapsort Trace
Advanced Algorithm Analysis (CS-717)72
12
1915
12 15 19 23 44 57 87
1 2 3 4 5 6 7
sorted
heap violated
-
8/11/2019 CS717 - Lecture 12
73/122
Heapsort Trace
-
8/11/2019 CS717 - Lecture 12
74/122
Heapsort Trace
Advanced Algorithm Analysis (CS-717)74
19
1215
19 15 12 23 44 57 87
1 2 3 4 5 6 7
sorted
Heapsort Trace
-
8/11/2019 CS717 - Lecture 12
75/122
Heapsort Trace
Advanced Algorithm Analysis (CS-717)75
19
1215
19 15 12 23 44 57 87
1 2 3 4 5 6 7
sorted
Heapsort Trace
-
8/11/2019 CS717 - Lecture 12
76/122
Heapsort Trace
Advanced Algorithm Analysis (CS-717)76
12
15
12 15 19 23 44 57 87
1 2 3 4 5 6 7
sorted
heap violated
Heapsort Trace
-
8/11/2019 CS717 - Lecture 12
77/122
Heapsort Trace
Advanced Algorithm Analysis (CS-717)77
12
15
12 15 19 23 44 57 87
1 2 3 4 5 6 7
sorted
Heapsort Trace
-
8/11/2019 CS717 - Lecture 12
78/122
eapso ace
Advanced Algorithm Analysis (CS-717)78
15
12
15 12 19 23 44 57 87
1 2 3 4 5 6 7
sorted
Heapsort Trace
-
8/11/2019 CS717 - Lecture 12
79/122
p
Advanced Algorithm Analysis (CS-717)79
12
12 15 19 23 44 57 87
1 2 3 4 5 6 7
sorted
Heapsort Trace
-
8/11/2019 CS717 - Lecture 12
80/122
p
Advanced Algorithm Analysis (CS-717)80
12 15 19 23 44 57 87
1 2 3 4 5 6 7
sorted
Analysis of Heapsort
-
8/11/2019 CS717 - Lecture 12
81/122
Heapify: The total time for heapify is O(log n)
BuildHeap:
BuildHeap takes (n) time
Heapsort:
Heapsort calls BuildHeap once. This takes (n)
Heapsort then extracts roughly n maximumelements from the heap
Each extract requires a constant amount ofwork (swap) and O(log n) heapify
Heapsort is thus O(n log n)
y p
Advanced Algorithm Analysis (CS-717)81
-
8/11/2019 CS717 - Lecture 12
82/122
Quicksort
Advanced Algorithm Analysis (CS-717)82
Quicksort
-
8/11/2019 CS717 - Lecture 12
83/122
QUICKSORT( array A, int p, int r)1. if(r > p)
2. then
3. i a random index from [p...r]4. swap A[i] with A[p]
5. q PARTITION(A, p, r)
6. QUICKSORT(A, p, q 1)7. QUICKSORT(A, q + 1, r)
Advanced Algorithm Analysis (CS-717)83
Partition Algorithm
-
8/11/2019 CS717 - Lecture 12
84/122
PARTITION( array A, int p, int r)1. x A[p]
2. q p
3. fors p + 1 to r4. do if(A[s] < x)
5. then q q + 1
6. swap A[q] with A[s]7. swap A[p] with A[q]
8. return q
g
Advanced Algorithm Analysis (CS-717)84
Partition Algorithm
-
8/11/2019 CS717 - Lecture 12
85/122
g
Advanced Algorithm Analysis (CS-717)85
7 5 10 8 6 9 5 3
p
q
r
s
7 5 10 8 6 9 5 3p
q
r
s
7 5 10 8 6 9 5 3
p
q
r
s
-
8/11/2019 CS717 - Lecture 12
86/122
Partition Algorithm
-
8/11/2019 CS717 - Lecture 12
87/122
g
Advanced Algorithm Analysis (CS-717)87
7 5 6 5 10 9 8 3
p
q
r
s
7 5 6 5 3 9 8 10p
q
r
s
3 5 6 5 7 9 8 10
p
q
r
s
Analysis of Quicksort
-
8/11/2019 CS717 - Lecture 12
88/122
The running time of quicksort dependsheavily on the selection of the pivot
If the rank of the pivot is very large or very
small then the partition (BST) will be
unbalanced
Since the pivot is chosen randomly in our
algorithm, the expected running time is
( log )
The average case, quicksort runs in
( log ) time
y
Advanced Algorithm Analysis (CS-717)88
-
8/11/2019 CS717 - Lecture 12
89/122
Counting Sort
Advanced Algorithm Analysis (CS-717)89
Counting Sort
-
8/11/2019 CS717 - Lecture 12
90/122
CountingSort(A, B, n, k)1. for i 0 to k
2. do C[i] 0
3. forj 1 to n
4. do C[A[j]] C[A[j]] +15. for i 1 to k
6. do C[i] C[i] + C[i-1]
7. forj
n downto 18. B[ C[A[j]] ] A[j]
9. C[ A[j] ] C[ A[j] ] -1
Advanced Algorithm Analysis (CS-717)90
-
8/11/2019 CS717 - Lecture 12
91/122
Counting Sort
-
8/11/2019 CS717 - Lecture 12
92/122
Advanced Algorithm Analysis (CS-717)92
7 1 3 1 2 4 5 7 2 4 3AInput
A[1n]
0 0 0 0 0 0 1
1 2 3 4 5 6 7
C[1k]
1 2 3 4 5 6 7 8 9 10 11
k
Counting Sort
-
8/11/2019 CS717 - Lecture 12
93/122
Advanced Algorithm Analysis (CS-717)93
7 1 3 1 2 4 5 7 2 4 3AInput
A[1n]
1 0 0 0 0 0 1
1 2 3 4 5 6 7
C[1k]
1 2 3 4 5 6 7 8 9 10 11
k
Counting Sort
-
8/11/2019 CS717 - Lecture 12
94/122
Advanced Algorithm Analysis (CS-717)94
7 1 3 1 2 4 5 7 2 4 3AInput
A[1n]
1 0 1 0 0 0 1
1 2 3 4 5 6 7
C[1k]
1 2 3 4 5 6 7 8 9 10 11
k
Counting Sort
-
8/11/2019 CS717 - Lecture 12
95/122
Advanced Algorithm Analysis (CS-717)95
7 1 3 1 2 4 5 7 2 4 3AInput
A[1n]
2 0 1 0 0 0 1
1 2 3 4 5 6 7
C[1k]
1 2 3 4 5 6 7 8 9 10 11
k
Counting Sort
-
8/11/2019 CS717 - Lecture 12
96/122
Advanced Algorithm Analysis (CS-717)96
7 1 3 1 2 4 5 7 2 4 3AInput
A[1n]
2 1 1 0 0 0 1
1 2 3 4 5 6 7
C[1k]
1 2 3 4 5 6 7 8 9 10 11
k
Counting Sort
-
8/11/2019 CS717 - Lecture 12
97/122
Advanced Algorithm Analysis (CS-717)97
7 1 3 1 2 4 5 7 2 4 3AInput
A[1n]
2 1 1 1 0 0 1
1 2 3 4 5 6 7
C[1k]
1 2 3 4 5 6 7 8 9 10 11
k
Counting Sort
-
8/11/2019 CS717 - Lecture 12
98/122
Advanced Algorithm Analysis (CS-717)98
7 1 3 1 2 4 5 7 2 4 3AInput
A[1n]
2 1 1 1 1 0 1
1 2 3 4 5 6 7
C[1k]
1 2 3 4 5 6 7 8 9 10 11
k
Counting Sort
-
8/11/2019 CS717 - Lecture 12
99/122
Advanced Algorithm Analysis (CS-717)99
7 1 3 1 2 4 5 7 2 4 3AInput
A[1n]
2 1 1 1 1 0 2
1 2 3 4 5 6 7
C[1k]
1 2 3 4 5 6 7 8 9 10 11
k
Counting Sort
-
8/11/2019 CS717 - Lecture 12
100/122
Advanced Algorithm Analysis (CS-717)100
7 1 3 1 2 4 5 7 2 4 3AInput
A[1n]
2 2 1 1 1 0 2
1 2 3 4 5 6 7
C[1k]
1 2 3 4 5 6 7 8 9 10 11
k
Counting Sort
-
8/11/2019 CS717 - Lecture 12
101/122
Advanced Algorithm Analysis (CS-717)101
7 1 3 1 2 4 5 7 2 4 3AInput
A[1n]
2 2 1 2 1 0 2
1 2 3 4 5 6 7
C[1k]
1 2 3 4 5 6 7 8 9 10 11
k
-
8/11/2019 CS717 - Lecture 12
102/122
Counting Sort
-
8/11/2019 CS717 - Lecture 12
103/122
Advanced Algorithm Analysis (CS-717)103
7 1 3 1 2 4 5 7 2 4 3AInput
A[1n]
2 2 2 2 1 0 2
1 2 3 4 5 6 7
C[1k]
1 2 3 4 5 6 7 8 9 10 11
2 4 6 8 9 9 11
1 2 3 4 5 6 7
C
fori = 2 to 7
do C[i] = C[i] + C[i-1]
Counting Sort
-
8/11/2019 CS717 - Lecture 12
104/122
Advanced Algorithm Analysis (CS-717)104
7 1 3 1 2 4 5 7 2 4 3AInput
A[1n]
2 4 6 8 9 9 11
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8 9 10 11
2 4 5 8 9 9 11
1 2 3 4 5 6 7
C
A
Output
B[1n]
1 2 3 4 5 6 7 8 9 10 11
C
3
Counting Sort
-
8/11/2019 CS717 - Lecture 12
105/122
Advanced Algorithm Analysis (CS-717)105
7 1 3 1 2 4 5 7 2 4 3AInput
A[1n]
2 4 5 8 9 9 11
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8 9 10 11
2 4 5 7 9 9 11
1 2 3 4 5 6 7
C
A
Output
B[1n]
1 2 3 4 5 6 7 8 9 10 11
C
3 4
Counting Sort
-
8/11/2019 CS717 - Lecture 12
106/122
Advanced Algorithm Analysis (CS-717)106
7 1 3 1 2 4 5 7 2 4 3AInput
A[1n]
2 4 5 7 9 9 11
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8 9 10 11
2 3 5 7 9 9 11
1 2 3 4 5 6 7
C
A
Output
B[1n]
1 2 3 4 5 6 7 8 9 10 11
C
3 42
Counting Sort
-
8/11/2019 CS717 - Lecture 12
107/122
Advanced Algorithm Analysis (CS-717)107
7 1 3 1 2 4 5 7 2 4 3AInput
A[1n]
2 3 5 7 9 9 11
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8 9 10 11
2 3 5 7 9 9 10
1 2 3 4 5 6 7
C
A
Output
B[1n]
1 2 3 4 5 6 7 8 9 10 11
C
3 42 7
Counting Sort
-
8/11/2019 CS717 - Lecture 12
108/122
Advanced Algorithm Analysis (CS-717)108
7 1 3 1 2 4 5 7 2 4 3AInput
A[1n]
2 3 5 7 9 9 10
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8 9 10 11
2 3 5 7 8 9 10
1 2 3 4 5 6 7
C
A
Output
B[1n]
1 2 3 4 5 6 7 8 9 10 11
C
3 42 75
Counting Sort
-
8/11/2019 CS717 - Lecture 12
109/122
Advanced Algorithm Analysis (CS-717)109
7 1 3 1 2 4 5 7 2 4 3AInput
A[1n]
2 3 5 7 8 9 10
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8 9 10 11
2 3 5 6 8 9 10
1 2 3 4 5 6 7
C
A
Output
B[1n]
1 2 3 4 5 6 7 8 9 10 11
C
3 42 754
-
8/11/2019 CS717 - Lecture 12
110/122
Counting Sort
-
8/11/2019 CS717 - Lecture 12
111/122
Advanced Algorithm Analysis (CS-717)111
7 1 3 1 2 4 5 7 2 4 3AInput
A[1n]
2 2 5 6 8 9 10
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8 9 10 11
1 2 5 6 8 9 10
1 2 3 4 5 6 7
C
A
Output
B[1n]
1 2 3 4 5 6 7 8 9 10 11
C
3 42 75421
Counting Sort
-
8/11/2019 CS717 - Lecture 12
112/122
Advanced Algorithm Analysis (CS-717)112
7 1 3 1 2 4 5 7 2 4 3AInput
A[1n]
1 2 5 6 8 9 10
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8 9 10 11
1 2 4 6 8 9 10
1 2 3 4 5 6 7
C
A
Output
B[1n]
1 2 3 4 5 6 7 8 9 10 11
C
3 42 7542 31
Counting Sort
-
8/11/2019 CS717 - Lecture 12
113/122
Advanced Algorithm Analysis (CS-717)113
7 1 3 1 2 4 5 7 2 4 3AInput
A[1n]
1 2 4 6 8 9 10
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8 9 10 11
0 2 4 6 8 9 10
1 2 3 4 5 6 7
C
A
Output
B[1n]
1 2 3 4 5 6 7 8 9 10 11
C
3 42 7542 31 1
Counting Sort
-
8/11/2019 CS717 - Lecture 12
114/122
Advanced Algorithm Analysis (CS-717)114
7 1 3 1 2 4 5 7 2 4 3AInput
A[1n]
0 2 4 6 8 9 10
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8 9 10 11
0 2 4 6 8 9 9
1 2 3 4 5 6 7
C
A
Output
B[1n]
1 2 3 4 5 6 7 8 9 10 11
C
3 42 7542 31 71
Analysis of Counting Sort
-
8/11/2019 CS717 - Lecture 12
115/122
CountingSort(A, B, n, k)
1. for i 0 to k
2. do C[i] 0
3. forj 1 to n
4. do C[A[j]] C[A[j]] +15. for i 1 to k
6. do C[i] C[i] + C[i-1]
7. forj n downto 1
8. B[ C[A[j]] ] A[j]
9. C[ A[j] ] C[ A[j] ] -1
Advanced Algorithm Analysis (CS-717)115
(k)
(n)
(k)
(n)
-
8/11/2019 CS717 - Lecture 12
116/122
Bucket Sort
Advanced Algorithm Analysis (CS-717)116
-
8/11/2019 CS717 - Lecture 12
117/122
Bucket Sort
-
8/11/2019 CS717 - Lecture 12
118/122
Advanced Algorithm Analysis (CS-717)118
.78
.17
.39
.26
.72
.94
.21
.23
.68
/
.72
.94
/
//
.12
0
1
2
3
4
6
7
8
5
9
.12 .17 /
.21 .23 .26 /
.39 /
.68 /
/.78.72
.94 /
Step 2:concatenate
the lists
Bucket Sort
-
8/11/2019 CS717 - Lecture 12
119/122
Advanced Algorithm Analysis (CS-717)119
.78
.17
.39
.26
.72
.94
.21
.23
.68
.12
.12 .17 /
.21 .23 .26 /
.39 /
.68 /
/.78.72
.94 /
Step 2:concatenate
the lists
-
8/11/2019 CS717 - Lecture 12
120/122
Radix Sort
Advanced Algorithm Analysis (CS-717)120
Radix SortSTEP 1 STEP 2 STEP 3
-
8/11/2019 CS717 - Lecture 12
121/122
Advanced Algorithm Analysis (CS-717)121
576
494
194
296
278
176
954
49[4]
19[4]
95[4]
57[6]
29[6]
17[6]
27[8]
9[5]4
5[7]6
1[7]6
2[7]8
4[9]4
1[9]4
2[9]6
[1]76
[1]94
[2]78
[2]96
[4]94
[5]76
[9]54
176
194
278
296
494
576
954
STEP 1 STEP 2 STEP 3
-
8/11/2019 CS717 - Lecture 12
122/122