-
Notifications
You must be signed in to change notification settings - Fork 0
/
100-jump.c
44 lines (36 loc) · 923 Bytes
/
100-jump.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "search_algos.h"
#include <math.h>
/**
* jump_search - searches for a value in an array of
* integers using the Jump search algorithm
*
* @array: input array
* @size: size of the array
* @value: value to search in
* Return: index of the number
*/
int jump_search(int *array, size_t size, int value)
{
int index, i, j, prev;
if (array == NULL || size == 0)
return (-1);
i = (int)sqrt((double)size);
j = 0;
prev = index = 0;
do {
printf("Value checked array[%d] = [%d]\n", index, array[index]);
if (array[index] == value)
return (index);
j++;
prev = index;
index = i * j;
} while (index < (int)size && array[index] < value);
printf("Value found between indexes [%d] and [%d]\n", prev, index);
for (; prev <= index && prev < (int)size; prev++)
{
printf("Value checked array[%d] = [%d]\n", prev, array[prev]);
if (array[prev] == value)
return (prev);
}
return (-1);
}