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
114 changes: 51 additions & 63 deletions searching/jump_search.c
Original file line number Diff line number Diff line change
@@ -1,85 +1,73 @@
/**
* @file jump_search.c
* @brief Implementation of [jump
* search](https://en.wikipedia.org/wiki/Jump_search) algorithm.
* Jump search implementation
* author: George-Anastasios Koutsovasilis
* github: https://github.com/koutsovasilis-tech
* @param arr The sorted array
* @param size The size of the sorted array
* @param x The element we are trying to find
*/
#include <assert.h>
#include <math.h>

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>

/**
* @brief Macro to return the minimum of two values
*/
#define min(X, Y) ((X) < (Y) ? (X) : (Y))

/**
* @brief Implement Jump-search algorithm
*
* @param [in] arr Array to search within
* @param x value to search for
* @param n length of array
* @return index where the value was found
* @return -1 if value not found
*/
int jump_search(const int *arr, int x, size_t n)
int jump_search(int arr[], size_t size, int x)
{
int step = floor(sqrt(n));
int prev = 0;

while (arr[min(step, n) - 1] < x)
if (size == 0) return -1;
size_t step = sqrt(size);
size_t prev = 0;
size_t i = 0;
while (i < size && arr[i] < x) // jumps till it finds the correct block or it exceeds it
{
prev = step;
step += floor(sqrt(n));
if (prev >= n)
prev = i;
i += step;
if (prev >= size)
{
return -1;
}
}

while (arr[prev] < x)
size_t limit = (i < size) ? i : size - 1; // limit because i might be larger than size
for (size_t m = prev; m <= limit; m++) // linear search
{
prev = prev + 1;
if (prev == min(step, n))
if (arr[m] == x)
{
return -1;
return m;
}
}
if (arr[prev] == x)
{
return prev;
}
return -1;
return -1;
}

/**
* @brief Test implementation of the function
*
*/
void test()
int main()
{
// Already sorted array for the tests
int arr[] = {0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610};
size_t n = sizeof(arr) / sizeof(int);
int size = sizeof(arr) / sizeof(arr[0]);

int x = 55;
printf("Test 1.... ");
int index = jump_search(arr, x, n);
assert(index == 10);
printf("passed\nTest 2.... ");
x = 56;
index = jump_search(arr, x, n);
assert(index == -1);
printf("passed\nTest 3.... ");
x = 13;
index = jump_search(arr, x, n);
assert(index == 7);
printf("passed\n");
}
// Test Case 1: Element in the middle
int target1 = 55;
int result1 = jump_search(arr, size, target1);
assert(arr[result1] == target1);
printf("Test 1 Passed: Found %d at index %d\n", target1, result1);

/**
* @brief Main function
*/
int main()
{
test();
// Test Case 2: Element at the start
int target2 = 0;
int result2 = jump_search(arr, size, target2);
assert(result2 == 0);
printf("Test 2 Passed: Found %d at index %d\n", target2, result2);

// Test Case 3: Element at the end
int target3 = 610;
int result3 = jump_search(arr, size, target3);
assert(result3 == size - 1);
printf("Test 3 Passed: Found %d at index %d\n", target3, result3);

// Test Case 4: Element does not exist
int target4 = 100;
int result4 = jump_search(arr, size, target4);
assert(result4 == -1);
printf("Test 4 Passed: Element %d correctly not found\n", target4);
printf("\n--- ALL TESTS PASSED SUCCESSFULLY ---\n");
return 0;
}

Loading