Radix sort vs Counting sort vs Bucket sort. What's the difference?

前端 未结 7 1138
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-29 21:50

I am reading the definitions of radix, counting and bucket sorts and it seems that all of them are just the code below:

public static void sort(int[] a, int maxV         


        
7条回答
  •  北海茫月
    2021-01-29 22:06

    To sort an array using count sort:

    #define MAX_INPUT 1000
    
    void sort(int arr[100], int n)
    {
        static int hash[MAX_INPUT], i, j;
    
        memset(hash, 0, sizeof hash);
    
        for (i = 0; i < n; ++i) ++hash[arr[i]];
    
        j = 0;
        for (i = 0; i < MAX_INPUT; ++i)
            while (hash[i]--)
               arr[j++] = i;
    }
    

    This is just O(MAX_INPUT), thus sorting in linear time. For bucket sort, it is very different. Here is an implementation

提交回复
热议问题