Advertisement
kuczi55

Untitled

Mar 11th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. //counting sort
  2.  
  3. #include <iostream>
  4. #include <windows.h>
  5. #include <time.h>
  6.  
  7. using namespace std;
  8.  
  9. const int N=256;
  10.  
  11. void counting(int A[])
  12. {
  13. int*B=new int[N*N-1];
  14. int*C=new int[N];
  15. for(int i=0; i<N; i++)
  16. B[i]=0;
  17. for(int i=0; i<N; i++)
  18. B[A[i]]++;
  19. for(int i=1; i<N*N-1; i++)
  20. B[i]+=B[i-1];
  21. for(int i=N-1; i>=0; i--)
  22. {
  23. B[A[i]]--;
  24. C[B[A[i]]]=A[i];
  25. }
  26. for(int i=0; i<N; i++)
  27. A[i]=C[i];
  28. delete[]B;
  29. delete[]C;
  30. }
  31.  
  32. int main()
  33. {
  34. srand(time(NULL));
  35. int t[N];
  36. for(int i=0; i<N; i++)
  37. t[i]=rand()%65535;
  38. for(int i=0; i<N; i++)
  39. cout << t[i] << " ";
  40. cout << endl;
  41. cout << endl;
  42. counting(t);
  43. for(int i=0; i<N; i++)
  44. cout << t[i] << " ";
  45. return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement