└── Binary search /Binary search: -------------------------------------------------------------------------------- 1 | // C program to implement iterative Binary Search 2 | #include 3 | 4 | // An iterative binary search function. 5 | int binarySearch(int arr[], int low, int high, int x) 6 | { 7 | while (low <= high) { 8 | int mid = low + (high - low) / 2; 9 | 10 | // Check if x is present at mid 11 | if (arr[mid] == x) 12 | return mid; 13 | 14 | // If x greater, ignore left half 15 | if (arr[mid] < x) 16 | low = mid + 1; 17 | 18 | // If x is smaller, ignore right half 19 | else 20 | high = mid - 1; 21 | } 22 | 23 | // If we reach here, then element was not present 24 | return -1; 25 | } 26 | 27 | // Driver code 28 | int main(void) 29 | { 30 | int arr[] = { 2, 3, 4, 10, 40 }; 31 | int n = sizeof(arr) / sizeof(arr[0]); 32 | int x = 10; 33 | int result = binarySearch(arr, 0, n - 1, x); 34 | if(result == -1) printf("Element is not present in array"); 35 | else printf("Element is present at index %d",result); 36 | 37 | } 38 | --------------------------------------------------------------------------------