Algorithms_in_C  1.0.0
Set of algorithms implemented in C.
jump_search.c File Reference

Implementation of jump search algorithm. More...

#include <assert.h>
#include <math.h>
#include <stdio.h>
Include dependency graph for jump_search.c:

Macros

#define min(X, Y)   ((X) < (Y) ? (X) : (Y))
 Macro to return the minimum of two values.
 

Functions

int jump_search (const int *arr, int x, size_t n)
 Implement Jump-search algorithm. More...
 
void test ()
 Test implementation of the function.
 
int main ()
 Main function.
 

Detailed Description

Implementation of jump search algorithm.

Function Documentation

◆ jump_search()

int jump_search ( const int *  arr,
int  x,
size_t  n 
)

Implement Jump-search algorithm.

Parameters
[in]arrArray to search within
xvalue to search for
nlength of array
Returns
index where the value was found
-1 if value not found
25 {
26  int step = floor(sqrt(n));
27  int prev = 0;
28 
29  while (arr[min(step, n) - 1] < x)
30  {
31  prev = step;
32  step += floor(sqrt(n));
33  if (prev >= n)
34  {
35  return -1;
36  }
37  }
38 
39  while (arr[prev] < x)
40  {
41  prev = prev + 1;
42  if (prev == min(step, n))
43  {
44  return -1;
45  }
46  }
47  if (arr[prev] == x)
48  {
49  return prev;
50  }
51  return -1;
52 }
min
#define min(X, Y)
Macro to return the minimum of two values.
Definition: jump_search.c:13