kokokozhina

Untitled

Feb 13th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <stdio.h> /* printf, scanf, puts, NULL */
  5. #include <stdlib.h> /* srand, rand */
  6. #include <time.h> /* time */
  7.  
  8. using namespace std;
  9.  
  10. void quick_sort(vector<int> & a, int p, int r)
  11. {
  12. int q = -1;
  13. if(p < r)
  14. {
  15. int x = a[p];
  16. int i = p - 1;
  17. int j = r + 1;
  18. while(true)
  19. {
  20. do j--;
  21. while(a[j] > x);
  22.  
  23. do i++;
  24. while(a[i] < x);
  25.  
  26. if(i < j)
  27. {
  28. swap(a[i], a[j]);
  29. }
  30. else
  31. {
  32. q = j;
  33. break;
  34. }
  35. }
  36. quick_sort(a, p, q);
  37. quick_sort(a, q + 1, r);
  38. }
  39. }
  40.  
  41. int main()
  42. {
  43. #ifdef _DEBUG
  44. freopen("in.txt", "r", stdin);
  45. freopen("out.txt", "w", stdout);
  46. #endif
  47.  
  48. //Быстрая сортировка.
  49. srand (time(NULL));
  50. int n;
  51. cin >> n;
  52. vector<int> a(n);
  53. cout << "Non-sorted array:\n";
  54. for(int i = 0; i < n; i++)
  55. {
  56. a[i] = rand() % (int)1e9 - (int)1e4;
  57. cout << a[i] << endl;
  58. }
  59. cout << endl;
  60.  
  61. quick_sort(a, 0, a.size() - 1);
  62.  
  63. cout << "Sorted array:\n";
  64. for(int i = 0; i < n; i++)
  65. {
  66. cout << a[i] << endl;
  67. }
  68. return 0;
  69. }
Add Comment
Please, Sign In to add comment