Advertisement
rozman50

QS

Mar 8th, 2019
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include "time.h"
  5.  
  6. const int arrSize = 30;
  7. int arr[arrSize];
  8. //int arr[arrSize] = { 5, 3, 7, 10, 8, 9 };
  9.  
  10. using namespace std;
  11.  
  12. int deli(int dno, int vrh);
  13. void izpisi();
  14. void hitroUredi(int dno, int vrh);
  15. void generiranje();
  16.  
  17. int main()
  18. {
  19. clock_t start, finish;
  20. double duration;
  21.  
  22. srand(time(NULL));
  23. generiranje();
  24.  
  25. izpisi();
  26.  
  27. start = clock();
  28. hitroUredi(0, arrSize - 1);
  29. finish = clock();
  30.  
  31. duration = (double)(finish - start);
  32. cout << "Dolzina: " << duration << endl;
  33. izpisi();
  34.  
  35. }
  36.  
  37. void generiranje() {
  38. for (int i = 0; i < arrSize; i++) {
  39. arr[i] = (rand() % 500);
  40. }
  41. }
  42.  
  43. void hitroUredi(int dno, int vrh) {
  44. if (dno < vrh) {
  45. int j = deli(dno, vrh);
  46. hitroUredi(dno, j - 1);
  47. hitroUredi(j + 1, vrh);
  48. }
  49. }
  50.  
  51. int deli(int dno, int vrh) {
  52.  
  53. int m = (dno + vrh) / 2;
  54.  
  55. int tmpB = arr[dno];
  56. arr[dno] = arr[m];
  57. arr[m] = tmpB;
  58.  
  59. int pe = arr[dno];
  60.  
  61. //cout << "pe: " << pe << endl;
  62. int levi = dno + 1;
  63. int desni = vrh;
  64.  
  65. while (true) {
  66.  
  67. while (arr[levi] < pe) { levi++; }
  68. while (arr[desni] > pe) { desni--; };
  69.  
  70. // sort asc/desc
  71. /*while (arr[levi] > pe) levi++;
  72. while (arr[desni] < pe) desni--;
  73. */
  74. /*for (; levi < vrh; levi++) {
  75. if (arr[levi] > pe) {
  76. leviNajden = levi;
  77. break;
  78. }
  79. }
  80. for (; desni >= dno; desni--) {
  81. if (arr[desni] < pe) {
  82. desniNajden = desni;
  83. break;
  84. }
  85. }
  86. */
  87. // cout << "LEVI: " << levi << " DESNI: " << desni << endl;
  88.  
  89. if (desni <= levi) break;
  90. else {
  91. int tmp = arr[levi];
  92. arr[levi] = arr[desni];
  93. arr[desni] = tmp;
  94. }
  95. }
  96.  
  97. int tmpA = arr[dno];
  98. arr[dno] = arr[desni];
  99. arr[desni] = tmpA;
  100.  
  101. return desni;
  102. }
  103.  
  104. void izpisi() {
  105. for (int i = 0; i < arrSize; i++) {
  106. cout << arr[i] << " ";
  107. }
  108. cout << endl;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement