Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 59 additions & 31 deletions search/jump_search.cpp
Original file line number Diff line number Diff line change
@@ -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 <algorithm>
#include <cmath>
#include <iostream>

/** jump search implementation
#include <algorithm> /// for std::min
#include <cassert> /// for assert
#include <cmath> /// for std::sqrt
#include <iostream> /// for std::cout
#include <vector> /// 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<int>& arr, int x) {
int n = static_cast<int>(arr.size());
if (n == 0) return -1;

// Finding the block where element is
// present (if it is present)
int step = static_cast<int>(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<int>(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<int> 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<int> single = {42};
assert(jumpSearch(single, 42) == 0); // single element found
assert(jumpSearch(single, 10) == -1); // single element not found

std::vector<int> 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;
}