for mid lies in the left line
if(a[s]<=a[mid){
}
this case has to be satisfied
i am not able to understand it
Searching in sorted and rotated
Hello @Chetan_Khandelwal,
As sir explained in the video:
-
Initially you had a sorted array in increasing order.
Suppose: 0 1 2 3 4 5 6 7 8 -
Now you have rotated the array towards right by 3 elements:
6 7 8 0 1 2 3 4 5 -
Now, observe the resulted sequence,
there are two increasing functions:
first, 6 7 8
second, 01 2 3 4 5 -
So, after calculating mid=(low+high)/2 =(0+8)/2=4 i.e. third index: a[mid]=1.
Now, you have to check which sequence among-st two specified in point 3, mid lies in.
4.1. if a[low]<a[mid]: then mid lies on the first sequence
this is because all the elements bigger than a[low] lies in left sequence
4.2. ielsef a[mid]<a[high]: then mid lies on the second sequence.
because all the elements smaller than a[high] lies in second/left sequence
Hope, this would help.
Give a like, if you are satisfied.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.