53namespace binary_search {
61uint64_t binarySearch(std::vector<uint64_t> arr, uint64_t val) {
63 uint64_t high = arr.size() - 1;
66 uint64_t m = low + (high - low) / 2;
75 else if (val < arr[m]) {
97 std::vector<uint64_t> arr = {{1, 3, 5, 7, 9, 8, 6, 4, 2}};
98 std::sort(arr.begin(), arr.end());
99 uint64_t expected_ans = 3;
100 uint64_t derived_ans = search::binary_search::binarySearch(arr, 4);
101 std::cout <<
"Test #1: ";
102 assert(derived_ans == expected_ans);
103 std::cout <<
"Passed!" << std::endl;
114 std::vector<uint64_t> arr = {{1, 23, 25, 4, 2}};
115 std::sort(arr.begin(), arr.end());
116 uint64_t expected_ans = 4;
117 uint64_t derived_ans = search::binary_search::binarySearch(arr, 25);
118 std::cout <<
"Test #2: ";
119 assert(derived_ans == expected_ans);
120 std::cout <<
"Passed!" << std::endl;
131 std::vector<uint64_t> arr = {{1, 31, 231, 12, 2, 5, 51, 21, 23, 12, 3}};
132 std::sort(arr.begin(), arr.end());
133 uint64_t expected_ans = 8;
134 uint64_t derived_ans = search::binary_search::binarySearch(arr, 31);
135 std::cout <<
"Test #3: ";
136 assert(derived_ans == expected_ans);
137 std::cout <<
"Passed!" << std::endl;
void test3(double eta=0.01)
void test2(const std::string &text)
Self test 2 - using 8x8 randomly generated key.
void test1(const std::string &text)
Self test 1 - using 3x3 randomly generated key.