sorting in linear time

31
Sorting in linear time

Upload: drew

Post on 06-Jan-2016

19 views

Category:

Documents


0 download

DESCRIPTION

Sorting in linear time. Q:Is O(n lg n) the best we can do? A: Yes, as long as we use comparison sorts. 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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Sorting in linear time

Sorting in linear time

Page 2: Sorting in linear time

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

•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

•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

•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

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

BUCKET SORT

Page 30: Sorting in linear time

30

Bucket Sort

• Pseudo code of Bucket sort is:

Page 31: Sorting in linear time

Bucket Sort Example