The Full Counting Sort

  • + 0 comments

    Hope this helps, this is my solution in C

    void countSort(int arr_rows, int arr_columns, char*** arr) {
        int i;
        int range[100] = {0};
        int new_range[100] = {0};
        char ** res = (char**)malloc(sizeof(char) * 10 * arr_rows);
        
        
        // step 0: replace first half with '-'
        for (i = 0; i < arr_rows / 2; i++)
            strcpy(arr[i][1], "-");
        
        
        // step 1: populating range
        for (i = 0; i < arr_rows; i++){
            int num = atoi(arr[i][0]);
            range[num]++;
        }
        // step 2: inctremting range
        for (i = 1; i < 100; i++)
            range[i] += range[i - 1];
        // step 3: shifting range array by 1
        for (i = 1; i < 100; i++)
            new_range[i] = range[i - 1];
        
        // step 4: sort
        for (i = 0; i < arr_rows; i++){
            int num = atoi(arr[i][0]);
            int r_i = new_range[num];
            res[r_i] = arr[i][1];
            new_range[num]++;
        }
        
        // step 5: print sorting result
        for (i = 0; i < arr_rows; i++)
            printf("%s ", res[i]);
        
        free(res);
        printf("\n");
        
    }