forked from gouravthakur39/beginners-C-program-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinarySearch.c
46 lines (40 loc) · 1.14 KB
/
BinarySearch.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 <stdio.h>
/* If x in into array return 0, else 1 */
/*
* A function with 4 params:
* int[] = array of elements
* int = wanted number
* int = start index
* int = end index
*
* returns 0 or 1
*/
int binarySearch(int[], int, int, int);
int main() {
int arr[] = {5, 15, 24, 32, 56, 89};
/* check length of array */
int size_of_array = sizeof(arr) / sizeof(int);
/* Check if 24 is into arr */
printf("%d\n", binarySearch(arr, 24, 0, size_of_array-1));
/* Check if 118 is into arr */
printf("%d\n", binarySearch(arr, 118, 0, size_of_array-1));
return 0;
}
int binarySearch(int array[], int number, int start, int end) {
/* if start index is get end index, check if that element is equals wanter nmber */
if(start >= end) {
return array[start] == number ? 0 : 1;
}
int tmp = (int) end / 2;
/* divide array length in half */
/* if number is greater than element in half, do search by start to tmp
* else search by tmp to end
*/
if(number == array[tmp]) {
return 0;
} else if(number > array[tmp]) {
return binarySearch(array, number, start, tmp);
} else {
return binarySearch(array, number, tmp, end);
}
}