Created
October 11, 2019 03:44
-
-
Save easleyschool/11d7c4b78d65cf8663e33512b074de8a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
function for carrying out binary search on given array | |
- values[] => given sorted array | |
- len => length of the array | |
- target => value to be searched | |
*/ | |
int binarySearch(int values[], int len, int target) | |
{ | |
int max = (len - 1); | |
int min = 0; | |
int guess; // this will hold the index of middle elements | |
int step = 0; // to find out in how many steps we completed the search | |
while(max >= min) | |
{ | |
guess = (max + min) / 2; | |
// we made the first guess, incrementing step by 1 | |
step++; | |
if(values[guess] == target) | |
{ | |
printf("Number of steps required for search: %d \n", step); | |
return guess; | |
} | |
else if(values[guess] > target) | |
{ | |
// target would be in the left half | |
max = (guess - 1); | |
} | |
else | |
{ | |
// target would be in the right half | |
min = (guess + 1); | |
} | |
} | |
// We reach here when element is not | |
// present in array | |
return -1; | |
} | |
int main(void) | |
{ | |
int values[] = {13, 21, 54, 81, 90}; | |
int n = sizeof(values) / sizeof(values[0]); | |
int target = 81; | |
int result = binarySearch(values, n, target); | |
if(result == -1) | |
{ | |
printf("Element is not present in the given array."); | |
} | |
else | |
{ | |
printf("Element is present at index: %d", result); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment