-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy path0-linear.c
41 lines (34 loc) · 915 Bytes
/
0-linear.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
#include "search_algos.h"
int recurse_helper(int *arr, size_t size, int val, size_t idx);
/**
* linear_search - search for value in an integer array
* @array: pointer to array of ints
* @size: size of array
* @value: value to locate
*
* Return: index of value; -1 if value not found
*/
int linear_search(int *array, size_t size, int value)
{
if (array == NULL)
return (-1);
return (recurse_helper(array, size, value, 0));
}
/**
* recurse_helper - recursive implement of linear search
* @arr: pointer to array of ints
* @size: size of array
* @val: value to locate
* @idx: current index
*
* Return: index of value; -1 if value not found
*/
int recurse_helper(int *arr, size_t size, int val, size_t idx)
{
if (idx == size)
return (-1);
printf("Value checked array[%lu] = [%d]\n", idx, arr[idx]);
if (arr[idx] == val)
return (idx);
return (recurse_helper(arr, size, val, idx + 1));
}