Yes, you can use mid+2 there. On Apr 17, 1:23 pm, rahul sharma <rahul23111...@gmail.com> wrote: > following is logn soln > int ceilSearch(int arr[], int low, int high, int x) > { > int mid; > > /* If x is smaller than or equal to the first element, > then return the first element */ > if(x <= arr[low]) > return low; > > /* If x is greater than the last element, then return -1 */ > if(x > arr[high]) > return -1; > > /* get the index of middle element of arr[low..high]*/ > mid = (low + high)/2; /* low + (high - low)/2 */ > > /* If x is same as middle element, then return mid */ > if(arr[mid] == x) > return mid; > > /* If x is greater than arr[mid], then either arr[mid + 1] > is ceiling of x or ceiling lies in arr[mid+1...high] */ > else if(arr[mid] < x) > { > if(mid + 1 <= high && x <= arr[mid+1]) > return mid + 1; > else > return ceilSearch(arr, mid+1, high, x); > } > > /* If x is smaller than arr[mid], then either arr[mid] > is ceiling of x or ceiling lies in arr[mid-1...high] */ > else > { > if(mid - 1 >= low && x > arr[mid-1]) > return mid; > else > return ceilSearch(arr, low, mid - 1, x); > }} > > I wana ask cant we put mid+2 in elseif else part.as we know mid+1 cant be > ceil.so cant we start from mid+2............
-- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To unsubscribe from this group and stop receiving emails from it, send an email to algogeeks+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.