Advertisement
Guest User

Untitled

a guest
Feb 20th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. #include <iostream>
  2. #include <time.h>
  3. using namespace std;
  4.  
  5. void quickSort(int mas[], int n)
  6. {
  7. int m = mas[n / 2], i = 0, j = n;
  8. do {
  9. while (mas[i] < m)
  10. i++;
  11. while (mas[j] > m)
  12. j--;
  13. if (i <= j)
  14. {
  15. int temp = mas[i];
  16. mas[i] = mas[j];
  17. mas[j] = temp;
  18. i++;
  19. j--;
  20. }
  21. } while (i <= j);
  22.  
  23. if (i < n)
  24. quickSort(mas + i, n - i);
  25. if (j > 0)
  26. quickSort(mas, j);
  27. }
  28. int main()
  29. {
  30. srand(time(NULL));
  31. int n;
  32. cin >> n;
  33. int* mas = new int[n];
  34.  
  35. for (int i = 0; i < n; i++)
  36. {
  37. mas[i] = rand() % 20 + 1;
  38. cout << mas[i] << ' ';
  39. }
  40. cout << endl;
  41. quickSort(mas,n-1);
  42.  
  43. for (int i = 0; i < n; i++)
  44. {
  45. cout << mas[i] << ' ';
  46. }
  47. system("pause");
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement