Sorting in linear time
description
Transcript of Sorting in linear time
![Page 1: Sorting in linear time](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813c03550346895da5670e/html5/thumbnails/1.jpg)
Sorting in linear time
![Page 2: Sorting in linear time](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813c03550346895da5670e/html5/thumbnails/2.jpg)
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.•E.g., insertion sort, merge sort, quicksort,
heapsort.
The best worst-case running time that we’ve seen for comparison sorting is O(n lg n) .
Q:Is O(n lg n) the best we can do?A: Yes, as long as we use comparison sorts
![Page 3: Sorting in linear time](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813c03550346895da5670e/html5/thumbnails/3.jpg)
Sorting in linear time
Counting sort: No comparisons between elements.
•Input: A[1 . . n], where A[ j]{1, 2, …, k} .•Output: B[1 . . n], sorted.•Auxiliary storage: C[1 . . k] .
![Page 4: Sorting in linear time](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813c03550346895da5670e/html5/thumbnails/4.jpg)
Counting sort
for i 1 to kdo C[i] 0
for j 1 to ndo C[A[ j]] C[A[ j]] + 1 ⊳ C[i] = |{key = i}|
for i 2 to kdo C[i] C[i] + C[i–1] ⊳ C[i] = |{key i}|
for j n downto 1do B[C[A[ j]]] A[ j]
C[A[ j]] C[A[ j]] – 1
![Page 5: Sorting in linear time](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813c03550346895da5670e/html5/thumbnails/5.jpg)
Counting-sort example
A: 4 1 3 4 3
B:
1 2 3 4 5
C:
1 2 3 4
![Page 6: Sorting in linear time](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813c03550346895da5670e/html5/thumbnails/6.jpg)
Loop 1
A: 4 1 3 4 3
B:
1 2 3 4 5
C: 0 0 0 0
1 2 3 4
for i 1 to kdo C[i] 0
![Page 7: Sorting in linear time](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813c03550346895da5670e/html5/thumbnails/7.jpg)
Loop 2
A: 4 1 3 4 3
B:
1 2 3 4 5
C: 0 0 0 1
1 2 3 4
for j 1 to ndo C[A[ j]] C[A[ j]] + 1 ⊳ C[i] = |{key = i}|
![Page 8: Sorting in linear time](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813c03550346895da5670e/html5/thumbnails/8.jpg)
Loop 2
A: 4 1 3 4 3
B:
1 2 3 4 5
C: 1 0 0 1
1 2 3 4
for j 1 to ndo C[A[ j]] C[A[ j]] + 1 ⊳ C[i] = |{key = i}|
![Page 9: Sorting in linear time](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813c03550346895da5670e/html5/thumbnails/9.jpg)
Loop 2
A: 4 1 3 4 3
B:
1 2 3 4 5
C: 1 0 1 1
1 2 3 4
for j 1 to ndo C[A[ j]] C[A[ j]] + 1 ⊳ C[i] = |{key = i}|
![Page 10: Sorting in linear time](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813c03550346895da5670e/html5/thumbnails/10.jpg)
Loop 2
A: 4 1 3 4 3
B:
1 2 3 4 5
C: 1 0 1 2
1 2 3 4
for j 1 to ndo C[A[ j]] C[A[ j]] + 1 ⊳ C[i] = |{key = i}|
![Page 11: Sorting in linear time](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813c03550346895da5670e/html5/thumbnails/11.jpg)
Loop 2
A: 4 1 3 4 3
B:
1 2 3 4 5
C: 1 0 2 2
1 2 3 4
for j 1 to ndo C[A[ j]] C[A[ j]] + 1 ⊳ C[i] = |{key = i}|
![Page 12: Sorting in linear time](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813c03550346895da5670e/html5/thumbnails/12.jpg)
Loop 3
A: 4 1 3 4 3
B:
1 2 3 4 5
C: 1 0 2 2
1 2 3 4
C': 1 1 2 2
for i 2 to kdo C[i] C[i] + C[i–1] ⊳ C[i] = |{key i}|
![Page 13: Sorting in linear time](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813c03550346895da5670e/html5/thumbnails/13.jpg)
Loop 3
A: 4 1 3 4 3
B:
1 2 3 4 5
C: 1 0 2 2
1 2 3 4
C': 1 1 3 2
for i 2 to kdo C[i] C[i] + C[i–1] ⊳ C[i] = |{key i}|
![Page 14: Sorting in linear time](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813c03550346895da5670e/html5/thumbnails/14.jpg)
Loop 3
A: 4 1 3 4 3
B:
1 2 3 4 5
C: 1 0 2 2
1 2 3 4
C': 1 1 3 5
for i 2 to kdo C[i] C[i] + C[i–1] ⊳ C[i] = |{key i}|
![Page 15: Sorting in linear time](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813c03550346895da5670e/html5/thumbnails/15.jpg)
Loop 4
A: 4 1 3 4 3
B: 3
1 2 3 4 5
C: 1 1 3 5
1 2 3 4
C': 1 1 2 5
for j n downto 1do B[C[A[ j]]] A[ j]
C[A[ j]] C[A[ j]] – 1
![Page 16: Sorting in linear time](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813c03550346895da5670e/html5/thumbnails/16.jpg)
Loop 4
A: 4 1 3 4 3
B: 3 4
1 2 3 4 5
C: 1 1 2 5
1 2 3 4
C': 1 1 2 4
for j n downto 1do B[C[A[ j]]] A[ j]
C[A[ j]] C[A[ j]] – 1
![Page 17: Sorting in linear time](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813c03550346895da5670e/html5/thumbnails/17.jpg)
Loop 4
A: 4 1 3 4 3
B: 3 3 4
1 2 3 4 5
C: 1 1 2 4
1 2 3 4
C': 1 1 1 4
for j n downto 1do B[C[A[ j]]] A[ j]
C[A[ j]] C[A[ j]] – 1
![Page 18: Sorting in linear time](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813c03550346895da5670e/html5/thumbnails/18.jpg)
Loop 4
A: 4 1 3 4 3
B: 1 3 3 4
1 2 3 4 5
C: 1 1 1 4
1 2 3 4
C': 0 1 1 4
for j n downto 1do B[C[A[ j]]] A[ j]
C[A[ j]] C[A[ j]] – 1
![Page 19: Sorting in linear time](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813c03550346895da5670e/html5/thumbnails/19.jpg)
Loop 4
A: 4 1 3 4 3
B: 1 3 3 4 4
1 2 3 4 5
C: 0 1 1 4
1 2 3 4
C': 0 1 1 3
for j n downto 1do B[C[A[ j]]] A[ j]
C[A[ j]] C[A[ j]] – 1
![Page 20: Sorting in linear time](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813c03550346895da5670e/html5/thumbnails/20.jpg)
Analysisfor i 1 to k
do C[i] 0
(n)
(k)
(n)
(k)
for j 1 to ndo C[A[ j]] C[A[ j]] + 1
for i 2 to kdo C[i] C[i] + C[i–1]
for j n downto 1do B[C[A[ j]]] A[ j]
C[A[ j]] C[A[ j]] – 1(n + k)
![Page 21: Sorting in linear time](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813c03550346895da5670e/html5/thumbnails/21.jpg)
Running time
If k = O(n), then counting sort takes (n) time.
•But, sorting takes (n lg n) time!
•Where’s the fallacy?
Answer:
•Comparison sorting takes (n lg n) time.
•Counting sort is not a comparison sort.
• In fact, not a single comparison between elements occurs!
![Page 22: Sorting in linear time](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813c03550346895da5670e/html5/thumbnails/22.jpg)
Stable sorting
Counting sort is a stable sort: it preserves the input order among equal elements.
A: 4 1 3 4 3
B: 1 3 3 4 4
![Page 23: Sorting in linear time](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813c03550346895da5670e/html5/thumbnails/23.jpg)
Radix sort
•Origin: Herman Hollerith’s card-sorting machine for the 1890 U.S. Census. (See Appendix .)
•Digit-by-digit sort.
•Hollerith’s original (bad) idea: sort on most-significant digit first.
•Good idea: Sort on least-significant digit first with auxiliary stable sort.
![Page 24: Sorting in linear time](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813c03550346895da5670e/html5/thumbnails/24.jpg)
Operation of radix sort
3 2 94 5 76 5 78 3 94 3 67 2 03 5 5
7 2 03 5 54 3 64 5 76 5 73 2 98 3 9
7 2 03 2 94 3 68 3 93 5 54 5 76 5 7
3 2 93 5 54 3 64 5 76 5 77 2 08 3 9
![Page 25: Sorting in linear time](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813c03550346895da5670e/html5/thumbnails/25.jpg)
•Sort on digit t
Correctness of radix sort
Induction on digit position
•Assume that the numbers are sorted by their low-order t – 1 digits.
7 2 03 2 94 3 68 3 93 5 54 5 76 5 7
3 2 93 5 54 3 64 5 76 5 77 2 08 3 9
![Page 26: Sorting in linear time](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813c03550346895da5670e/html5/thumbnails/26.jpg)
•Sort on digit t
Correctness of radix sort
Induction on digit position
•Assume that the numbers are sorted by their low-order t – 1 digits.
7 2 03 2 94 3 68 3 93 5 54 5 76 5 7
3 2 93 5 54 3 64 5 76 5 77 2 08 3 9
Two numbers that differ in digit t are correctly sorted.
![Page 27: Sorting in linear time](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813c03550346895da5670e/html5/thumbnails/27.jpg)
•Sort on digit t
Correctness of radix sort
Induction on digit position
•Assume that the numbers are sorted by their low-order t – 1 digits.
7 2 03 2 94 3 68 3 93 5 54 5 76 5 7
3 2 93 5 54 3 64 5 76 5 77 2 08 3 9
Two numbers that differ in digit t are correctly sorted.
Two numbers equal in digit t are put in the same order as the input correct order.
![Page 28: Sorting in linear time](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813c03550346895da5670e/html5/thumbnails/28.jpg)
RADIX SORT ALGORITHM
• RADIX_SORT(A,d)
1.For i 1to d
2. do use stable sort to sort array A on
digit i
![Page 29: Sorting in linear time](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813c03550346895da5670e/html5/thumbnails/29.jpg)
BUCKET SORT
![Page 30: Sorting in linear time](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813c03550346895da5670e/html5/thumbnails/30.jpg)
30
Bucket Sort
• Pseudo code of Bucket sort is:
![Page 31: Sorting in linear time](https://reader035.fdocuments.us/reader035/viewer/2022070404/56813c03550346895da5670e/html5/thumbnails/31.jpg)
Bucket Sort Example