Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why radix sort preferred to Least Significant Digit first

Tags:

algorithm

I still have no idea why radix sort preferred to Least Significant Digit first after reading introduct to algorithm.In my opinion,Most Significant Digit first should be the same as Least Significant Digit first,even better.Because the most Significant Digit first is heavier than the least,and it can reduce the sort times.Who can help to explain it ?

like image 692
liumilan Avatar asked Mar 21 '14 09:03

liumilan


2 Answers

Consider an example population with the values

91, 19, 55, 54

If you first distribute by LSD (ahem), you get

91, 54, 55, 19

then by MSD;

19, 54, 55, 91 - good

Now instead distribute first by MSD:

19, 55, 54, 91

and then by LSD:

91, 54, 55, 19 - bad

like image 148
500 - Internal Server Error Avatar answered Sep 19 '22 03:09

500 - Internal Server Error


Because Most Significant Digit first not works if you don't do extra works in number inputs.

Try think about the number input set: 1 2 10

Now you use MSDF in the below way(Not the Least Significant Digit first Way):

  1. Tens bucket: 1 10
    Twenties bucket: 2

  2. since two ten bucket just has one item, just handle one ten bucket
    Zero bucket: 1 10

  3. so final order is 1 10 2 wrong!

But

if you do the extra works for put the all number in same length, it will works:

  1. adjust input to same length 01 02 10

  2. Zero ten bucket: 01 02
    Tens bucket: 10

  3. since Tens bucket just has one item, just handle Zero ten bucket
    One bucket: 01
    Two bucket: 02

  4. so final order is 01 02 10 right!

Try 91, 19, 55, 54 in MSDF:

  1. all number is in the same length

  2. Tens bucket: 19
    Fifties bucket: 55 54 Nineties bucket: 91

  3. since Tens bucket and Nineties bucket just has one item, just handle Fifties bucket
    Four bucket: 54
    Five bucket: 55

  4. so final order is 19 54 55 91 right!

like image 41
Tim Avatar answered Sep 19 '22 03:09

Tim