Professional Documents
Culture Documents
ANUJ DUBEY
IIIrd B.TECH.(C.S.E.)
H.B.T.I. KANPUR
How fast can we sort?
All the sorting algorithms we have seen so
far are comparison sorts: only use
comparisons to determine the relative
order of elements.
The best worst-case running time that we’ve
seen for comparison sorting is O(n lg n) .
Prove a Lower Bound for
any comparison based
algorithm for the Sorting
Problem
How?
Decision trees help us.
Decision-tree example
Sort 〈a1, a2, …, an〉 1:2
2:3 1:3
Stable sort: If two records are the same for that digit,
their order does not change.
Value v: 0 1 2 3
# of records with digit v: 5 9 3 2
Value v: 0 1 2 3
# of records with digit v: 5 9 3 3
# of records with digit < v: 0 5 14 17
Value v: 0 1 2 3
# of records with digit < v: 0 5 14 17
Location of first record
with digit v.
CountingSort
Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0
Output: 0 ? 1
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Value v: 0 1 2 3
Location of first record 0 5 14 17
with digit v.
Algorithm: Go through the records in order
putting them where they go.
CountingSort
Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0
Output: 1
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Value v: 0 1 2 3
Location of next record 0 5 14 17
with digit v.
Algorithm: Go through the records in order
putting them where they go.
CountingSort
Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0
Output: 0 1
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Value v: 0 1 2 3
Location of next record 0 6 14 17
with digit v.
Algorithm: Go through the records in order
putting them where they go.
CountingSort
Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0
Output: 0 0 1
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Value v: 0 1 2 3
Location of next record 1 6 14 17
with digit v.
Algorithm: Go through the records in order
putting them where they go.
CountingSort
Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0
Output: 0 0 1 1
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Value v: 0 1 2 3
Location of next record 2 6 14 17
with digit v.
Algorithm: Go through the records in order
putting them where they go.
CountingSort
Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0
Output: 0 0 1 1 3
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Value v: 0 1 2 3
Location of next record 2 7 14 17
with digit v.
Algorithm: Go through the records in order
putting them where they go.
CountingSort
Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0
Output: 0 0 1 1 1 3
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Value v: 0 1 2 3
Location of next record 2 7 14 18
with digit v.
Algorithm: Go through the records in order
putting them where they go.
CountingSort
Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0
Output: 0 0 1 1 1 1 3
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Value v: 0 1 2 3
Location of next record 2 8 14 18
with digit v.
Algorithm: Go through the records in order
putting them where they go.
CountingSort
Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0
Output: 0 0 1 1 1 1 3 3
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Value v: 0 1 2 3
Location of next record 2 9 14 18
with digit v.
Algorithm: Go through the records in order
putting them where they go.
CountingSort
Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0
Output: 0 0 1 1 1 1 1 3 3
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Value v: 0 1 2 3
Location of next record 2 9 14 19
with digit v.
Algorithm: Go through the records in order
putting them where they go.
CountingSort
Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0
Output: 0 0 0 1 1 1 1 1 3 3
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Value v: 0 1 2 3
Location of next record 2 10 14 19
with digit v.
Algorithm: Go through the records in order
putting them where they go.
CountingSort
Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0
Output: 0 0 0 1 1 1 1 1 2 3 3
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Value v: 0 1 2 3
Location of next record 3 10 14 19
with digit v.
Algorithm: Go through the records in order
putting them where they go.
CountingSort
Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0
Output: 0 0 0 1 1 1 1 1 1 2 3 3
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Value v: 0 1 2 3
Location of next record 3 10 15 19
with digit v.
Algorithm: Go through the records in order
putting them where they go.
CountingSort
Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0
Output: 0 0 0 1 1 1 1 1 1 2 3 3
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Value v: 0 1 2 3
Location of next record 3 10 15 19
with digit v.
Algorithm: Go through the records in order
putting them where they go.
CountingSort
Input: 1 0 0 1 3 1 1 3 1 0 2 1 0 1 1 2 2 1 0
Output: 0 0 0 0 0 1 1 1 1 1 1 1 1 1 2 2 2 3 3
Index: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Value v: 0 1 2 3
Location of next record 5 14 17 19
with digit v.
Θ(N) ×(log
Time = Θ(N+k)
Total (Counting Ops)
N + log k) (bit ops)
Analysis
for i ← 1 to k
Θ(k) do C[i] ← 0
for j ← 1 to n
Θ(n) do C[A[ j]] ← C[A[ j]] + 1
for i ← 2 to k
Θ(k) do C[i] ← C[i] + C[i–1]
for j ← n downto 1
Θ(n) do B[C[A[ j]]] ← A[ j]
C[A[ j]] ← C[A[ j]] – 1
Θ(n + k)
Radix Sort
• Algorithm
– sort by the least significant digit first (counting
sort)
– sort by the next least significant digit
– continue this process until the numbers have
been sorted on all k digits
Radix Sort
• Does it work?
Correctness:
• Induction on number of passes ( i in pseudocode).
• Assume digits 1, 2, . . . , i-1 are sorted.
• Show that a stable sort on digit i leaves digits 1, ..., i sorted:
• If 2 digits in position i are different, ordering by position i is correct,
and positions 1, . . . , i-1 are irrelevant.
• If 2 digits in position i are equal, numbers are already in the right
order (by inductive hypothesis). The stable sort on digit i leaves them
in the right order.
Analysis of Radix Sort
Given n d-digit numbers in which each digit can take
on up to k possible values, RADIX-SORT correctly
sorts these numbers in Θ (d( n + k)) time.
(1)
(1) n
(n )
(n )
Correctness:
Consider A[i ], A[ j ].
Assume without loss of generality that A[i ] ≤ A[j].