63int64_t
binary_search(std::vector<T> &arr, T val, int64_t low, int64_t high) {
65 return (val > arr[low]) ? (low + 1) : low;
67 int64_t mid = low + (high - low) / 2;
70 }
else if (arr[mid] < val) {
108 std::vector<int64_t> arr1({5, -3, -1, -2, 7});
109 std::cout <<
"1st test... ";
111 assert(std::is_sorted(std::begin(arr1), std::end(arr1)));
112 std::cout <<
"passed" << std::endl;
116 std::vector<int64_t> arr2({12, 26, 15, 91, 32, 54, 41});
117 std::cout <<
"2nd test... ";
119 assert(std::is_sorted(std::begin(arr2), std::end(arr2)));
120 std::cout <<
"passed" << std::endl;
124 std::vector<float> arr3({7.1, -2.5, -4.0, -2.1, 5.7});
125 std::cout <<
"3rd test... ";
127 assert(std::is_sorted(std::begin(arr3), std::end(arr3)));
128 std::cout <<
"passed" << std::endl;
132 std::vector<float> arr4({12.8, -3.7, -20.7, -7.1, 2.2});
133 std::cout <<
"4th test... ";
135 assert(std::is_sorted(std::begin(arr4), std::end(arr4)));
136 std::cout <<
"passed" << std::endl;
int64_t binary_search(std::vector< T > &arr, T val, int64_t low, int64_t high)
Binary search function to find the most suitable pace for an element.