Implementation of counting sort

The name of the pictureThe name of the pictureThe name of the pictureClash 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;







share|improve this question





















  • 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
















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;







share|improve this question





















  • 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












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;







share|improve this question













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;









share|improve this question












share|improve this question




share|improve this question








edited Jun 10 at 7:47









yuri

3,3872832




3,3872832









asked Jun 10 at 3:46









Trey

1084




1084











  • 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
















  • 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















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










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;






share|improve this answer























    Your Answer




    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("mathjaxEditing", function ()
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    );
    );
    , "mathjax-editing");

    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "196"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    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






























    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;






    share|improve this answer



























      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;






      share|improve this answer

























        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;






        share|improve this answer















        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;







        share|improve this answer















        share|improve this answer



        share|improve this answer








        edited Jun 10 at 14:33


























        answered Jun 10 at 9:44









        Gregor Ophey

        1564




        1564






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            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













































































            Popular posts from this blog

            Python Lists

            Aion

            JavaScript Array Iteration Methods