add jumpsearch

This commit is contained in:
koseokkyu 2017-11-20 11:50:40 +09:00
parent 19a2def4a9
commit 04527dfc56

33
Searches/Jump_Search.c Normal file
View File

@ -0,0 +1,33 @@
#include <stdio.h>
#include <math.h>
#define min(X,Y) ((X) < (Y) ? (X) : (Y))
int jump_search(int* arr, int x);
int n;
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);
}
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;
}
while (*(arr + prev) < x) {
prev = prev + 1;
if (prev == fmin(step, n))
return -1;
}
if (*(arr + prev) == x)
return prev;
return -1;
}