-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinary_Search.c
42 lines (34 loc) · 1.2 KB
/
Binary_Search.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
#include <stdio.h>
#include <stdlib.h>
// This function implements the Binary search algorithm
// How it works :
// Basically it compares a given value as a parameter the the middle of the array
// if it is bigger then it goes to middle of the second part of the array
// if it is less then it goes to the middle of first part of the array
// It repeats this until it finds the value
int binarySearch(int val, int v[], int arrSize, int* nbr){
int i =0;
int end=arrSize-1;
int found=0;
int middle;
// check in the value to search is between the max and min values the sorted array
if ( v[0] < val && v[end] > val) {
while ( found == 0 && end != middle) {
( *nbr)++; //increment the number of iterations of this functions
middle = (end + i) / 2;
if(v[middle] == val) { //check if the value at v[middle] equals to val
found = 1;
return middle;
} else {
if (v[middle] < val) {
i = middle;
} else {
end = middle;
}
}
}
} else { // return -1 if the value to search is not in the sorted array
printf("\nThe value doesn't exist in the list\n");
return -1;
}
}