HackerRank: Counting Sort 1 Solution in C

Another sorting method, the counting sort, does not require comparison. Instead, you create an integer array whose index range covers the entire range of values in your array to sort. Each time a value occurs in the original array, you increment the counter at that index. In the end, run through your counting array, printing the value of each non-zero valued index that a number of times.

int* countingSort(int arr_count, int* arr, int* result_count) {
    int *a =(int *)calloc(100, sizeof(int));

    int count=0;
    for(int i=0; i<arr_count; i++){
    a[arr[i]]+=1;
    }
    
    *result_count = 100;
    return a;
}

Leave a Comment