Advertisement
Guest User

qsort

a guest
Nov 14th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<math.h>
  4. #pragma warning(disable:4996)
  5. #define LEN 10
  6. //sortowanie szybkie 2 podejście
  7.  
  8. void qsort(int tab[LEN], int left, int right)
  9. {
  10. int pivot = (left + right) / 2; //srodkowy index
  11. int i = left, j = right, mem;
  12. while (1)
  13. {
  14. while (tab[i] < tab[pivot])
  15. ++i;
  16. while (tab[j] > tab[pivot])
  17. --j;
  18.  
  19. if (i >= j) break;
  20. mem = tab[i];
  21. tab[i] = tab[j];
  22. tab[j] = mem;
  23. }
  24. if(left - pivot > 2)qsort(tab, left, pivot);
  25. if (pivot - right > 2)qsort(tab, pivot+1, right);
  26. return;
  27. }
  28.  
  29.  
  30. int main()
  31. {
  32. int tab[LEN] = { 4, 23, 10, 13, 3, 26, 4, 24, 24, 14 }; // sypie sie na 4
  33. int left = 0, right = LEN -1;
  34. qsort(tab, left, right);
  35. for (int i = 0; i < LEN; ++i)
  36. printf("%d, ", tab[i]);
  37. return 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement