|
1 |
| -/** |
| 1 | +/****************************************************************************** |
2 | 2 | * @file
|
3 | 3 | * @brief [Binary search
|
4 | 4 | * algorithm](https://en.wikipedia.org/wiki/Binary_search_algorithm)
|
5 |
| - */ |
6 |
| -#include <iostream> |
7 |
| - |
8 |
| -/** binary_search function |
9 |
| - * \param [in] a array to sort |
10 |
| - * \param [in] r right hand limit = \f$n-1\f$ |
11 |
| - * \param [in] key value to find |
12 |
| - * \returns index if T is found |
13 |
| - * \return -1 if T is not found |
14 |
| - */ |
15 |
| -int binary_search(int a[], int r, int key) { |
16 |
| - int l = 0; |
17 |
| - |
18 |
| - while (l <= r) { |
19 |
| - int m = l + (r - l) / 2; |
20 |
| - if (key == a[m]) |
| 5 | + * @details |
| 6 | + * Binary search is a search algorithm that finds the position of a target value |
| 7 | + * within a sorted array. Binary search compares the target value to the middle |
| 8 | + * element of the array. If they are not equal, the half in which the target |
| 9 | + * cannot lie is eliminated and the search continues on the remaining half, |
| 10 | + * again taking the middle element to compare to the target value, and repeating |
| 11 | + * this until the target value is found. If the search ends with the remaining |
| 12 | + * half being empty, the target is not in the array. |
| 13 | + * |
| 14 | + * ### Implementation |
| 15 | + * |
| 16 | + * Binary search works on sorted arrays. Binary search begins by comparing an |
| 17 | + * element in the middle of the array with the target value. If the target value |
| 18 | + * matches the element, its position in the array is returned. If the target |
| 19 | + * value is less than the element, the search continues in the lower half of |
| 20 | + * the array. If the target value is greater than the element, the search |
| 21 | + * continues in the upper half of the array. By doing this, the algorithm |
| 22 | + * eliminates the half in which the target value cannot lie in each iteration. |
| 23 | + * |
| 24 | + * ### Complexities |
| 25 | + * |
| 26 | + * //n is the number of element in the array. |
| 27 | + * |
| 28 | + * Worst-case time complexity O(log n) |
| 29 | + * Best-case time complexity O(1) |
| 30 | + * Average time complexity O(log n) |
| 31 | + * Worst-case space complexity 0(1) |
| 32 | + * |
| 33 | + * @author [Lajat Manekar](https://github.com/Lazeeez) |
| 34 | + * @author Unknown author |
| 35 | + *******************************************************************************/ |
| 36 | + |
| 37 | +#include <algorithm> /// for std::sort function |
| 38 | +#include <cassert> /// for std::assert |
| 39 | +#include <iostream> /// for IO operations |
| 40 | +#include <vector> /// for std::vector |
| 41 | +/****************************************************************************** |
| 42 | + * @namespace search |
| 43 | + * @brief Searching algorithms |
| 44 | + *******************************************************************************/ |
| 45 | +namespace search { |
| 46 | + |
| 47 | +/****************************************************************************** |
| 48 | + * @namespace binary_search |
| 49 | + * @brief Binary search searching algorihm |
| 50 | + *******************************************************************************/ |
| 51 | +namespace binary_search { |
| 52 | + |
| 53 | +/****************************************************************************** |
| 54 | + * @brief The main function which implements binary search |
| 55 | + * @param arr vector to be searched in |
| 56 | + * @param val value to be searched |
| 57 | + * @returns @param int index of val in vector arr |
| 58 | + *******************************************************************************/ |
| 59 | +uint64_t binarySearch(std::vector<uint64_t> arr, uint64_t val) { |
| 60 | + uint64_t low = 0; // set the lowest point of the vector. |
| 61 | + uint64_t high = arr.size() - 1; // set the highest point of the vector. |
| 62 | + |
| 63 | + while (low <= high) { |
| 64 | + uint64_t m = low + (high - low) / 2; // set the pivot point |
| 65 | + |
| 66 | + if (val == arr[m]) { |
21 | 67 | return m;
|
22 |
| - else if (key < a[m]) |
23 |
| - r = m - 1; |
24 |
| - else |
25 |
| - l = m + 1; |
| 68 | + } /**************************************************** |
| 69 | + * if pivot point is the val, return it, |
| 70 | + * else check if val is greater or smaller than pivot value |
| 71 | + * and set the next pivot point accordingly. |
| 72 | + ****************************************************/ |
| 73 | + else if (val < arr[m]) { |
| 74 | + high = m - 1; |
| 75 | + } else { |
| 76 | + low = m + 1; |
| 77 | + } |
26 | 78 | }
|
27 |
| - return -1; |
| 79 | + return -1; // if val is not in the array, return -1. |
28 | 80 | }
|
29 | 81 |
|
30 |
| -/** main function */ |
31 |
| -int main(int argc, char const* argv[]) { |
32 |
| - int n, key; |
33 |
| - std::cout << "Enter size of array: "; |
34 |
| - std::cin >> n; |
35 |
| - std::cout << "Enter array elements: "; |
| 82 | +} // namespace binary_search |
36 | 83 |
|
37 |
| - int* a = new int[n]; |
| 84 | +} // namespace search |
38 | 85 |
|
39 |
| - // this loop use for store value in Array |
40 |
| - for (int i = 0; i < n; i++) { |
41 |
| - std::cin >> a[i]; |
42 |
| - } |
| 86 | +/******************************************************************************* |
| 87 | + * @brief Self-test implementation #1 |
| 88 | + * @returns void |
| 89 | + *******************************************************************************/ |
| 90 | +static void test1() { |
| 91 | + // testcase #1 |
| 92 | + // array = [1,3,5,7,9,8,6,4,2] , Value = 4 |
| 93 | + // should return 3 |
43 | 94 |
|
44 |
| - std::cout << "Enter search key: "; |
45 |
| - std::cin >> key; |
| 95 | + std::vector<uint64_t> arr = {{1, 3, 5, 7, 9, 8, 6, 4, 2}}; |
| 96 | + std::sort(arr.begin(), arr.end()); |
| 97 | + uint64_t expected_ans = 3; |
| 98 | + uint64_t derived_ans = search::binary_search::binarySearch(arr, 4); |
| 99 | + std::cout << "Test #1: "; |
| 100 | + assert(derived_ans == expected_ans); |
| 101 | + std::cout << "Passed!" << std::endl; |
| 102 | +} |
| 103 | + |
| 104 | +/******************************************************************************* |
| 105 | + * @brief Self-test implementation #2 |
| 106 | + * @returns void |
| 107 | + *******************************************************************************/ |
| 108 | +void test2() { |
| 109 | + // testcase #2 |
| 110 | + // array = [1,23,25,4,2] , Value = 25 |
| 111 | + // should return 4 |
| 112 | + std::vector<uint64_t> arr = {{1, 23, 25, 4, 2}}; |
| 113 | + std::sort(arr.begin(), arr.end()); |
| 114 | + uint64_t expected_ans = 4; |
| 115 | + uint64_t derived_ans = search::binary_search::binarySearch(arr, 25); |
| 116 | + std::cout << "Test #2: "; |
| 117 | + assert(derived_ans == expected_ans); |
| 118 | + std::cout << "Passed!" << std::endl; |
| 119 | +} |
| 120 | + |
| 121 | +/******************************************************************************* |
| 122 | + * @brief Self-test implementation #3 |
| 123 | + * @returns void |
| 124 | + *******************************************************************************/ |
| 125 | +void test3() { |
| 126 | + // testcase #3 |
| 127 | + // array = [1,31,231,12,12,2,5,51,21,23,12,3] , Value = 5 |
| 128 | + // should return 8 |
| 129 | + std::vector<uint64_t> arr = {{1, 31, 231, 12, 2, 5, 51, 21, 23, 12, 3}}; |
| 130 | + std::sort(arr.begin(), arr.end()); |
| 131 | + uint64_t expected_ans = 8; |
| 132 | + uint64_t derived_ans = search::binary_search::binarySearch(arr, 31); |
| 133 | + std::cout << "Test #3: "; |
| 134 | + assert(derived_ans == expected_ans); |
| 135 | + std::cout << "Passed!" << std::endl; |
| 136 | +} |
46 | 137 |
|
47 |
| - // this is use for find value in given array |
48 |
| - int res = binary_search(a, n - 1, key); |
49 |
| - if (res != -1) |
50 |
| - std::cout << key << " found at index " << res << std::endl; |
51 |
| - else |
52 |
| - std::cout << key << " not found" << std::endl; |
| 138 | +/******************************************************************************* |
| 139 | + * @brief Main function |
| 140 | + * @returns 0 on exit |
| 141 | + *******************************************************************************/ |
| 142 | +int main() { |
| 143 | + test1(); // run self-test implementation #1 |
| 144 | + test2(); // run self-test implementation #2 |
| 145 | + test3(); // run self-test implementation #3 |
53 | 146 |
|
54 |
| - delete[] a; |
55 | 147 | return 0;
|
56 | 148 | }
|
0 commit comments