A recursive binary search function. It returns location of x in given array arr[l..r] is present, otherwise -1
| 210 | // A recursive binary search function. It returns location of x in |
| 211 | // given array arr[l..r] is present, otherwise -1 |
| 212 | static int |
| 213 | uint16_binary_search(uint16_t arr[], int l, int r, uint16_t x) |
| 214 | { |
| 215 | if (r >= l) { |
| 216 | int mid = l + (r - l)/2; |
| 217 | |
| 218 | // If the element is present at the middle itself |
| 219 | if (arr[mid] == x) return mid; |
| 220 | |
| 221 | // If element is smaller than mid, then it can only be present |
| 222 | // in left subarray |
| 223 | if (arr[mid] > x) return uint16_binary_search(arr, l, mid-1, x); |
| 224 | |
| 225 | // Else the element can only be present in right subarray |
| 226 | return uint16_binary_search(arr, mid+1, r, x); |
| 227 | } |
| 228 | |
| 229 | // We reach here when element is not present in array |
| 230 | return -1; |
| 231 | } |
| 232 | |
| 233 | static int |
| 234 | uint16_cmp (const void * a, const void * b) |