6

I'm not sure why would one ever use LSD radix sort.

Advantages of MSD:

  1. It can handle strings of variable length
  2. It doesn't always need to scan the entire strings (it ca decide sooner about the order)
  3. One can use insertion sort to circumvent the disadvantages of counting sort.
templatetypedef
  • 328,018
  • 92
  • 813
  • 992
user1377000
  • 1,313
  • 2
  • 14
  • 27
  • Consider sorting integers in numerical order rather than strings in lexicographical order. For more detailed responses, you should probably try http://cstheory.stackexchange.com/ – beaker Jan 12 '14 at 17:22
  • @beaker- cstheory.stackexchange.com is for research-level CS questions. I don't think this would be appropriate there. – templatetypedef Jan 12 '14 at 17:23
  • @templatetypedef Bad cut and paste job, sorry. I was going for http://cs.stackexchange.com/ which for some reason doesn't appear in the handy list at the bottom of the page. – beaker Jan 13 '14 at 00:40

3 Answers3

7

One advantage of LSD radix sort over MSD radix sort is that LSD radix sort is a stable sort - if there are multiple elements to sort with the same key, they'll end up in the same relative order in the sorted output when you run LSD radix sort, but might not if you run MSD radix sort. If you're sorting key/value pairs where the key is a string or an integer and you want to preserve the original relative ordering, LSD radix sort would be preferable over MSD radix sort.

Hope this helps!

templatetypedef
  • 328,018
  • 92
  • 813
  • 992
  • Why MSD radix is not a stable sorting algorithm ? Even here we use counting sort(which is stable) to sort the digits but from MSB right ? – Zephyr Nov 09 '17 at 15:45
  • 1
    @Zephyr Some implementations of MSD radix sort - especially binary MSD radix sort - use an unstable partitioning strategy like what quicksort does. You could do it stably, but it’s not required. Look up “binary quicksort” for more on this. – templatetypedef Nov 09 '17 at 17:00
  • Yes true that if we use quicksort then it would be unstable. But can you tell me why can't we use counting sort to sort the elements whose MSB is same inside the bucket? – Zephyr Nov 09 '17 at 17:13
  • Oh, you totally could. I just haven’t seen many implementations that do. – templatetypedef Nov 09 '17 at 17:23
  • So if we use counting sort inside the bucket then will there be any change in total time complexity ? If no change, then why do many implementations dont use it as counting sort ensures stability and quicksort doesn't. – Zephyr Nov 09 '17 at 17:32
  • 1
    IIRC counting sort does change the time complexity if you use it in MSD radix sort, since the cost of scanning over each bucket starts to get factored in more and more as the recursion progresses (each recursive branch incurs the cost of the bucket scan). – templatetypedef Nov 09 '17 at 17:42
  • If possible can you just tell me if the below recurrence relation in case of MSD in radix sort $T(n) = bT(n/b) + n$ where b stands for base using counting sort is correct or not ? – Zephyr Nov 09 '17 at 18:26
  • 1
    You need to include a “+ b” term to account for the cost of iterating over all the buckets. That +b term will change what this recurrence solves to. Also, you cannot assume each subproblem has size n / b, since you can’t assume that each digit is equally represented. It could be that some digits appear overwhelmingly more commonly than others. Also, this recurrence needs to factor in the number of base-b digits in your numbers, since after that many iterations the algorithm is guaranteed to terminate. – templatetypedef Nov 09 '17 at 18:37
  • Then what would be appropriate recurrence relation to estimate the time complexity? I am not able to estimate the worst case time complexity of MSD radix using counting sort. – Zephyr Nov 09 '17 at 18:59
  • I don't know of a simple recurrence relation you could use here. It might be best to work out a worst-case input and a best-case input to get a sense of the time bounds. (Also, this might be best posted as an independent question!) – templatetypedef Nov 09 '17 at 19:03
  • I asked the question here https://cs.stackexchange.com/q/83687/63873 . Let's hope someone replies. – Zephyr Nov 09 '17 at 19:11
1

@templatetypedef has summed it beautifully .
MSD radix sort is useful to sort keys in lexicographic order .
take a look at wikipedia for working examples and clearer info.

Aseem Goyal
  • 2,516
  • 2
  • 25
  • 43
  • 1
    Thanks anon. Well, the lexicographic order for integers is the natural ordering. I don't think there's a difference in what the two radix sort variants are trying to achieve. – user1377000 Jan 13 '14 at 08:25
  • @user1377000 yes , basically both perform the similarly on integers and strings, provided started from same direction . Generally lexicographic ordering is achieved from left to right , and to sort in non-decreasing order follow from right to left . – Aseem Goyal Jan 13 '14 at 09:37
1

Biggest advantage of LSD radix sort for me is it speed because it is branch-free algorithm. It makes LSD radix sort fastest possible sort algorithm for relatively short fixed length keys. The stability of LSD is also nice feature.

Hynek -Pichi- Vychodil
  • 25,511
  • 5
  • 49
  • 71