Sorting in Linear Time
Sorting in Linear Time
Sorting in Linear Time
Time
1
About this lecture
• Sorting algorithms we studied so far
– Insertion, Selection, Merge, Quicksort
determine sorted order by comparison
2
Counting Sort
3
Counting Sort extra info
on values
• Input: Array A[1..n] of n integers,
each has value from [0,k]
• Output: Sorted array of the n integers
• Idea 1: Create B[1..n] to store the output
• Idea 2: Process A[1..n] from right to left
• Use k + 2 counters:
• One for “which element to process”
• k + 1 for “where to place”
4
Counting Sort (Details)
Before Running
A
2 1 2 5 3 3 1 2
k+1 counters
5
Counting Sort (Details)
Step 1: Set c[j] = location in B for placing the
next element if it has value j
A
2 1 2 5 3 3 1 2
next element
c[2] = 5 c[5] = 8
next element
c[2] = 4 c[5] = 8
B
2
next element
c[2] = 4 c[5] = 8
B
1 2
next element
c[2] = 4 c[5] = 8
B
1 2 3
next element
c[2] = 4 c[5] = 8
B
1 2 3 3
next element
c[2] = 4 c[5] = 7
B
1 2 3 3 5
next element
c[2] = 3 c[5] = 7
B
1 2 2 3 3 5
next element
c[2] = 3 c[5] = 7
B
1 1 2 2 3 3 5
A
2 1 2 5 3 3 1 2
next element
c[2] = 3 c[5] = 7
B
1 1 2 2 2 3 3 5
17
Stable Sort
Before
2 1 2 5 3 3 1 2
Sorting
After
Sorting 1 1 2 2 2 3 3 5
18
Radix Sort
19
Radix Sort extra info
on values
• Input: Array A[1..n] of n integers,
each has d digits, and
each digit has value from
[0,k]
• Output: Sorted array of the n integers
• Idea: Sort in d rounds
• At Round j, stable sort A on digit j
(where rightmost digit = digit 1)
20
Radix Sort (Example Run)
Before Running
1904
2579
1874
6355
4432
8318
1304
4 digits
21
Radix Sort (Example Run)
Round 1: Stable sort digit 1
1904 4432
2579 1904
1874 1874
6355 1304
4432 6355
831 8 831 8
1304 2579
22
Radix Sort (Example Run)
Round 2: Stable sort digit 2
4432 1903
1903 1304
1874 8318
1304 4432
6355 6355
831 8 1874
2579 2579
1903 1304
1304 8318
8318 6355
4432 4432
6355 2579
1874 1874
2579 1903
1304 1304
8318 1874
63 5 5 1903
4432 2579
2579 4432
1874 63 5 5
1903 8318
1304
1874
1904
2579
4432
63 5 5
8318
26
Radix Sort (Correctness)
Question:
“After r rounds, last r digits are sorted”
Why ??
Answer:
This can be proved by induction :
The statement is true for r = 1
Assume the statement is true for r = k
Then …
27
Radix Sort (Correctness)
At Round k+1,
• if two numbers differ in digit “k+1”, their
relative order [based on last k+1 digits] will be
correct after sorting digit “k+1”
• if two numbers match in digit “k+1”, their
relative order [based on last k+1 digits] will be
correct after stable sorting digit “k+1” (why?)
28
Radix Sort (Summary)
Conclusion:
• After d rounds, last d digits are sorted,
so that the numbers in A[1..n] are sorted
• There are d rounds of stable sort, each
can be done in ( n + k ) time
Running time = ( d (n + k) )
• if d=(1) and k=(n), asymptotically optimal
29
Bucket Sort
30
Bucket Sort extra info
on values
• Input: Array A[1..n] of n elements,
each is drawn uniformly at
random from the
interval [0,1)
• Output: Sorted array of the n elements
• Idea:
Distribute elements into n buckets, so
that each bucket is likely to have fewer
elements easier to sort
31
Bucket Sort (Details)
Before 0.78, 0.17, 0.39, 0.26, 0.72,
Running 0.94, 0.21, 0.12, 0.23, 0.68
Step 1:
Create n
buckets [0,0.1) [0.1,0.2) [0.2,0.3) [0.3,0.4) [0.4,0.5)
n = #buckets
= #elements
[0.5,0.6) [0.6,0.7) [0.7,0.8) [0.8,0.9) [0.9,1)
0.17 0.26
0.21 0.39
0.12 0.23
[0,0.1) [0.1,0.2) [0.2,0.3) [0.3,0.4) [0.4,0.5)
0.78 0.94
0.68
0.72
[0.5,0.6) [0.6,0.7) [0.7,0.8) [0.8,0.9) [0.9,1)
0.21
0.12 0.23
0.17 0.39
0.26
[0,0.1) [0.1,0.2) [0.2,0.3) [0.3,0.4) [0.4,0.5)
0.72 0.94
0.68 0.78
[0.5,0.6) [0.6,0.7) [0.7,0.8) [0.8,0.9) [0.9,1)
34
Bucket Sort (Details)
Step 4: Collect elements from Bucket 0 to Bucket n-1
0.21
0.12 0.23
0.17 0.39
0.26
[0,0.1) [0.1,0.2) [0.2,0.3) [0.3,0.4) [0.4,0.5)
0.72 0.94
0.68 0.78
[0.5,0.6) [0.6,0.7) [0.7,0.8) [0.8,0.9) [0.9,1)
36
Average Running Time
Let nj = # elements in Bucket j
varies on input
So, E[X] ≤ E[c(n02 + n12 + … + nn-12)]
= c E[n02 + n12 + … + nn-12]
= c (E[n02] + E[n12] + … + E[nn-12])
= cn E[n02] (by uniform
distribution) 37
Average Running Time
Textbook (pages 202-203) shows that
E[n02] = 2 – (1/n)
E[X] ≤ cn E[n02] = 2cn – c
In other words, E[X] = ( n )
Average running time = ( n )
38
For Interested Classmates
The following is how we can show
E[n02] = 2 – (1/n)
Then, n0 = Y1 + Y2 + … + Yn
39
For Interested Classmates
Then,
E[n02] = E[(Y1 + Y2 + … + Yn)2]
= E[ Y12 + Y22 + … + Yn2
+ Y1Y2 + Y1Y3 + … + Y1Yn
+ Y2Y1 + Y2Y3 + … + Y2Yn
+…
+ YnY1 + YnY2 + … + YnYn-1 ]
40
= E[Y12] + E[Y22] + … + E[Yn2]
+ E[Y1Y2] + … + E[YnYn-1 ]
= n E[Y12] + n(n-1) E[Y1Y2]
(by symmetry)
43