We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Apply
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Sorting
  4. The Full Counting Sort
  5. Discussions

The Full Counting Sort

Problem
Submissions
Leaderboard
Discussions

    You are viewing a single comment's thread. Return to all comments →

  • mohamed_ali_oba1
    8 months ago+ 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");
        
    }
    
    0|
    Permalink
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy