radix sort -...

54
Radix Sort

Upload: lamcong

Post on 17-Apr-2019

228 views

Category:

Documents


0 download

TRANSCRIPT

Radix Sort

Radix Sort

45834 6283 56323 92634 78332 55111

Radix Sort

45834 6283 56323 92634 78332 55111

Radix Sort

45834 6283 56323 92634 78332 55111

0:1:2:3:4:5:6:7:8:9:

Radix Sort

45834 6283 56323 92634 78332 55111

0:1:2:3:4:5:6:7:8:9:

Radix Sort

45834

6283 56323 92634 78332 55111

0:1:2:3:4:5:6:7:8:9:

Radix Sort

45834

6283 56323 92634 78332 55111

0:1:2:3:4:5:6:7:8:9:

Radix Sort

45834

6283 56323 92634 78332 55111

0:1:2:3:4:5:6:7:8:9:

Radix Sort

458346283

56323 92634 78332 55111

0:1:2:3:4:5:6:7:8:9:

Radix Sort

458346283

56323 92634 78332 55111

0:1:2:3:4:5:6:7:8:9:

Radix Sort

458346283

56323 92634 78332 55111

0:1:2:3:4:5:6:7:8:9:

Radix Sort

458346283 56323

92634 78332 55111

0:1:2:3:4:5:6:7:8:9:

Radix Sort

458346283 56323

92634 78332 55111

0:1:2:3:4:5:6:7:8:9:

Radix Sort

458346283 56323

92634 78332 55111

0:1:2:3:4:5:6:7:8:9:

Radix Sort

458346283 56323

92634

78332 55111

0:1:2:3:4:5:6:7:8:9:

Radix Sort

458346283 56323

92634

78332 55111

0:1:2:3:4:5:6:7:8:9:

Radix Sort

458346283 56323

92634

78332 55111

0:1:2:3:4:5:6:7:8:9:

Radix Sort

458346283 56323

92634

78332

55111

0:1:2:3:4:5:6:7:8:9:

Radix Sort

458346283 56323

92634

78332

55111

0:1:2:3:4:5:6:7:8:9:

Radix Sort

458346283 56323

92634

78332

55111

0:1:2:3:4:5:6:7:8:9:

Radix Sort

458346283 56323

92634

7833255111

0:1:2:3:4:5:6:7:8:9:

Radix Sort

458346283 56323

92634

7833255111

0:1:2:3:4:5:6:7:8:9:

Radix Sort

458346283 56323

92634

7833255111

0:1:2:3:4:5:6:7:8:9:

Radix Sort

458346283 56323

92634

78332

55111

0:1:2:3:4:5:6:7:8:9:

Radix Sort

458346283 56323

92634

78332

55111

0:1:2:3:4:5:6:7:8:9:

Radix Sort

458346283 56323

92634

7833255111

0:1:2:3:4:5:6:7:8:9:

Radix Sort

458346283 56323

92634

7833255111

0:1:2:3:4:5:6:7:8:9:

Radix Sort

458346283 56323

92634

7833255111

0:1:2:3:4:5:6:7:8:9:

Radix Sort

45834

6283 56323

92634

7833255111

0:1:2:3:4:5:6:7:8:9:

Radix Sort

45834

6283 56323

92634

7833255111

0:1:2:3:4:5:6:7:8:9:

Radix Sort

45834

6283 56323

92634

7833255111

0:1:2:3:4:5:6:7:8:9:

Radix Sort

458346283 56323 926347833255111

0:1:2:3:4:5:6:7:8:9:

Radix Sort

458346283 56323 926347833255111

0:1:2:3:4:5:6:7:8:9:

Radix Sort

45834

6283

563239263478332

551110:1:2:3:4:5:6:7:8:9:

Radix Sort

0:1:2:3:4:5:6:7:8:9:

9263478332 45834 62835632355111

Radix Sort

0:1:2:3:4:5:6:7:8:9:

92634

78332

45834

6283

56323

55111

Radix Sort

0:1:2:3:4:5:6:7:8:9:

9263456323 78332 45834628355111

Radix Sort

0:1:2:3:4:5:6:7:8:9:

92634

78332

45834563236283

55111

Radix Sort

0:1:2:3:4:5:6:7:8:9:

5632345834 6283 783325511192634

Radix Sort

0:1:2:3:4:5:6:7:8:9:

6283

4583456323

78332

55111

92634

Radix Sort

0:1:2:3:4:5:6:7:8:9:

7833255111 56323 92634458346283

Radix Sort

Example shows working from LSD to MSD. Is radix sort correct working from MSD to LSD?

Radix Sort

Example shows working from LSD to MSD. Is radix sort correct working from MSD to LSD?

No! Here is a counter-example:

72 27

Radix Sort

Example shows working from LSD to MSD. Is radix sort correct working from MSD to LSD?

No! Here is a counter-example:

72

272:...7:

Radix Sort

Example shows working from LSD to MSD. Is radix sort correct working from MSD to LSD?

No! Here is a counter-example:

27 72

Radix Sort

Example shows working from LSD to MSD. Is radix sort correct working from MSD to LSD?

No! Here is a counter-example:

27

722:...7:

Radix Sort

Example shows working from LSD to MSD. Is radix sort correct working from MSD to LSD?

No! Here is a counter-example - numbers not increasing:

72 27

Radix SortCan we prove that radix sort is correct when working from LSD to MSD?

Radix SortCan we prove that radix sort is correct when working from LSD to MSD?

Yes! By induction on the digit being sorted

Radix SortCan we prove that radix sort is correct when working from LSD to MSD?

Yes! By induction on the digit being sorted

Base step: After 1st step – all numbers in order on LSD

Radix SortCan we prove that radix sort is correct when working from LSD to MSD?

Yes! By induction on the digit being sorted

Base step: After 1st step – all numbers in order on LSD

Induction step: suppose (P) after k th step that all numbers are

in non-decreasing order on k least significant digits.

Radix SortCan we prove that radix sort is correct when working from LSD to MSD?

Yes! By induction on the digit being sorted

Base step: After 1st step – all numbers in order on LSD

Induction step: suppose (P) after k th step that all numbers are

in non-decreasing order on k least significant digits. Consider a queue on the k+1

st step where at least two numbers are out of order in the k least significant digits. For example,

572 527 5495: (k=2)

Radix SortCan we prove that radix sort is correct when working from LSD to MSD?

Yes! By induction on the digit being sorted

Base step: After 1st step – all numbers in order on LSD

Induction step: suppose (P) after k th step that all numbers are

in non-decreasing order on k least significant digits. Consider a queue on the k+1

st step where at least two numbers are out of order in the k least significant digits. For example,

The numbers in that queue are in the same order they were in the list following the k

th step. But that is a violation of (P). Hence, all numbers in a queue are in non-decreasing order.

572 527 5495: (k=2)

Radix SortCan we prove that radix sort is correct when working from LSD to MSD?

Yes! By induction on the digit being sorted

Base step: After 1st step – all numbers in order on LSD

Induction step: suppose (P) after k th step that all numbers are

in non-decreasing order on k least significant digits. Consider a queue on the k+1

st step where at least two numbers are out of order in the k least significant digits. For example,

The numbers in that queue are in the same order they were in the list following the k

th step. But that is a violation of (P). Hence, all numbers in a queue are in non-decreasing order. Since assembling the list after the k+1

st step is by increasing queue rank (k+1

st digit) all numbers in the list are in non- decreasing order.

572 527 5495: (k=2)