Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. #include<fstream>
  2. #include<string.h>
  3.  
  4. using namespace std;
  5. ifstream fin("radixsort.in");
  6. ofstream fout("radixsort.out");
  7.  
  8. int maxi,n,m,a[1000001],b[10],c[1000001];
  9.  
  10. int nrcifre(int x)
  11. {
  12. if(x==0) return 0;
  13. int nr=0;
  14. while(x)
  15. {
  16. nr++;
  17. x/=10;
  18. }
  19. return nr;
  20. }
  21.  
  22. void atribuire(int a[], int c[])
  23. {
  24. for(int i=1;i<=n;i++) a[i]=c[i];
  25. }
  26.  
  27. void countsort(int p)
  28. {
  29. memset(b,0,sizeof(b));
  30. for(int i=1;i<=n;i++) b[(a[i]/p)%10]++;
  31. for(int i=1;i<=9;i++) b[i]=b[i-1]+b[i];
  32. for(int i=n;i>0;i--) c[b[(a[i]/p)%10]--]=a[i];
  33. }
  34.  
  35. void radix(int nr)
  36. {
  37. int p10=1;
  38. for(int i=1;i<=nr;i++)
  39. {
  40. countsort(p10);
  41. atribuire(a,c);
  42. p10*=10;
  43. }
  44. }
  45.  
  46. int main()
  47. {
  48. fin>>n;
  49. maxi=0;
  50. for(int i=1;i<=n;i++)
  51. {
  52. fin>>a[i];
  53. if(maxi<a[i]) maxi=a[i];
  54. }
  55. int nr=nrcifre(maxi);
  56. radix(nr);
  57. for(int i=1;i<=n;i++) fout<<a[i]<<" ";
  58. return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement