Implementation of jump search algorithm.
More...
#include <assert.h>
#include <math.h>
#include <stdio.h>
|
#define | min(X, Y) ((X) < (Y) ? (X) : (Y)) |
| Macro to return the minimum of two values.
|
|
Implementation of jump search algorithm.
◆ jump_search()
int jump_search |
( |
const int * |
arr, |
|
|
int |
x, |
|
|
size_t |
n |
|
) |
| |
Implement Jump-search algorithm.
- Parameters
-
[in] | arr | Array to search within |
| x | value to search for |
| n | length of array |
- Returns
- index where the value was found
-
-1 if value not found
26 int step = floor(sqrt(n));
29 while (arr[
min(step, n) - 1] < x)
32 step += floor(sqrt(n));
42 if (prev ==
min(step, n))
#define min(X, Y)
Macro to return the minimum of two values.
Definition: jump_search.c:13
◆ main()
Main function.
void test()
Test implementation of the function.
Definition: jump_search.c:58
◆ test()
Test implementation of the function.
60 int arr[] = {0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610};
61 size_t n =
sizeof(arr) /
sizeof(
int);
64 printf(
"Test 1.... ");
67 printf(
"passed\nTest 2.... ");
71 printf(
"passed\nTest 3.... ");
int jump_search(const int *arr, int x, size_t n)
Implement Jump-search algorithm.
Definition: jump_search.c:24