From 5a6112b8017371f847795859c67a7092d593763b Mon Sep 17 00:00:00 2001 From: Isha Sharma Date: Wed, 10 Jun 2026 14:32:30 +0530 Subject: [PATCH] fix: improve jump_search - precompute sqrt, add tests, modernize C++ --- search/jump_search.cpp | 90 +++++++++++++++++++++++++++--------------- 1 file changed, 59 insertions(+), 31 deletions(-) diff --git a/search/jump_search.cpp b/search/jump_search.cpp index f7b100a4e03..ea7a22c72da 100644 --- a/search/jump_search.cpp +++ b/search/jump_search.cpp @@ -1,55 +1,83 @@ /** * \file - * \brief C++ program to implement [Jump - * Search](https://en.wikipedia.org/wiki/Jump_search) + * \brief [Jump Search Algorithm](https://en.wikipedia.org/wiki/Jump_search) + * + * \details + * Jump Search is a searching algorithm for sorted arrays. It works by + * jumping ahead by fixed steps of size sqrt(n), then doing a linear + * scan backward once the target range is identified. + * + * Time Complexity: O(sqrt(n)) + * Space Complexity: O(1) + * + * @author [SaiEashwarKS](https://github.com/SaiEashwarKS) + * @author [Your Name](https://github.com/YOUR_USERNAME) */ -#include -#include -#include -/** jump search implementation +#include /// for std::min +#include /// for assert +#include /// for std::sqrt +#include /// for std::cout +#include /// for std::vector + +/** + * \brief Jump Search implementation + * \param arr sorted input array + * \param x element to search for + * \returns index of x in arr + * \returns -1 if x is not found */ -int jumpSearch(int arr[], int x, int n) { - // Finding block size to be jumped - int step = std::sqrt(n); +int jumpSearch(const std::vector& arr, int x) { + int n = static_cast(arr.size()); + if (n == 0) return -1; - // Finding the block where element is - // present (if it is present) + int step = static_cast(std::sqrt(n)); // compute once int prev = 0; + while (arr[std::min(step, n) - 1] < x) { prev = step; - step += std::sqrt(n); + step += static_cast(std::sqrt(n)); if (prev >= n) return -1; } - // Doing a linear search for x in block - // beginning with prev. while (arr[prev] < x) { prev++; - - // If we reached next block or end of - // array, element is not present. if (prev == std::min(step, n)) return -1; } - // If element is found - if (arr[prev] == x) - return prev; - return -1; + return (arr[prev] == x) ? prev : -1; } -// Driver program to test function -int main() { - int arr[] = {0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610}; - int x = 55; - int n = sizeof(arr) / sizeof(arr[0]); +/** + * @brief Self-test implementations + * @returns void + */ +static void tests() { + std::vector arr = {0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610}; - // Find the index of 'x' using Jump Search - int index = jumpSearch(arr, x, n); + assert(jumpSearch(arr, 55) == 10); // element in middle + assert(jumpSearch(arr, 0) == 0); // first element + assert(jumpSearch(arr, 610) == 15); // last element + assert(jumpSearch(arr, 100) == -1); // not present + assert(jumpSearch(arr, -1) == -1); // below range - // Print the index where 'x' is located - std::cout << "\nNumber " << x << " is at index " << index; - return 0; + std::vector single = {42}; + assert(jumpSearch(single, 42) == 0); // single element found + assert(jumpSearch(single, 10) == -1); // single element not found + + std::vector empty = {}; + assert(jumpSearch(empty, 1) == -1); // empty array + + std::cout << "All tests have successfully passed!\n"; } + +/** + * @brief Main function + * @returns 0 on exit + */ +int main() { + tests(); + return 0; +} \ No newline at end of file