From 6bbcdfdfc59fb0d96af1d659a69c7d75cfaee041 Mon Sep 17 00:00:00 2001 From: Shakir Hussain Date: Fri, 15 May 2026 23:43:26 +0500 Subject: [PATCH 1/2] Add test cases and improve binary search correctness --- search/binary_search.cpp | 82 +++++++++++++++++++++++++++++----------- 1 file changed, 60 insertions(+), 22 deletions(-) diff --git a/search/binary_search.cpp b/search/binary_search.cpp index ebe488e5658..cf9ccd4c3d9 100644 --- a/search/binary_search.cpp +++ b/search/binary_search.cpp @@ -4,12 +4,13 @@ * algorithm](https://en.wikipedia.org/wiki/Binary_search_algorithm) * @details * Binary search is a search algorithm that finds the position of a target value - * within a sorted array.Just like looking for a word in a dictionary, in binary search we compare the target value to the middle - * element of the array. If they are not equal, then the half in which the target - * cannot lie is eliminated and the search continues on the remaining half, - * again taking the middle element to compare to the target value, and repeating - * this until the target value is found. If the search ends with the remaining - * half being empty, the target is not in the array. + * within a sorted array.Just like looking for a word in a dictionary, in binary + * search we compare the target value to the middle element of the array. If + * they are not equal, then the half in which the target cannot lie is + * eliminated and the search continues on the remaining half, again taking the + * middle element to compare to the target value, and repeating this until the + * target value is found. If the search ends with the remaining half being + * empty, the target is not in the array. * * ### Implementation * @@ -38,8 +39,8 @@ #include /// for std::sort function #include /// for std::assert #include -#include /// for IO operations -#include /// for std::vector +#include /// for IO operations +#include /// for std::vector /****************************************************************************** * @namespace search * @brief Searching algorithms @@ -58,12 +59,15 @@ namespace binary_search { * @param val value to be searched * @returns @param int index of val in vector arr *******************************************************************************/ -uint64_t binarySearch(std::vector arr, uint64_t val) { - uint64_t low = 0; // set the lowest point of the vector. - uint64_t high = arr.size() - 1; // set the highest point of the vector. +int64_t binarySearch(const std::vector & arr, const int64_t val) { + if (arr.empty()) { + return -1; // if the array is empty, return -1. + } + int64_t low = 0; // set the lowest point of the vector. + int64_t high = (int64_t)arr.size() - 1; // set the highest point of the vector. while (low <= high) { - uint64_t m = low + (high - low) / 2; // set the pivot point + int64_t m = low + (high - low) / 2; // set the pivot point if (val == arr[m]) { return m; @@ -94,10 +98,10 @@ static void test1() { // array = [1,3,5,7,9,8,6,4,2] , Value = 4 // should return 3 - std::vector arr = {{1, 3, 5, 7, 9, 8, 6, 4, 2}}; + std::vector arr = {{1, 3, 5, 7, 9, 8, 6, 4, 2}}; std::sort(arr.begin(), arr.end()); - uint64_t expected_ans = 3; - uint64_t derived_ans = search::binary_search::binarySearch(arr, 4); + int64_t expected_ans = 3; + int64_t derived_ans = search::binary_search::binarySearch(arr, 4); std::cout << "Test #1: "; assert(derived_ans == expected_ans); std::cout << "Passed!" << std::endl; @@ -111,10 +115,10 @@ void test2() { // testcase #2 // array = [1,23,25,4,2] , Value = 25 // should return 4 - std::vector arr = {{1, 23, 25, 4, 2}}; + std::vector arr = {{1, 23, 25, 4, 2}}; std::sort(arr.begin(), arr.end()); - uint64_t expected_ans = 4; - uint64_t derived_ans = search::binary_search::binarySearch(arr, 25); + int64_t expected_ans = 4; + int64_t derived_ans = search::binary_search::binarySearch(arr, 25); std::cout << "Test #2: "; assert(derived_ans == expected_ans); std::cout << "Passed!" << std::endl; @@ -126,16 +130,48 @@ void test2() { *******************************************************************************/ void test3() { // testcase #3 - // array = [1,31,231,12,12,2,5,51,21,23,12,3] , Value = 5 + // array = [1,31,231,12,12,2,5,51,21,23,12,3] , Value = 31 // should return 8 - std::vector arr = {{1, 31, 231, 12, 2, 5, 51, 21, 23, 12, 3}}; + std::vector arr = {{1, 31, 231, 12, 2, 5, 51, 21, 23, 12, 3}}; std::sort(arr.begin(), arr.end()); - uint64_t expected_ans = 8; - uint64_t derived_ans = search::binary_search::binarySearch(arr, 31); + int64_t expected_ans = 8; + int64_t derived_ans = search::binary_search::binarySearch(arr, 31); std::cout << "Test #3: "; assert(derived_ans == expected_ans); std::cout << "Passed!" << std::endl; } +/******************************************************************************* + * @brief Self-test implementation #4 + * @returns void + *******************************************************************************/ +void test4() { + // testcase #4 + // array = [] , Value = 5 + // should return -1 + std::vector arr = {}; + std::sort(arr.begin(), arr.end()); + int64_t expected_ans = -1; + int64_t derived_ans = search::binary_search::binarySearch(arr, 5); + std::cout << "Test #4: "; + assert(derived_ans == expected_ans); + std::cout << "Passed!" << std::endl; +} +/******************************************************************************* + * @brief Self-test implementation #5 + * @returns void + *******************************************************************************/ +void test5() { + // testcase #5 + // array = [-20,20,50,-100,-5,20,-10] , Value = -10 + // should return 2 + std::vector arr = {{-20, 20, 50, -100, -5, 20, -10}}; + std::sort(arr.begin(), arr.end()); + int64_t expected_ans = 2; + int64_t derived_ans = search::binary_search::binarySearch(arr, -10); + std::cout << "Test #5: "; + assert(derived_ans == expected_ans); + std::cout << "Passed!" << std::endl; +} /******************************************************************************* * @brief Main function @@ -145,6 +181,8 @@ int main() { test1(); // run self-test implementation #1 test2(); // run self-test implementation #2 test3(); // run self-test implementation #3 + test4(); // run self-test implementation #4 + test5(); // run self-test implementation #5 return 0; } From dd9f16dde1bd7afa160cfd1853bc9eff6ed78472 Mon Sep 17 00:00:00 2001 From: Shakir Hussain Date: Sat, 16 May 2026 00:08:46 +0500 Subject: [PATCH 2/2] Improve readability by renaming m to mid --- search/binary_search.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/search/binary_search.cpp b/search/binary_search.cpp index cf9ccd4c3d9..96c62c883c2 100644 --- a/search/binary_search.cpp +++ b/search/binary_search.cpp @@ -67,19 +67,19 @@ int64_t binarySearch(const std::vector & arr, const int64_t val) { int64_t high = (int64_t)arr.size() - 1; // set the highest point of the vector. while (low <= high) { - int64_t m = low + (high - low) / 2; // set the pivot point + int64_t mid = low + (high - low) / 2; // set the pivot point - if (val == arr[m]) { - return m; + if (val == arr[mid]) { + return mid; } /**************************************************** * if pivot point is the val, return it, * else check if val is greater or smaller than pivot value * and set the next pivot point accordingly. ****************************************************/ - else if (val < arr[m]) { - high = m - 1; + else if (val < arr[mid]) { + high = mid - 1; } else { - low = m + 1; + low = mid + 1; } } return -1; // if val is not in the array, return -1.