Radix sort: LSD versus MSD versions

后端 未结 3 1076
失恋的感觉
失恋的感觉 2021-02-04 02:36

The book \"Introduction to Algorithms\" mentions about the LSD (Least Significant Digit) version of radix sort. However , as others have pointed out here in stackoverflow, a MSD

相关标签:
3条回答
  • 2021-02-04 02:45

    LSD is faster than MSD when there is a fixed length. MSD is too slow for small files, and it need huge number of recursive calls on small files.

    0 讨论(0)
  • 2021-02-04 03:01

    As read in the book Algorithms, LSD and MSD are both string array sorting algorithms, based on the so-called key indexed counting rather than on comparisons. Therefore, LSD and MSD have a different running time versus traditional quick sort or merge sort.

    As Dzhabarov mentioned, the biggest difference between LSD and MSD is that MSD considers the most significant digit or character first, which by nature sorts strings without iterating through all of the digits in the strings. This is an advantage. However, a recursive implementation of MSD uses more space than LSD.

    The table below illustrates parts of the difference among quick sort, LSD and MSD.

    algorithm    running time              extra space
    quicksort    N(lgN)^2                  1
    LSD          NW                        N
    MSD          between N and NW          N + WR
    

    where N is the length of array, and W is the length of string, and R is size of radix.

    PS: as mentioned in the book, the Java system sort uses a general sorting algorithm with fast string comparison and not LSD or MSD.

    0 讨论(0)
  • 2021-02-04 03:10

    Taken from the link, might be useful: http://www.eternallyconfuzzled.com/tuts/algorithms/jsw_tut_sorting.aspx (At the very bottom)

    The biggest problem with LSD radix sort is that it starts at the digits that make the least difference. If we could start with the most significant digits, the first pass would go a long way toward sorting the entire range, and each pass after that would simply handle the details. The idea of MSD radix sorting is to divide all of the digits with an equal value into their own bucket, then do the same thing with all of the buckets until the array is sorted. Naturally, this suggests a recursive algorithm, but it also means that we can now sort variable length items and we don't have to touch all of the digits to get a sorted array. This makes MSD radix sort considerably faster and more useful.

    0 讨论(0)
提交回复
热议问题