Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. int tab[10]={1,2,13,3,15,4,6,13,5,5};
  4. void swap(int *tab, int i, int j)
  5. {
  6. int tmp=tab[i];
  7. tab[i]=tab[j];
  8. tab[j]=tmp;
  9. }
  10. void fix_table(int *tab, int pivot, int l, int p)
  11. {
  12. while(l<p)
  13. {
  14. while(tab[l]==pivot)l++;
  15. while(tab[p]!=pivot)p--;
  16. swap(tab,p,l);
  17. l++;
  18. p--;
  19. }
  20. }
  21. int partition(int *tab, int l, int p, int &counter)
  22. {
  23. int i = l-1;
  24. int pivot = tab[p];
  25. for (int j=l; j<p; j++)
  26. {
  27. if(tab[j]<pivot)
  28. {
  29. i++;
  30. swap(tab,i,j);
  31. }
  32. else if(tab[j]==pivot)counter++;
  33. }
  34. i++;
  35. swap(tab,i, p);
  36. fix_table(tab,tab[i],i,p);
  37. return i;
  38. }
  39. void quick_sort(int *tab, int l, int p)
  40. {
  41. int counter=0;
  42. if(l<p)
  43. {
  44. int q = partition(tab, l, p, counter);
  45. quick_sort(tab, l, q-1);
  46. quick_sort(tab,q+counter+1, p);
  47. }
  48. }
  49. int main() {
  50. quick_sort(tab,0,9);
  51. int cou=0;
  52. // partition(tab,0,9,cou);
  53. cout << endl;
  54. for(int i : tab)cout << i << endl;
  55. return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement