Most significant vs least significant sort

If I just need to sort strings consisting of ASCII characters, I wonder what are the differences between using the most significant vs the least significant sorting by radius? I think they should have the same results, but are confused by the following statement from the link below, and if someone can help clarify, that would be great.

https://en.wikipedia.org/wiki/Radix_sort

To sort keys in lexicographical order, you can use radix sort of the most significant digit (MSD). Unlike the least significant digit radix (LSD) sort, the most significant digit sort does not necessarily preserve the original order of the duplicate keys.

thanks in advance Lin

+3
source share
2 answers

The LSD radix sort can logically combine the sorted bins after each pass (count them as one bunker if counting / radix sorting is used). The MSD radix variety must recursively sort each bit independently after each pass. If sorted by bytes, 256 bins after the first pass, 65536 boxes after the second pass, 16777216 (16 million) boxes after the third pass, ....

LSD. . . "0" "9", "0", 1 () , - .., "" . , , , , .

http://www.youtube.com/watch?v=jJH2alRcx4M

++ LSD radix sort 32- , "" . , , . .

//  a is input array, b is working array
uint32_t * RadixSort(uint32_t * a, uint32_t *b, size_t count)
{
size_t mIndex[4][256] = {0};            // count / index matrix
size_t i,j,m,n;
uint32_t u;
    for(i = 0; i < count; i++){         // generate histograms
        u = a[i];
        for(j = 0; j < 4; j++){
            mIndex[j][(size_t)(u & 0xff)]++;
            u >>= 8;
        }       
    }
    for(j = 0; j < 4; j++){             // convert to indices
        m = 0;
        for(i = 0; i < 256; i++){
            n = mIndex[j][i];
            mIndex[j][i] = m;
            m += n;
        }       
    }
    for(j = 0; j < 4; j++){             // radix sort
        for(i = 0; i < count; i++){     //  sort by current lsb
            u = a[i];
            m = (size_t)(u>>(j<<3))&0xff;
            b[mIndex[j][m]++] = u;
        }
        std::swap(a, b);                //  swap ptrs
    }
    return(a);
}
+5

, , , LSD radix . , , . , 2 , , :

22        21        11
21   ->   11   ->   21
11        22        22

, , , , 21 22 , 10- . ( ) , ( ?), .

radix MSD , radix LSD, . , radix MSD, .

MSD, , , .. .

, , , ASCII. " " , . , , , , "Abe" "abE" , .

+2

Source: https://habr.com/ru/post/1629652/


All Articles