Radix Sort Base 16 (Hexadecimals)

前端 未结 4 1319
予麋鹿
予麋鹿 2020-12-21 14:13

I have spent more 10hr+ on trying to sort the following(hexadecimals) in LSD radix sort, but no avail. There is very little material on this subject on web.

4条回答
  •  失恋的感觉
    2020-12-21 14:49

    void int_radix_sort(void) {
        int group; //because extracting 8 bits
        int buckets = 1 << 8; //using size 256
        int map[buckets];   
        int mask = buckets - 1;
        int i;
        int cnt[buckets];
        int flag = NULL;
        int partition;
        int *src, *dst;
    
        for (group = 0; group < 32; group += 8) {
            // group = 8, number of bits we want per round, we want 4 rounds
            // cnt  
            for (int i = 0; i < buckets; i++) {
                cnt[i] = 0;
            }
            for (int j = 0; j < n; j++) {
                i = (lst[j] >> group) & mask;
                cnt[i]++; 
                tmp[j] = lst[j];
            }
    
            //map
            map[0] = 0;
            for (int i = 1; i < buckets; i++) {
                map[i] = map[i - 1] + cnt[i - 1];
            }
    
            //move
            for (int j = 0; j < n; j++) {   
                i = (tmp[j] >> group) & mask;
                lst[map[i]] = tmp[j];
                map[i]++;
            }
        }
    }
    

    After hours of researching I came across the answer. I'm still do not understand what is going on in this code/answer. I cannot get my head wrapped around the concept. Hopefully, someone can explain.

提交回复
热议问题