Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. /* Name: Abu Hurayra
  2. Handle: HurayraIIT
  3. Institute: IIT_JU
  4. Date: 20/3/2019 */
  5.  
  6. #include <bits/stdc++.h>
  7. using namespace std;
  8.  
  9. typedef long long LL;
  10. typedef string S;
  11. typedef double D;
  12. typedef vector<int> vi;
  13. typedef stack<int> si;
  14. typedef queue<int> qi;
  15. typedef priority_queue<int> pqi;
  16. typedef pair<int,int> pi;
  17.  
  18. #define pi acos(-1.0)
  19. #define PSB push_back
  20. #define PPB pop_back
  21. #define REP(i,a,b) for(i=a;i<=b;i++)
  22.  
  23. void CountSort(int a[] , int n , int max);
  24. void PrintSortedArray(int a[] , int n);
  25.  
  26. int main()
  27. {
  28. #ifndef ONLINE_JUDGE
  29. freopen("input.txt","r",stdin);
  30. freopen("output.txt","w",stdout);
  31. #endif
  32.  
  33. int n, i, j, max = -1;
  34. cin >> n;
  35. int a[n];
  36. for(i = 0 ; i < n ; i++)
  37. {
  38. cin >> a[i];
  39. if(a[i] > max) max = a[i];
  40. }
  41. CountSort(a , n , max);
  42. PrintSortedArray(a , n);
  43.  
  44. return 0;
  45. }
  46.  
  47. void CountSort(int a[] , int n , int max)
  48. {
  49. int count[max+10] = {0} , i , j = 0 , k = 0 ;
  50. for(i = 0 ; i < n ; i++)
  51. {
  52. count[a[i]]++;
  53. }
  54. for(i = 0 ; i <= max+5 ; i++)
  55. {
  56. while(count[i]--)
  57. {
  58. a[j++] = i ;
  59. }
  60. }
  61. return;
  62. }
  63.  
  64. void PrintSortedArray(int a[] , int n)
  65. {
  66. for(int i = 0 ; i < n ; i++) cout << a[i] << " ";
  67. cout << endl;
  68. return;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement