Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. #include<iostream>
  2. #include<fstream>
  3. using namespace std;
  4. int k=0;
  5.  
  6. void countingSort(int A[],int B[],int n)
  7. {
  8. int C[k];
  9. for(int i=0;i<k+1;i++)
  10. {
  11. C[i]=0;
  12. }
  13. for(int j=1;j<=n;j++)
  14. {
  15. C[A[j]]++;
  16. }
  17. for(int i=1;i<=k;i++)
  18. {
  19. C[i]+=C[i-1];
  20. }
  21. for(int j=n;j>=1;j--)
  22. {
  23. B[C[A[j]]]=A[j];
  24. C[A[j]]=C[A[j]]-1;
  25. }
  26. }
  27.  
  28. void romanSort(int A[],int B[],int n)
  29. {
  30. int C[k];
  31. for(int i=0;i<k+1;i++)
  32. {
  33. C[i]=0;
  34. }
  35. for(int j=1;j<=n;j++)
  36. {
  37. C[A[j]]++;
  38. }
  39. for(int i=1;i<=k;i++)
  40. {
  41. if(C[i]!=0)
  42. {
  43. B[i] = (C[i] * i);
  44. cout<<B[i]<<" ";
  45. }
  46. }
  47. }
  48.  
  49. int main()
  50. {
  51. ofstream data;
  52. data.open("vlez.txt");
  53. int n,izbor;
  54. cout<<"vnesi golemina na nizata"<<endl;
  55. cin>>n;
  56. int A[n],B[n];
  57. cout<<"Vnesi elementi na nizata"<<endl;
  58. for(int i=1;i<=n;i++)
  59. {
  60. cin>>A[i];
  61. data << A[i] << " ";
  62. if(A[i]>k)
  63. {
  64. k=A[i];
  65. }
  66. }
  67. data.close();
  68. ofstream data1;
  69. data1.open("izlez.txt");
  70. cout<<"Izberi sort (0-Counting sort, 1-Roman Sort): ";
  71. cin>>izbor;
  72. if(izbor==0)
  73. {
  74. countingSort(A,B,n);
  75. for(int i=1;i<=n;i++)
  76. {
  77. cout<<B[i]<<" ";
  78. data1 << B[i] << " ";
  79. }
  80. }
  81. else if(izbor==1)
  82. {
  83. romanSort(A,B,n);
  84.  
  85. }
  86. data1.close();
  87. cout<<endl;
  88. return 0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement