mirror of
https://github.com/TheAlgorithms/C
synced 2024-11-25 06:49:36 +03:00
add jumpsearch
This commit is contained in:
parent
19a2def4a9
commit
04527dfc56
33
Searches/Jump_Search.c
Normal file
33
Searches/Jump_Search.c
Normal 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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user