2017-11-20 05:50:40 +03:00
|
|
|
#include <math.h>
|
2020-05-29 23:23:24 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#define min(X, Y) ((X) < (Y) ? (X) : (Y))
|
|
|
|
int jump_search(int *arr, int x);
|
2017-11-20 05:50:40 +03:00
|
|
|
int n;
|
|
|
|
|
2020-05-29 23:23:24 +03:00
|
|
|
int main()
|
|
|
|
{
|
|
|
|
int arr[] = {0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610};
|
|
|
|
n = sizeof(arr) / sizeof(int);
|
|
|
|
int x = 55;
|
|
|
|
int index = jump_search(arr, x);
|
|
|
|
printf("\nNumber %d is at index %d\n", x, index);
|
2017-11-20 05:50:40 +03:00
|
|
|
}
|
|
|
|
|
2020-05-29 23:23:24 +03:00
|
|
|
int jump_search(int *arr, int x)
|
|
|
|
{
|
|
|
|
int step = floor(sqrt(n));
|
|
|
|
int prev = 0;
|
|
|
|
while (*(arr + (min(step, n) - 1)) < x)
|
|
|
|
{
|
|
|
|
prev = step;
|
|
|
|
step += floor(sqrt(n));
|
|
|
|
if (prev >= n)
|
|
|
|
return -1;
|
|
|
|
}
|
2017-11-20 05:50:40 +03:00
|
|
|
|
2020-05-29 23:23:24 +03:00
|
|
|
while (*(arr + prev) < x)
|
|
|
|
{
|
|
|
|
prev = prev + 1;
|
|
|
|
if (prev == fmin(step, n))
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (*(arr + prev) == x)
|
|
|
|
return prev;
|
|
|
|
return -1;
|
2017-11-20 05:50:40 +03:00
|
|
|
}
|