You are on page 1of 122

Advanced Algorithm Analysis

CS-717
Lecture No. 12
Muhammad Shahid
shahid.abdullah@hotmail.com
Bucket Sort
Radix Sort
Dynamic Programming
Last Lecture Review
Advanced Algorithm Analysis (CS-717) 2
Revision from Lecture 01 Lecture 11
What Will You Learn Today?
Advanced Algorithm Analysis (CS-717) 3
We will measure algorithm in terms of the
amount of computation recourses that
algorithm requires:
Running time
Memory used
We should be agree on some criterion
Analyzing Algorithms
Advanced Algorithm Analysis (CS-717) 4
A RAM is an idealized machine with an
infinitely large random-access memory
Instructions are executed one-by-one
Each instruction involves performing some
basic operation on two values in the
machines memory
Random Access Machine (RAM)
Advanced Algorithm Analysis (CS-717) 5
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
Let a point p in 2-dimensional space be given
by its integer coordinates, = (. , . )
A point p is said to be dominated by point q if
. . and . .
Given a set of n points, = {
1
,
2
. . .,

} in
2-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
Given a set of points = {
1
,
2
. . . ,

} in
2-space, output the set of maximal points of
, i.e., those points

such that

is not
dominated by any other point of
2-dimension maxima
Advanced Algorithm Analysis (CS-717) 8
Let = {
1
,
2
. . .,

} be the initial set of


points
For each point

, test it against all other


points

If

is not dominated by any other point, then


output it
Brute-Force Algorithm
Advanced Algorithm Analysis (CS-717) 9
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
Maximal(int n, Point p[1n])
1. for i = 1 to n
2. maximal = true
3. for j = 1 to n
4. if (i != j) and (p[i].x <= p[j].x) and (p[i].y <= p[j].y)
5. maximal = false;
6. break;
7. if (maximal == true)
8. then output p[i].x, p[i].y
Brute-Force Algorithm
Advanced Algorithm Analysis (CS-717) 11
Maximal(int n, Point p[1n])
1. for i = 1 to n
2. maximal = true
3. for j = 1 to n
4. if (i != j) and (p[i].x <= p[j].x) and (p[i].y <= p[j].y)
5. maximal = false;
6. break;
7. if (maximal == true)
8. then output p[i].x, p[i].y
Analysis of the Brute-Force Maxima
Advanced Algorithm Analysis (CS-717) 12
n times
n times
4 accesses
2 accesses
Thus we might express the worst-case
running time as a pair of nested summations,
one for the i-loop and the other for the j-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
= =
=
= +
= +
= +
= +
O

