Guest User

Untitled

a guest
Dec 15th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include <windows.h>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. void main()
  8. {
  9. srand(time(0));
  10. rand();
  11.  
  12. // array size
  13. const int size = 50000;
  14.  
  15. // integer data array
  16. int ar[size];
  17.  
  18. // fill array by random values
  19. for (int i = 0; i < size; i++)
  20. {
  21. ar[i] = rand() % 10000;
  22. }
  23.  
  24. // print array - foreach example!
  25. for (int current : ar)
  26. {
  27. cout << current << ", ";
  28. }
  29. cout << "\n\n";
  30.  
  31. // some value to find in array
  32. int value_to_find = 777;
  33.  
  34. //////////////////////////////////////////////////////////////////
  35. // linear search
  36. //////////////////////////////////////////////////////////////////
  37.  
  38. int linear_iteration_count = 0;
  39. int linear_index = -1;
  40.  
  41. LARGE_INTEGER frequency, start_time, end_time;
  42. QueryPerformanceFrequency(&frequency);
  43. QueryPerformanceCounter(&start_time);
  44.  
  45. for (int i = 0; i < size; i++)
  46. {
  47. linear_iteration_count++;
  48. if (ar[i] == value_to_find)
  49. {
  50. linear_index = i;
  51. break;
  52. }
  53. }
  54.  
  55. QueryPerformanceCounter(&end_time);
  56.  
  57. double work_time = (double)(end_time.QuadPart - start_time.QuadPart) / frequency.QuadPart * 1000.0;
  58.  
  59. cout << "\nValue was find by index: " << linear_index << "\n";
  60. cout << "Linear iteration count: " << linear_iteration_count << "\n";
  61. cout << "Linear search work time: " << work_time << "ms." << "\n";
  62. cout << "\n////////////////////////////////////////\n";
  63.  
  64. system("pause");
  65.  
  66. //////////////////////////////////////////////////////////////////
  67. // binary search
  68. //////////////////////////////////////////////////////////////////
  69.  
  70. int binary_iteration_count = 0;
  71. int binary_index = -1;
  72.  
  73. sort(ar, ar + size);
  74.  
  75. // print sorted array
  76. for (int i = 0; i < size; i++)
  77. {
  78. cout << /*i << " - " << */ar[i] << ", ";
  79. }
  80.  
  81. QueryPerformanceFrequency(&frequency);
  82. QueryPerformanceCounter(&start_time);
  83.  
  84. int L = 0, R = size - 1; // left and right border
  85. int M; // median element index
  86. while (true)
  87. {
  88. binary_iteration_count++;
  89. M = L + (R - L) / 2; // or (L + R) / 2
  90. if (ar[M] > value_to_find)
  91. R = M - 1;
  92. else if (ar[M] < value_to_find)
  93. L = M + 1;
  94. else
  95. {
  96. binary_index = M;
  97. break;
  98. }
  99. if (L > R)
  100. break; // oops!
  101. }
  102.  
  103. QueryPerformanceCounter(&end_time);
  104.  
  105. work_time = (double)(end_time.QuadPart - start_time.QuadPart) / frequency.QuadPart * 1000.0;
  106.  
  107. cout << "\nValue was find by index: " << binary_index << "\n";
  108. cout << "Binary Iteration count: " << binary_iteration_count << "\n";
  109. cout << "Binary search work time: " << work_time << "ms.\n";
  110.  
  111. system("pause");
  112. }
Add Comment
Please, Sign In to add comment