Advertisement
tanvirs2

counting_sort

Apr 1st, 2015
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. #include<cstdio>
  2. #include<cstring>
  3. using namespace std;
  4.  
  5. #define size 10000
  6.  
  7. struct count_sort
  8. {
  9. int ara[size],counter[size],sorted[size];
  10. int i,j,k;
  11.  
  12. void take_input(int n)
  13. {
  14. for(i=0;i<n;i++)
  15. scanf("%d",ara+i);
  16. memset(counter,0,sizeof(counter));
  17. }
  18.  
  19. void sorting(int n)
  20. {
  21. for(i=0;i<n;i++)
  22. {
  23. counter[ara[i]]++;
  24. }
  25.  
  26. for(i=0,k=0;i<size;i++)
  27. {
  28. while(counter[i]--)
  29. {
  30. sorted[k++]=i;
  31. }
  32. }
  33. }
  34.  
  35. void print_out(int n)
  36. {
  37. for(i=0;i<n;i++)
  38. printf("%d ",sorted[i]);
  39. puts("");
  40. }
  41.  
  42. };
  43.  
  44. int main()
  45. {
  46. int n;
  47. count_sort A;
  48.  
  49. scanf("%d",&n);
  50. A.take_input(n);
  51. A.sorting(n);
  52. A.print_out(n);
  53.  
  54. return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement