Implementation of counting sort

Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
1
down vote
favorite
I'm trying to learn more about counting sort and I just implemented the example given in CLRS, my question is: How can I improve this code?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void counting_sort(int *, int, int);
int find_max(int *, int);
int main(void)
int l_size;
int i;
scanf("%d", &l_size);
int *num_list = calloc(l_size, sizeof(int));
for (i = 0; i < l_size; i++)
scanf("%d", &num_list[i]);
int max = find_max(num_list, l_size);
counting_sort(num_list, l_size, max);
puts("Sorted:");
for (i = 0; i < l_size; i++)
printf("%d ", num_list[i]);
printf("n");
return 0;
void counting_sort(int *num_list, int l_size, int max)
int i, j;
int *count_list = calloc(max + 1, sizeof(int)); // auxiliary array C
int *sorted_list = calloc(l_size, sizeof(int));
for (i = 0; i < l_size; i++)
count_list[num_list[i]]++;
for (i = 1; i < max + 1; i++)
count_list[i] += count_list[i - 1];
for (i = l_size - 1; i >= 0; i--)
sorted_list[count_list[num_list[i]] - 1] = num_list[i];
count_list[num_list[i]]--;
memcpy(num_list, sorted_list, l_size * sizeof(int));
int find_max(int *num_list, int l_size)
int i;
int max = num_list[0];
for (i = 1; i < l_size; i++)
if (num_list[i] > max)
max = num_list[i];
return max;
c sorting
add a comment |Â
up vote
1
down vote
favorite
I'm trying to learn more about counting sort and I just implemented the example given in CLRS, my question is: How can I improve this code?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void counting_sort(int *, int, int);
int find_max(int *, int);
int main(void)
int l_size;
int i;
scanf("%d", &l_size);
int *num_list = calloc(l_size, sizeof(int));
for (i = 0; i < l_size; i++)
scanf("%d", &num_list[i]);
int max = find_max(num_list, l_size);
counting_sort(num_list, l_size, max);
puts("Sorted:");
for (i = 0; i < l_size; i++)
printf("%d ", num_list[i]);
printf("n");
return 0;
void counting_sort(int *num_list, int l_size, int max)
int i, j;
int *count_list = calloc(max + 1, sizeof(int)); // auxiliary array C
int *sorted_list = calloc(l_size, sizeof(int));
for (i = 0; i < l_size; i++)
count_list[num_list[i]]++;
for (i = 1; i < max + 1; i++)
count_list[i] += count_list[i - 1];
for (i = l_size - 1; i >= 0; i--)
sorted_list[count_list[num_list[i]] - 1] = num_list[i];
count_list[num_list[i]]--;
memcpy(num_list, sorted_list, l_size * sizeof(int));
int find_max(int *num_list, int l_size)
int i;
int max = num_list[0];
for (i = 1; i < l_size; i++)
if (num_list[i] > max)
max = num_list[i];
return max;
c sorting
Avoidsizeot(TYPE)in favor ofsizeof expr: Repetition is error-prone.
â Deduplicator
Jun 10 at 14:31
Actually you donâÂÂt needsorted_listat all, since you are just sorting ints (the indices are equal to the data members). Simply writecount_list[i]times the valueiin thenum_listarray.
â Cris Luengo
Jun 16 at 13:36
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I'm trying to learn more about counting sort and I just implemented the example given in CLRS, my question is: How can I improve this code?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void counting_sort(int *, int, int);
int find_max(int *, int);
int main(void)
int l_size;
int i;
scanf("%d", &l_size);
int *num_list = calloc(l_size, sizeof(int));
for (i = 0; i < l_size; i++)
scanf("%d", &num_list[i]);
int max = find_max(num_list, l_size);
counting_sort(num_list, l_size, max);
puts("Sorted:");
for (i = 0; i < l_size; i++)
printf("%d ", num_list[i]);
printf("n");
return 0;
void counting_sort(int *num_list, int l_size, int max)
int i, j;
int *count_list = calloc(max + 1, sizeof(int)); // auxiliary array C
int *sorted_list = calloc(l_size, sizeof(int));
for (i = 0; i < l_size; i++)
count_list[num_list[i]]++;
for (i = 1; i < max + 1; i++)
count_list[i] += count_list[i - 1];
for (i = l_size - 1; i >= 0; i--)
sorted_list[count_list[num_list[i]] - 1] = num_list[i];
count_list[num_list[i]]--;
memcpy(num_list, sorted_list, l_size * sizeof(int));
int find_max(int *num_list, int l_size)
int i;
int max = num_list[0];
for (i = 1; i < l_size; i++)
if (num_list[i] > max)
max = num_list[i];
return max;
c sorting
I'm trying to learn more about counting sort and I just implemented the example given in CLRS, my question is: How can I improve this code?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void counting_sort(int *, int, int);
int find_max(int *, int);
int main(void)
int l_size;
int i;
scanf("%d", &l_size);
int *num_list = calloc(l_size, sizeof(int));
for (i = 0; i < l_size; i++)
scanf("%d", &num_list[i]);
int max = find_max(num_list, l_size);
counting_sort(num_list, l_size, max);
puts("Sorted:");
for (i = 0; i < l_size; i++)
printf("%d ", num_list[i]);
printf("n");
return 0;
void counting_sort(int *num_list, int l_size, int max)
int i, j;
int *count_list = calloc(max + 1, sizeof(int)); // auxiliary array C
int *sorted_list = calloc(l_size, sizeof(int));
for (i = 0; i < l_size; i++)
count_list[num_list[i]]++;
for (i = 1; i < max + 1; i++)
count_list[i] += count_list[i - 1];
for (i = l_size - 1; i >= 0; i--)
sorted_list[count_list[num_list[i]] - 1] = num_list[i];
count_list[num_list[i]]--;
memcpy(num_list, sorted_list, l_size * sizeof(int));
int find_max(int *num_list, int l_size)
int i;
int max = num_list[0];
for (i = 1; i < l_size; i++)
if (num_list[i] > max)
max = num_list[i];
return max;
c sorting
edited Jun 10 at 7:47
yuri
3,3872832
3,3872832
asked Jun 10 at 3:46
Trey
1084
1084
Avoidsizeot(TYPE)in favor ofsizeof expr: Repetition is error-prone.
â Deduplicator
Jun 10 at 14:31
Actually you donâÂÂt needsorted_listat all, since you are just sorting ints (the indices are equal to the data members). Simply writecount_list[i]times the valueiin thenum_listarray.
â Cris Luengo
Jun 16 at 13:36
add a comment |Â
Avoidsizeot(TYPE)in favor ofsizeof expr: Repetition is error-prone.
â Deduplicator
Jun 10 at 14:31
Actually you donâÂÂt needsorted_listat all, since you are just sorting ints (the indices are equal to the data members). Simply writecount_list[i]times the valueiin thenum_listarray.
â Cris Luengo
Jun 16 at 13:36
Avoid
sizeot(TYPE) in favor of sizeof expr: Repetition is error-prone.â Deduplicator
Jun 10 at 14:31
Avoid
sizeot(TYPE) in favor of sizeof expr: Repetition is error-prone.â Deduplicator
Jun 10 at 14:31
Actually you donâÂÂt need
sorted_list at all, since you are just sorting ints (the indices are equal to the data members). Simply write count_list[i] times the value i in the num_list array.â Cris Luengo
Jun 16 at 13:36
Actually you donâÂÂt need
sorted_list at all, since you are just sorting ints (the indices are equal to the data members). Simply write count_list[i] times the value i in the num_list array.â Cris Luengo
Jun 16 at 13:36
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
You can avoid modifying the input parameter num_list and the memcopy by returning the sorted list pointer. All relevant changes to your code are marked with // (1) in the code below.
You also have a memory leak in counting_sort since you don't release count_list. The fix is marked // (2).
Same for the lists in main // (3)
Note : The program also seg faults when negative data is entered. You either have to guard against that, or also find the minimum data value, allocate count_list big enough and fix the indices when accessing it.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int* counting_sort(int *, int, int); // (1)
int find_max(int *, int);
int main(void)
int l_size;
int i;
scanf("%d", &l_size);
int *num_list = calloc(l_size, sizeof(int));
for (i = 0; i < l_size; i++)
scanf("%d", &num_list[i]);
int max = find_max(num_list, l_size);
int* sorted = counting_sort(num_list, l_size, max); // (1)
puts("Sorted:");
for (i = 0; i < l_size; i++)
printf("%d ", sorted[i]); // (1)
printf("n");
free(sorted); // (3)
free(num_list); // (3)
return 0;
int* counting_sort(int *num_list, int l_size, int max) // (1)
int i, j;
int *count_list = calloc(max + 1, sizeof(int)); // auxiliary array C
int *sorted_list = calloc(l_size, sizeof(int));
for (i = 0; i < l_size; i++)
count_list[num_list[i]]++;
for (i = 1; i < max + 1; i++)
count_list[i] += count_list[i - 1];
for (i = l_size - 1; i >= 0; i--)
sorted_list[count_list[num_list[i]] - 1] = num_list[i];
count_list[num_list[i]]--;
free(count_list); // (2)
return sorted_list; // (1)
int find_max(int *num_list, int l_size)
int i;
int max = num_list[0];
for (i = 1; i < l_size; i++)
if (num_list[i] > max)
max = num_list[i];
return max;
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
You can avoid modifying the input parameter num_list and the memcopy by returning the sorted list pointer. All relevant changes to your code are marked with // (1) in the code below.
You also have a memory leak in counting_sort since you don't release count_list. The fix is marked // (2).
Same for the lists in main // (3)
Note : The program also seg faults when negative data is entered. You either have to guard against that, or also find the minimum data value, allocate count_list big enough and fix the indices when accessing it.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int* counting_sort(int *, int, int); // (1)
int find_max(int *, int);
int main(void)
int l_size;
int i;
scanf("%d", &l_size);
int *num_list = calloc(l_size, sizeof(int));
for (i = 0; i < l_size; i++)
scanf("%d", &num_list[i]);
int max = find_max(num_list, l_size);
int* sorted = counting_sort(num_list, l_size, max); // (1)
puts("Sorted:");
for (i = 0; i < l_size; i++)
printf("%d ", sorted[i]); // (1)
printf("n");
free(sorted); // (3)
free(num_list); // (3)
return 0;
int* counting_sort(int *num_list, int l_size, int max) // (1)
int i, j;
int *count_list = calloc(max + 1, sizeof(int)); // auxiliary array C
int *sorted_list = calloc(l_size, sizeof(int));
for (i = 0; i < l_size; i++)
count_list[num_list[i]]++;
for (i = 1; i < max + 1; i++)
count_list[i] += count_list[i - 1];
for (i = l_size - 1; i >= 0; i--)
sorted_list[count_list[num_list[i]] - 1] = num_list[i];
count_list[num_list[i]]--;
free(count_list); // (2)
return sorted_list; // (1)
int find_max(int *num_list, int l_size)
int i;
int max = num_list[0];
for (i = 1; i < l_size; i++)
if (num_list[i] > max)
max = num_list[i];
return max;
add a comment |Â
up vote
2
down vote
accepted
You can avoid modifying the input parameter num_list and the memcopy by returning the sorted list pointer. All relevant changes to your code are marked with // (1) in the code below.
You also have a memory leak in counting_sort since you don't release count_list. The fix is marked // (2).
Same for the lists in main // (3)
Note : The program also seg faults when negative data is entered. You either have to guard against that, or also find the minimum data value, allocate count_list big enough and fix the indices when accessing it.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int* counting_sort(int *, int, int); // (1)
int find_max(int *, int);
int main(void)
int l_size;
int i;
scanf("%d", &l_size);
int *num_list = calloc(l_size, sizeof(int));
for (i = 0; i < l_size; i++)
scanf("%d", &num_list[i]);
int max = find_max(num_list, l_size);
int* sorted = counting_sort(num_list, l_size, max); // (1)
puts("Sorted:");
for (i = 0; i < l_size; i++)
printf("%d ", sorted[i]); // (1)
printf("n");
free(sorted); // (3)
free(num_list); // (3)
return 0;
int* counting_sort(int *num_list, int l_size, int max) // (1)
int i, j;
int *count_list = calloc(max + 1, sizeof(int)); // auxiliary array C
int *sorted_list = calloc(l_size, sizeof(int));
for (i = 0; i < l_size; i++)
count_list[num_list[i]]++;
for (i = 1; i < max + 1; i++)
count_list[i] += count_list[i - 1];
for (i = l_size - 1; i >= 0; i--)
sorted_list[count_list[num_list[i]] - 1] = num_list[i];
count_list[num_list[i]]--;
free(count_list); // (2)
return sorted_list; // (1)
int find_max(int *num_list, int l_size)
int i;
int max = num_list[0];
for (i = 1; i < l_size; i++)
if (num_list[i] > max)
max = num_list[i];
return max;
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
You can avoid modifying the input parameter num_list and the memcopy by returning the sorted list pointer. All relevant changes to your code are marked with // (1) in the code below.
You also have a memory leak in counting_sort since you don't release count_list. The fix is marked // (2).
Same for the lists in main // (3)
Note : The program also seg faults when negative data is entered. You either have to guard against that, or also find the minimum data value, allocate count_list big enough and fix the indices when accessing it.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int* counting_sort(int *, int, int); // (1)
int find_max(int *, int);
int main(void)
int l_size;
int i;
scanf("%d", &l_size);
int *num_list = calloc(l_size, sizeof(int));
for (i = 0; i < l_size; i++)
scanf("%d", &num_list[i]);
int max = find_max(num_list, l_size);
int* sorted = counting_sort(num_list, l_size, max); // (1)
puts("Sorted:");
for (i = 0; i < l_size; i++)
printf("%d ", sorted[i]); // (1)
printf("n");
free(sorted); // (3)
free(num_list); // (3)
return 0;
int* counting_sort(int *num_list, int l_size, int max) // (1)
int i, j;
int *count_list = calloc(max + 1, sizeof(int)); // auxiliary array C
int *sorted_list = calloc(l_size, sizeof(int));
for (i = 0; i < l_size; i++)
count_list[num_list[i]]++;
for (i = 1; i < max + 1; i++)
count_list[i] += count_list[i - 1];
for (i = l_size - 1; i >= 0; i--)
sorted_list[count_list[num_list[i]] - 1] = num_list[i];
count_list[num_list[i]]--;
free(count_list); // (2)
return sorted_list; // (1)
int find_max(int *num_list, int l_size)
int i;
int max = num_list[0];
for (i = 1; i < l_size; i++)
if (num_list[i] > max)
max = num_list[i];
return max;
You can avoid modifying the input parameter num_list and the memcopy by returning the sorted list pointer. All relevant changes to your code are marked with // (1) in the code below.
You also have a memory leak in counting_sort since you don't release count_list. The fix is marked // (2).
Same for the lists in main // (3)
Note : The program also seg faults when negative data is entered. You either have to guard against that, or also find the minimum data value, allocate count_list big enough and fix the indices when accessing it.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int* counting_sort(int *, int, int); // (1)
int find_max(int *, int);
int main(void)
int l_size;
int i;
scanf("%d", &l_size);
int *num_list = calloc(l_size, sizeof(int));
for (i = 0; i < l_size; i++)
scanf("%d", &num_list[i]);
int max = find_max(num_list, l_size);
int* sorted = counting_sort(num_list, l_size, max); // (1)
puts("Sorted:");
for (i = 0; i < l_size; i++)
printf("%d ", sorted[i]); // (1)
printf("n");
free(sorted); // (3)
free(num_list); // (3)
return 0;
int* counting_sort(int *num_list, int l_size, int max) // (1)
int i, j;
int *count_list = calloc(max + 1, sizeof(int)); // auxiliary array C
int *sorted_list = calloc(l_size, sizeof(int));
for (i = 0; i < l_size; i++)
count_list[num_list[i]]++;
for (i = 1; i < max + 1; i++)
count_list[i] += count_list[i - 1];
for (i = l_size - 1; i >= 0; i--)
sorted_list[count_list[num_list[i]] - 1] = num_list[i];
count_list[num_list[i]]--;
free(count_list); // (2)
return sorted_list; // (1)
int find_max(int *num_list, int l_size)
int i;
int max = num_list[0];
for (i = 1; i < l_size; i++)
if (num_list[i] > max)
max = num_list[i];
return max;
edited Jun 10 at 14:33
answered Jun 10 at 9:44
Gregor Ophey
1564
1564
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f196207%2fimplementation-of-counting-sort%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Avoid
sizeot(TYPE)in favor ofsizeof expr: Repetition is error-prone.â Deduplicator
Jun 10 at 14:31
Actually you donâÂÂt need
sorted_listat all, since you are just sorting ints (the indices are equal to the data members). Simply writecount_list[i]times the valueiin thenum_listarray.â Cris Luengo
Jun 16 at 13:36