Advertisement
bubuzaka

3465

Feb 22nd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. void qsort (int *a1, int l, int r, int n);
  6. void printmas (int *a1, int n);
  7. void change (int *a, int *b);
  8. static int c=0;
  9.  
  10. int main ()
  11. {
  12. int n;
  13. cout<< "Input n (number of elements):"<< endl;
  14. cin>> n;
  15. int a[n];
  16. cout<< "Inpunt mass elements:"<< endl;
  17. for (int i=0; i<n; i++)
  18. cin>> a[i];
  19. cout<< "Mass before sort:"<<endl;
  20. printmas (a, n);
  21. cout<< endl<< "Mass durung sorting:"<<endl;
  22. qsort (a, 0, n-1, n);
  23. cout<< "Number of changes: "<<endl<<c<<endl<<"Mass after sort:"<<endl;
  24. printmas (a, n);
  25. }
  26.  
  27. void printmas (int *a1, int n) //?-?, ????????? ?? ????? ??-?? ???????
  28. {
  29. for (int i=0; i<n; i++)
  30. cout<< a1[i]<< " ";
  31. }
  32.  
  33. void qsort ( int *a1, int l, int r, int n)
  34. {
  35. int fir=l; //???????? ?? ?????? ???????
  36. int las=r; //????????? ?? ????? ???????
  37. int mid=a1[(l+r)/2]; //????. ??-?
  38. while (l<r)
  39. {
  40. while (a1[l]<mid)
  41. l++; //???????? ????? ?????????, ???? ??-? ?????? ????.
  42. while (a1[r]>mid)
  43. r--; //???????? ?????? ?????????, ???? ??-? ?????? ????.
  44. if (l<=r)
  45. {
  46. if (a1[l]>a1[r]) //???? ????? ?????? ???????
  47. {
  48. change (&a1[l], &a1[r]); //?????? ??-?? ???????
  49. printmas (a1, n); //?????? ???????
  50. cout<< endl;
  51. }
  52. l++; r--; //???????? ?????????
  53. }
  54. if (l>fir)
  55. qsort (a1, fir, r, n); //???? ????? ????????? ?????????, ?? ???????? ?????????? ??????? ????? ?? ????. ??-??
  56. if (r<las)
  57. qsort (a1, l, las, n); //???? ?????? ????????? ?????????, ?? ???????? ?????????? ??????? ?????? ?? ????. ??-??
  58. }
  59.  
  60. }
  61.  
  62. void change (int *a, int *b) //?-?, ???????? ??-?? ?????? ???????
  63. {
  64. int k = *a;
  65. *a=*b;
  66. *b = k;
  67. c++;
  68. cout<<c<<" "<<endl;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement