Okorosso

bin injections

May 18th, 2021 (edited)
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.53 KB | None | 0 0
  1. #include <iostream>
  2. #include <chrono>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. int Binary_search(int *arr, int item, int low, int high) {
  8.     int mid = (low + high) / 2;
  9.  
  10.     if (high <= low) {
  11.         if (item > arr[low])
  12.             item = low + 1;
  13.         else item = low;
  14.         return item;
  15.     }
  16.  
  17.     if (item == arr[mid])
  18.         return mid + 1;
  19.  
  20.     if (item > arr[mid])
  21.         return Binary_search(arr, item, mid + 1, high);
  22.  
  23.     return Binary_search(arr, item, low, mid - 1);
  24. }
  25.  
  26. void InsertionSort(int *arr, int size) {
  27.     int j, location, num;
  28.     for (int i = 1; i < size; i++) {
  29.         j = i - 1;
  30.         num = arr[i];
  31.         location = Binary_search(arr, num, 0, j);
  32.         while (j >= location) {
  33.             arr[j + 1] = arr[j];
  34.             j--;
  35.         }
  36.         arr[j + 1] = num;
  37.     }
  38. }
  39.  
  40. int main() {
  41.     using namespace std;
  42.     using std::chrono::duration_cast;
  43.     using std::chrono::milliseconds;
  44.     using std::chrono::system_clock;
  45.  
  46.     int size = 10;
  47.     for (int j = 0; size < 1000000000; j++) {
  48.         int *arr = new int[size];
  49.         for (int i = 0; i < size; i++) {
  50.             arr[i] = rand();
  51.         }
  52.         auto start = duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
  53.         InsertionSort(arr, size);
  54.         auto end = duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
  55.         auto using_time = end - start;
  56.         cout << size << " elements sorted for " << using_time << "ms\n";
  57.         size *= 10;
  58.     }
  59. }
Add Comment
Please, Sign In to add comment