- 1

// Binary Search in C Can anyone explain me this code

// Binary Search in C #include <stdio.h> int binarySearch(int array[], int x, int low, int high) { // Repeat until the pointers low and high meet each other while (low <= high) { int mid = low + (high - low) / 2; if (array[mid] == x) return mid; if (array[mid] < x) low = mid + 1; else high = mid - 1; } return -1; } int main(void) { int array[] = {3, 4, 5, 6, 7, 8, 9}; int n = sizeof(array) / sizeof(array[0]); int x = 4; int result = binarySearch(array, x, 0, n - 1); if (result == -1) printf("Not found"); else printf("Element is found at index %d", result); return 0; }

6th Sep 2022, 6:55 AM
Abhishek
Abhishek - avatar
1 Réponse
+ 4
You can find explanation here clearly... https://www.sololearn.com/learn/664/?ref=app
6th Sep 2022, 7:05 AM
Jayakrishna 🇮🇳