Advertisement
rozman50

QS with pointers

Mar 8th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include "time.h"
  5.  
  6. using namespace std;
  7.  
  8. // int arrSize = 10;
  9. // int arr[arrSize];
  10.  
  11.  
  12. int deli(int *arr[], int dno, int vrh);
  13. void izpisiZaporedje(int *arr[], int velikost);
  14. void hitroUredi(int *arr[], int dno, int vrh);
  15. void generirajNakljucno(int *arr[], int velikost);
  16. void generirajNarascajoce(int *arr[], int velikost);
  17. void generirajPadajoce(int *arr[], int velikost);
  18. void preveri(int *arr[], int velikost);
  19.  
  20. int main()
  21. {
  22. clock_t start, finish;
  23. double duration;
  24. srand(time(NULL));
  25.  
  26. int size = 0;
  27.  
  28. cout << "Vnesi velikost polja: " << endl;
  29. cin >> size;
  30.  
  31. int *arrStevil = new int[size];
  32.  
  33. while (true) {
  34.  
  35. int izbira;
  36. cout <<
  37. "Hitro uredi - izbira\n\n" <<
  38. "1 Generiraj nakljucno zaporedje\n" <<
  39. "2 Generiraj urejeno narascajoce zaporedje\n" <<
  40. "3 Generiraj urejeno padajoče zaporedje\n" <<
  41. "4 Izpis zaporedja\n" <<
  42. "5 Uredi\n" <<
  43. "6 Konec\n" <<
  44. "Vasa izbira: ";
  45. cin >> izbira;
  46.  
  47. switch (izbira) {
  48. case 1:
  49. generirajNakljucno(&arrStevil, size);
  50. break;
  51. case 2:
  52. generirajNarascajoce(&arrStevil, size);
  53. break;
  54. case 3:
  55. generirajPadajoce(&arrStevil, size);
  56. break;
  57. case 4:
  58. izpisiZaporedje(&arrStevil, size);
  59. break;
  60. case 5:
  61. start = clock();
  62. hitroUredi(&arrStevil, 0, size - 1);
  63. finish = clock();
  64. duration = (double)(finish - start);
  65. cout << "Cas izvajanja: " << duration << endl;
  66. preveri(&arrStevil, size);
  67. break;
  68. case 6:
  69. return 1;
  70. default:
  71. cout << "Vpisi ponovno: ";
  72. }
  73. }
  74.  
  75. }
  76.  
  77. void preveri(int *arr[], int velikost) {
  78. bool pravilnoUrejeno = true;
  79. for (int i = 0; i < velikost - 1; i++) {
  80. if (arr[i + 1] < arr[i]) {
  81. cout << arr[i + 1] << " " << arr[i] << endl;
  82. pravilnoUrejeno = false;
  83. }
  84. }
  85. if (pravilnoUrejeno) cout << "Zaporedje je pravilno urejeno" << endl;
  86. else cout << "Zaporedje ni pravilno urejeno" << endl;
  87. }
  88.  
  89.  
  90. void generirajNakljucno(int *arr[], int velikost) {
  91. for (int i = 0; i < velikost; i++)
  92. *arr[i] = (rand() % 50000);
  93. }
  94.  
  95. void generirajNarascajoce(int *arr[], int velikost) {
  96. for (int i = 0; i < velikost; i++)
  97. *arr[i] = i;
  98. }
  99.  
  100. void generirajPadajoce(int *arr[], int velikost) {
  101. for (int i = velikost; i >= 0; i--)
  102. *arr[i] = velikost - i;
  103. }
  104.  
  105.  
  106. void hitroUredi(int *arr[], int dno, int vrh) {
  107. if (dno < vrh) {
  108. int j = deli(arr, dno, vrh);
  109. hitroUredi(arr, dno, j - 1);
  110. hitroUredi(arr, j + 1, vrh);
  111. }
  112. }
  113.  
  114. int deli(int *arr[], int dno, int vrh) {
  115.  
  116. int m = (dno + vrh) / 2;
  117.  
  118. int tmpA = *arr[dno];
  119. *arr[dno] = *arr[m];
  120. *arr[m] = tmpA;
  121.  
  122. int pe = *arr[dno];
  123. int levi = dno + 1;
  124. int desni = vrh;
  125.  
  126. while (true) {
  127.  
  128. while (*arr[levi] < pe && levi <= vrh) levi++;
  129. while (*arr[desni] > pe && desni >= dno) desni--;
  130.  
  131. if (desni <= levi) break;
  132. else {
  133. int tmpB = *arr[levi];
  134. *arr[levi] = *arr[desni];
  135. *arr[desni] = tmpB;
  136. levi++;
  137. desni--;
  138. }
  139. }
  140.  
  141. int tmpC = *arr[dno];
  142. *arr[dno] = *arr[desni];
  143. *arr[desni] = tmpC;
  144.  
  145. return desni;
  146. }
  147.  
  148. void izpisiZaporedje(int *arr[], int velikost) {
  149. for (int i = 0; i < velikost; i++) {
  150. cout << arr[i] << " ";
  151. }
  152. cout << endl;
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement