-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy path105-jump_list.c
51 lines (40 loc) · 1.08 KB
/
105-jump_list.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
45
46
47
48
49
50
51
#include "search_algos.h"
#include <math.h>
/**
* jump_list - searches for a value in an array of
* integers using the Jump search algorithm
*
* @list: input list
* @size: size of the array
* @value: value to search in
* Return: index of the number
*/
listint_t *jump_list(listint_t *list, size_t size, int value)
{
size_t index, k, m;
listint_t *prev;
if (list == NULL || size == 0)
return (NULL);
m = (size_t)sqrt((double)size);
index = 0;
k = 0;
do {
prev = list;
k++;
index = k * m;
while (list->next && list->index < index)
list = list->next;
if (list->next == NULL && index != list->index)
index = list->index;
printf("Value checked at index [%d] = [%d]\n", (int)index, list->n);
} while (index < size && list->next && list->n < value);
printf("Value found between indexes ");
printf("[%d] and [%d]\n", (int)prev->index, (int)list->index);
for (; prev && prev->index <= list->index; prev = prev->next)
{
printf("Value checked at index [%d] = [%d]\n", (int)prev->index, prev->n);
if (prev->n == value)
return (prev);
}
return (NULL);
}