Asymptotic
Growth Rate
of the function
We will sweep a vertical line across the plane
from left to right
As we sweep this line, we will build a
structure holding the maximal points lying to
the left of the sweep line
When the sweep line reaches the rightmost
point 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
Sort the points in increasing order of their x-
coordinates
For simplicity, let us assume that no two
points have the same y-coordinate
Then we will advance the sweep-line from
point 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 of
maximal points
Plane-Sweep Algorithm
Advanced Algorithm Analysis (CS-717) 15
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
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
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
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
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
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
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
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
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, 13
9, 8
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
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, 13
14, 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 5 10 15
Plane-Sweep Algorithm
Advanced Algorithm Analysis (CS-717) 27
11, 15
13, 13
14, 10
PLANE-SWEEP-MAXIMA(n, P[1..n])
1. Sort P in increasing order by x;
2. stack stk;
3. for i 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
We pop an element off the stack each time
we 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
O notation: asymptotic "equality":
f(n)= O(g(n)) implies: f(n) "=" g(n)
O notation(Big-O): asymptotic "less than":
f(n)=O(g(n)) implies: f(n) " "g(n)
O notation: asymptotic "greater than":
f(n)= O(g(n)) implies: f(n) "" g(n)
Asymptotic Notation
Advanced Algorithm Analysis (CS-717) 30
O-notation
Asymptotic notations (cont.)
Advanced Algorithm Analysis (CS-717) 31
O(g(n)) is the set of functions
with the same order of growth
as g(n)
Example
Advanced Algorithm Analysis (CS-717) 32
3 2 3
f(n) = 10n + 5n + 17 (n ) e O
3 3
3 3
1
2
0
10 ( ) (10 5 17)
10 ( ) 32
c 10
32
1
f n n
f n n
c
n
s s + +
s s
=
=
=
Example
Advanced Algorithm Analysis (CS-717) 33
3 3
f(n)=10n log ( ) n n n + eO
3 3
1 2 0
10 ( ) 11
c 10 11 1
f n n
c n
s s
= = =
O-notation:
Asymptotic notations
Advanced Algorithm Analysis (CS-717) 34
O - notation
Asymptotic notations (cont.)
Advanced Algorithm Analysis (CS-717) 35
O(g(n)) is the set of functions
with larger or same order of
growth as g(n)
Divide: Split A down the middle into
subsequences, each of size n/2
Conquer: Sort each subsequence by calling
merge sort recursively
Merge: The two sorted sequences into a
single sorted list
Merge Sort
Advanced Algorithm Analysis (CS-717) 36
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 5 8 6
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 5 6 8
Merge-sort(array A, int p, int r)
1. if (p<r)
2. then
3. q (p + r) / 2
4. Merge-sort(A, p, q) //sort A[pq]
5. Merge-sort(A, q+1, r) //sort A[q+1..r]
6. Merge(A, p, q, r)
Algorithm Merge-Sort
Advanced Algorithm Analysis (CS-717) 39
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. int j =q+1
5. while(i <= q) and (j <= r)
6. do if(A[i] <= A[j])
7. then B[k++] = A[i++]
8. else B[k++] = A[j++]
9. while(i<=q)
10. do B[k++] = A[i++]
11. while(j <= r)
12. do B[k++] = A[j++]
13. for i = p to r
14. do A[i] = B[i]
Recurrence Relation
Advanced Algorithm Analysis (CS-717) 41
1 1
( )
( / 2 ( / 2 ( )
if n
T n
T n T n n otherwise
=


=
`
+ +O
( (

(
)
This kind of equation is called Recurrence
Equation or Recurrence
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
= + +O
( (
(
=
= + + = + + =
= + + = + + =
= + + = + + =
= + + = + + =
= + + = + + =
= + + = + + =
= + + 12 12 8 32 = + + =
What is the pattern here?
Lets consider the ratios
()

for powers of 2
Solving the Recursion
Advanced Algorithm Analysis (CS-717) 43
(1)
1
1
(2) 4
2
2 2
T
T
=
= =
(4) 12
3
4 4
(8) 32
4
8 8
T
T
= =
= =
This suggests ()/ = log + 1
Or, () = log + which is ( log )
(use the limit rule)
If n is assumed to be a power of 2 (2
k
= n),
this will simplify the recurrence to
Eliminating Floor and Ceiling
Advanced Algorithm Analysis (CS-717) 44
1 1
( )
2 ( )
2
if n
T n
n
T n otherwise
=


=
`
+

)
The iteration method turns the recurrence
into a summation
Lets expand the recurrence:
The Iteration Method
Advanced Algorithm Analysis (CS-717) 45
The Iteration Method
Advanced Algorithm Analysis (CS-717) 46
n
T(n) = 2T( )
2
n
T(n) = 2(2T( ) )
4 2
n
T(n) = 4T( )
4
n
T(n) = 4(2T( ) )
8 4
n
T(n) = 8T( )
8
n
T(n) = 8(2T( ) )
16 8
n
T(n) = 16T( )
16
n
n
n
n n
n
n n
n n n
n
n n n
n n n n
+
+ +
+ +
+ + +
+ + +
+ + + +
+ + + +
If n is a power of 2 then let n = 2
k
or k = log n
The Iteration Method
Advanced Algorithm Analysis (CS-717) 47
k
k
k
k
logn
logn
logn
n
T(n) = 2 T( ) ( )
2
n
T(n) = 2 T( )
2
n
T(n) = 2 T( ) (logn)
2
n
T(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
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
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)
SORTING
Advanced Algorithm Analysis (CS-717) 50
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
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 sub
array
Create this position by shifting all larger
elements to the right
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]
A heap is a left-complete binary tree that
conforms to the heap order
The heap order property: in a (min) heap, for
every node X, the key in the parent is smaller
than or equal to the key in X
In other words, the parent node has key
smaller than or equal to both of its children
nodes
Heap
Advanced Algorithm Analysis (CS-717) 54
Similarly, in a max heap, the parent has a key
larger than or equal both of its children
Thus the smallest key is in the root in a min
heap; in the max heap, the largest is in the
root
Heap
Advanced Algorithm Analysis (CS-717) 55
Heap
Advanced Algorithm Analysis (CS-717) 56
16
3
7 8
10
14
9
1 4 2
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
Access to nodes involves simple arithmetic
operations:
parent(i): returns /2 , the parent of . The
root 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
We build a max heap out of the given array of
numbers A[1..n]
We repeatedly extract the maximum item from
the heap
Once the max item is removed, we are left with a
hole at the root
To fix this, we will replace it with the last leaf in
tree
But now the heap order will very likely be
destroyed
We will apply a heapify procedure to the root to
restore the heap
Heapsort Algorithm
Advanced Algorithm Analysis (CS-717) 58
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
Advanced Algorithm Analysis (CS-717) 60
87
12
44
57
15 19
23
87 57 44 12 15 19 23
1 2 3 4 5 6 7
Heapsort Trace
Advanced Algorithm Analysis (CS-717) 61
23
12
44
57
15 19
87
23 57 44 12 15 19 87
1 2 3 4 5 6 7
sorted
Heapsort Trace
Advanced Algorithm Analysis (CS-717) 62
23
12
44
57
15 19
23 57 44 12 15 19 87
1 2 3 4 5 6 7
sorted
heap violated
Heapsort Trace
Advanced Algorithm Analysis (CS-717) 63
23
12
44
57
15 19
23 57 44 12 15 19 87
1 2 3 4 5 6 7
sorted
Heapsort Trace
Advanced Algorithm Analysis (CS-717) 64
57
12
44
23
15 19
57 23 44 12 15 19 87
1 2 3 4 5 6 7
sorted
Heapsort Trace
Advanced Algorithm Analysis (CS-717) 65
19
12
44
23
15 57
19 23 44 12 15 57 87
1 2 3 4 5 6 7
sorted
Heapsort Trace
Advanced Algorithm Analysis (CS-717) 66
19
12
44
23
15
19 23 44 12 15 57 87
1 2 3 4 5 6 7
sorted
heap violated
Heapsort Trace
Advanced Algorithm Analysis (CS-717) 67
19
12
44
23
15
19 23 44 12 15 57 87
1 2 3 4 5 6 7
sorted
Heapsort Trace
Advanced Algorithm Analysis (CS-717) 68
44
12
19
23
15
44 23 19 12 15 57 87
1 2 3 4 5 6 7
sorted
Heapsort Trace
Advanced Algorithm Analysis (CS-717) 69
15
12
19
23
15 23 19 12 44 57 87
1 2 3 4 5 6 7
sorted
heap violated
Heapsort Trace
Advanced Algorithm Analysis (CS-717) 70
15
12
19
23
15 23 19 12 44 57 87
1 2 3 4 5 6 7
sorted
Heapsort Trace
Advanced Algorithm Analysis (CS-717) 71
23
12
19
15
23 15 19 12 44 57 87
1 2 3 4 5 6 7
sorted
Heapsort Trace
Advanced Algorithm Analysis (CS-717) 72
12
19
15
12 15 19 23 44 57 87
1 2 3 4 5 6 7
sorted
heap violated
Heapsort Trace
Advanced Algorithm Analysis (CS-717) 73
12
19
15
12 15 19 23 44 57 87
1 2 3 4 5 6 7
sorted
Heapsort Trace
Advanced Algorithm Analysis (CS-717) 74
19
12
15
19 15 12 23 44 57 87
1 2 3 4 5 6 7
sorted
Heapsort Trace
Advanced Algorithm Analysis (CS-717) 75
19
12
15
19 15 12 23 44 57 87
1 2 3 4 5 6 7
sorted
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
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
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
Advanced Algorithm Analysis (CS-717) 79
12
12 15 19 23 44 57 87
1 2 3 4 5 6 7
sorted
Heapsort Trace
Advanced Algorithm Analysis (CS-717) 80
12 15 19 23 44 57 87
1 2 3 4 5 6 7
sorted
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 maximum
elements from the heap
Each extract requires a constant amount of
work (swap) and O(log n) heapify
Heapsort is thus O(n log n)
Analysis of Heapsort
Advanced Algorithm Analysis (CS-717) 81
Quicksort
Advanced Algorithm Analysis (CS-717) 82
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)
Quicksort
Advanced Algorithm Analysis (CS-717) 83
PARTITION( array A, int p, int r)
1. x A[p]
2. q p
3. for s p + 1 to r
4. 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
Partition Algorithm
Advanced Algorithm Analysis (CS-717) 84
Partition Algorithm
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 3
p
q
r
s
7 5 10 8 6 9 5 3
p
q
r
s
Partition Algorithm
Advanced Algorithm Analysis (CS-717) 86
7 5 10 8 6 9 5 3
p
q
r
s
7 5 6 8 10 9 5 3
p
q
r
s
7 5 6 8 10 9 5 3
p
q
r
s
Partition Algorithm
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 10
p
q
r
s
3 5 6 5 7 9 8 10
p
q
r
s
The running time of quicksort depends
heavily 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
Analysis of Quicksort
Advanced Algorithm Analysis (CS-717) 88
Counting Sort
Advanced Algorithm Analysis (CS-717) 89
CountingSort(A, B, n, k)
1. for i 0 to k
2. do C[i] 0
3. for j 1 to n
4. do C[A[j]] C[A[j]] +1
5. for i 1 to k
6. do C[i] C[i] + C[i-1]
7. for j n downto 1
8. B[ C[A[j]] ] A[j]
9. C[ A[j] ] C[ A[j] ] -1
Counting Sort
Advanced Algorithm Analysis (CS-717) 90
Counting Sort
Advanced Algorithm Analysis (CS-717) 91
7 1 3 1 2 4 5 7 2 4 3 A
Input
A[1n]
0 0 0 0 0 0 0
1 2 3 4 5 6 7
K = 7
C[1k]
1 2 3 4 5 6 7 8 9 10 11
Counting Sort
Advanced Algorithm Analysis (CS-717) 92
7 1 3 1 2 4 5 7 2 4 3 A
Input
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
Advanced Algorithm Analysis (CS-717) 93
7 1 3 1 2 4 5 7 2 4 3 A
Input
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
Advanced Algorithm Analysis (CS-717) 94
7 1 3 1 2 4 5 7 2 4 3 A
Input
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
Advanced Algorithm Analysis (CS-717) 95
7 1 3 1 2 4 5 7 2 4 3 A
Input
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
Advanced Algorithm Analysis (CS-717) 96
7 1 3 1 2 4 5 7 2 4 3 A
Input
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
Advanced Algorithm Analysis (CS-717) 97
7 1 3 1 2 4 5 7 2 4 3 A
Input
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
Advanced Algorithm Analysis (CS-717) 98
7 1 3 1 2 4 5 7 2 4 3 A
Input
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
Advanced Algorithm Analysis (CS-717) 99
7 1 3 1 2 4 5 7 2 4 3 A
Input
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
Advanced Algorithm Analysis (CS-717) 100
7 1 3 1 2 4 5 7 2 4 3 A
Input
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
Advanced Algorithm Analysis (CS-717) 101
7 1 3 1 2 4 5 7 2 4 3 A
Input
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
Counting Sort
Advanced Algorithm Analysis (CS-717) 102
7 1 3 1 2 4 5 7 2 4 3 A
Input
A[1n]
2 2 2 2 1 0 2
1 2 3 4 5 6 7
Finally
C[1k]
1 2 3 4 5 6 7 8 9 10 11
k
Counting Sort
Advanced Algorithm Analysis (CS-717) 103
7 1 3 1 2 4 5 7 2 4 3 A
Input
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
for i = 2 to 7
do C[i] = C[i] + C[i-1]
Counting Sort
Advanced Algorithm Analysis (CS-717) 104
7 1 3 1 2 4 5 7 2 4 3 A
Input
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
Advanced Algorithm Analysis (CS-717) 105
7 1 3 1 2 4 5 7 2 4 3 A
Input
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
Advanced Algorithm Analysis (CS-717) 106
7 1 3 1 2 4 5 7 2 4 3 A
Input
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 4 2
Counting Sort
Advanced Algorithm Analysis (CS-717) 107
7 1 3 1 2 4 5 7 2 4 3 A
Input
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 4 2 7
Counting Sort
Advanced Algorithm Analysis (CS-717) 108
7 1 3 1 2 4 5 7 2 4 3 A
Input
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 4 2 7 5
Counting Sort
Advanced Algorithm Analysis (CS-717) 109
7 1 3 1 2 4 5 7 2 4 3 A
Input
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 4 2 7 5 4
Counting Sort
Advanced Algorithm Analysis (CS-717) 110
7 1 3 1 2 4 5 7 2 4 3 A
Input
A[1n]
2 3 5 6 8 9 10
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8 9 10 11
2 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 4 2 7 5 4 2
Counting Sort
Advanced Algorithm Analysis (CS-717) 111
7 1 3 1 2 4 5 7 2 4 3 A
Input
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 4 2 7 5 4 2 1
Counting Sort
Advanced Algorithm Analysis (CS-717) 112
7 1 3 1 2 4 5 7 2 4 3 A
Input
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 4 2 7 5 4 2 3 1
Counting Sort
Advanced Algorithm Analysis (CS-717) 113
7 1 3 1 2 4 5 7 2 4 3 A
Input
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 4 2 7 5 4 2 3 1 1
Counting Sort
Advanced Algorithm Analysis (CS-717) 114
7 1 3 1 2 4 5 7 2 4 3 A
Input
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 4 2 7 5 4 2 3 1 7 1
CountingSort(A, B, n, k)
1. for i 0 to k
2. do C[i] 0
3. for j 1 to n
4. do C[A[j]] C[A[j]] +1
5. for i 1 to k
6. do C[i] C[i] + C[i-1]
7. for j n downto 1
8. B[ C[A[j]] ] A[j]
9. C[ A[j] ] C[ A[j] ] -1
Analysis of Counting Sort
Advanced Algorithm Analysis (CS-717) 115
(k)
(n)
(k)
(n)
Bucket Sort
Advanced Algorithm Analysis (CS-717) 116
Bucket Sort
Advanced Algorithm Analysis (CS-717) 117
.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 1:
insertion sort
within each list
Bucket Sort
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
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
Radix Sort
Advanced Algorithm Analysis (CS-717) 120
Radix Sort
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
Questions
Advanced Algorithm Analysis (CS-717)

You might also like