-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path102-interpolation.c
85 lines (76 loc) · 1.97 KB
/
102-interpolation.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "search_algos.h"
/* header */
int interpolation_helper(int *array, int l, int h, int value, size_t p);
void print_value(int *array, int pos);
/**
* interpolation_search - implementation of the interpolation
* search algorithm
*
* @array: pointer to array to be searched
*
* @size: size of array
*
* @value: target to look for
*
* Return: the first index where value is located otherwise -1
*/
int interpolation_search(int *array, size_t size, int value)
{
int l, h;
size_t pos;
if (array == NULL || size < 2)
return (-1);
l = 0;
h = size - 1;
pos = l + (((double)(h - l) / (array[h] - array[l])) * (value - array[l]));
return (interpolation_helper(array, l, h, value, pos));
}
/**
* interpolation_helper - helper function
*
* @array: pointer to array to search
*
* @l: (int) low, keep tabs on the low position
*
* @h: (int) high, keep tabs on the highest position
*
* @value: target to look for
*
* @p: original position
*
* Return: the first index where value is located otherwise -1
*/
int interpolation_helper(int *array, int l, int h, int value, size_t p)
{
size_t pos;
if (l <= h && value >= array[l] && value <= array[h])
{
pos = l + (((double)(h - l) / (array[h] - array[l])) * (value - array[l]));
print_value(array, pos);
if (array[pos] == value)
return (pos);
if (array[pos] < value)
return (interpolation_helper(array, pos + 1, h, value, p));
if (array[pos] > value)
return (interpolation_helper(array, l, pos - 1, value, p));
}
printf("Value checked array[%lu] is out of range\n", p);
return (-1);
}
/**
* print_value - prints the value in the array everytime the array is
* being compared to the value being searched for
*
* @array: pointer to array
*
* @pos: position from helper function that keeps tab on the number
* of times the array has been searched
*
* Return: void
*/
void print_value(int *array, int pos)
{
int i;
for (i = pos; i <= pos; i++)
printf("Value checked array[%d] = [%d]\n", i, array[i]);
}