Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int closestSmallerSearch(int list[], int size, int value)
- {
- int low = 0, high = size - 1;
- int mid = 0;
- while (low <= high)
- {
- mid = low + ((high - low) / 2); // (low + high) / 2
- if (list[mid] == value)
- {
- return (mid - 1); // if matches then we need the index - 1 here
- }
- else if (list[mid] < value)
- {
- low = mid + 1;
- }
- else
- {
- high = mid - 1;
- }
- }
- if (low == high) // if not matches , we need the smaller nearest
- {
- if (list[low] < list[mid])
- {
- return low;
- }
- else if (list[low] > list[mid])
- {
- return (low - 1);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement