-
Notifications
You must be signed in to change notification settings - Fork 0
/
100-jump.c
39 lines (33 loc) · 1.18 KB
/
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
#include "search_algos.h"
/**
* jump_search - Searches for a value in a sorted array
* of integers using jump search.
* @array: A pointer to the first element of the array to search.
* @size: The number of elements in the array.
* @value: The value to search for.
*
* Return: If the value is not present or the array is NULL, -1.
* Otherwise, the first index where the value is located.
*
* Description: Prints a value every time it is compared in the array.
* Uses the square root of the array size as the jump step.
*/
int jump_search(int *array, size_t size, int value)
{
size_t i, jump, step;
if (array == NULL || size == 0)
return (-1);
step = sqrt(size);
for (i = jump = 0; jump < size && array[jump] < value;)
{
printf("Value checked array[%ld] = [%d]\n", jump, array[jump]);
i = jump;
jump += step;
}
printf("Value found between indexes [%ld] and [%ld]\n", i, jump);
jump = jump < size - 1 ? jump : size - 1;
for (; i < jump && array[i] < value; i++)
printf("Value checked array[%ld] = [%d]\n", i, array[i]);
printf("Value checked array[%ld] = [%d]\n", i, array[i]);
return (array[i] == value ? (int)i : -1);
}