MCPcopy Create free account
hub / github.com/F-Stack/f-stack / uint16_binary_search

Function uint16_binary_search

lib/ff_config.c:212–231  ·  view source on GitHub ↗

A recursive binary search function. It returns location of x in given array arr[l..r] is present, otherwise -1

Source from the content-addressed store, hash-verified

210// A recursive binary search function. It returns location of x in
211// given array arr[l..r] is present, otherwise -1
212static int
213uint16_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
233static int
234uint16_cmp (const void * a, const void * b)

Callers 1

ff_check_configFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected