Advertisement
rozman50

Untitled

Mar 8th, 2019
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include "time.h"
  5.  
  6. const int arrSize = 1000;
  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. cout << "Konec";
  35.  
  36. }
  37.  
  38. void generiranje() {
  39. for (int i = 0; i < arrSize; i++) {
  40. arr[i] = (rand() % 500);
  41. }
  42. }
  43.  
  44. void hitroUredi(int dno, int vrh) {
  45. if (dno < vrh) {
  46. int j = deli(dno, vrh);
  47. hitroUredi(dno, j - 1);
  48. hitroUredi(j + 1, vrh);
  49. }
  50. }
  51.  
  52. int deli(int dno, int vrh) {
  53.  
  54. int m = (dno + vrh) / 2;
  55.  
  56. int tmpB = arr[dno];
  57. arr[dno] = arr[m];
  58. arr[m] = tmpB;
  59.  
  60. int pe = arr[dno];
  61. int levi = dno + 1;
  62. int desni = vrh;
  63.  
  64. while (true) {
  65.  
  66. while (arr[levi] < pe && levi <= vrh) { levi++; }
  67. while (arr[desni] > pe && desni >= dno) { desni--; };
  68.  
  69. if (desni <= levi) break;
  70. else {
  71. int tmp = arr[levi];
  72. arr[levi] = arr[desni];
  73. arr[desni] = tmp;
  74. }
  75. }
  76.  
  77. int tmpA = arr[dno];
  78. arr[dno] = arr[desni];
  79. arr[desni] = tmpA;
  80.  
  81. return desni;
  82. }
  83.  
  84. void izpisi() {
  85. for (int i = 0; i < arrSize; i++) {
  86. cout << arr[i] << " ";
  87. }
  88. cout << endl;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement