Min element in a sorted array

Given an array, which has been sorted in ascending order and rotated, how will you go about
finding the min element in it.

Answer:


Let original array be 10 20 30 40 50 , so on rotation it become 40 50 10 20 30
40 50 10 20 30
^ ^
Up Down

up = array.begin();
down = Up+1;

while(down != array.end() )
{
if(*down < *up ) then *down is the smallest element
++up;
++down;
}


No comments: