// Binary Search in C #include int binarySearch(int array[], int x, int low, int high) /** * Binary search implementation. * int array[] - sorted array of INT to search for * x - INT value for searching * low - initial search starting index * high - initial last array index */ { // Repeat until the pointers low and high meet each other while (low <= high) { // Calculating mid position of an array int mid = low + (high - low) / 2; // If value of center object == value to search for if (array[mid] == x) // Return it return mid; // If mid value is less than value to search for if (array[mid] < x) // Move low border to the right low = mid + 1; // If mid value is greater than value to search for else // Move high border to mid-1 position high = mid - 1; } // If no value found - return -1 return -1; } int main(void) { int array[] = {3, 4, 5, 6, 7, 8, 9}; int n = sizeof(array) / sizeof(array[0]); int x = 8; 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; }