Question:-  How to find the smallest element in rotated sorted array?
Input 1: array[]={4,5,6,7,8,9,1,2,3}; Output : position of smallest element means here 6 (array starts from zero). Input 2: array[]={1,2,3,4,5,6,7,8,9}; Output : position of smallest element means here 0 (array starts from zero). The below mentioned code has been compiled in Microsoft Visual Studio 6.0 #include<stdio.h> int find_smallest_element_position(int *a, int first , int last); main() { int a[10]={4,5,6,7,8,9,1,2,3}; int pos; pos=find_smallest_element_position(a,0,8); printf("The smallest element's position is %d\n",pos); } int find_smallest_element_position(int *a, int first , int last) { int mid; while(first<last) { mid=(first+last)/2; if(a[mid]>a[last]) { first=mid+1; } else { last=mid; } } return first; }

NB:

The above question is one of the Frequently asked question in software Job interview in BIG company.
Free Web Hosting