Advertisement
mehedi1

counting sort.cpp

Jul 14th, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.50 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void count_sort(int A[], int n) {
  5.     int mx = 0;
  6.     for(int i=0;i<n;++i) {
  7.         if(A[i]>mx) mx = A[i];
  8.     }
  9.     int cnt[mx+5]={0};
  10.     for(int i=0;i<n;++i) cnt[A[i]]++;
  11.     int j=0;
  12.     for(int i=0;i<=mx;++i) {
  13.         int tmp = cnt[i];
  14.         while(tmp--) {
  15.             A[j++] = i;
  16.         }
  17.     }
  18. }
  19. int main() {
  20.     int a,b,n,T,cas=0;
  21.     cin>>n;
  22.     int A[n];
  23.     for(int i=0;i<n;++i) cin>>A[i];
  24.     count_sort(A, n);
  25.     for(int i=0;i<n;++i) cout<<A[i]<<" ";
  26.     return 0;
  27. }
  28.  
  29. /*
  30. 7
  31. 5 2 9 5 2 3 5
  32. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement