fork(7) download
  1. #include<iostream>
  2. #include<stdlib.h>
  3. using namespace std;
  4.  
  5. void counting_sort(int [], int, int);
  6.  
  7. main()
  8. {
  9. int n,k = 0, a[15];
  10. cout << "Enter the number of input : ";
  11. cin >> n;
  12. cout << "\nEnter the elements to be sorted :\n";
  13. for ( int i = 1; i <= n; i++)
  14. {
  15. cin >> a[i];
  16. if(a[i] > k)
  17. {
  18. k = a[i];
  19. }
  20. }
  21. counting_sort(a, k, n);
  22. system("pause");
  23. //getch();
  24. }
  25.  
  26. void counting_sort(int a[], int k, int n)
  27. {
  28. int i, j;
  29. int b[15], c[100];
  30. for(i = 0; i <= k; i++)
  31. c[i] = 0;
  32.  
  33. for(j =1; j <= n; j++)
  34. c[a[j]] = c[a[j]] + 1;
  35.  
  36. for(i = 1; i <= k; i++)
  37. c[i] = c[i] + c[i-1];
  38.  
  39. for(j = n; j >= 1; j--)
  40. {
  41. b[c[a[j]]] = a[j];
  42. c[a[j]] = c[a[j]] - 1;
  43. }
  44. cout << "\nThe Sorted array is : ";
  45. for(i = 1; i <= n; i++)
  46. cout << b[i] << " " ;
  47. }
  48.  
Success #stdin #stdout #stderr 0s 3100KB
stdin
5
4 1 8 2 0
stdout
Enter the number of input : 
Enter the elements to be sorted :

The Sorted array is : 0 1 2 4 8 
stderr
sh: 1: pause: not found