Thursday, September 13, 2012

Find smallest element in rotated array

#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&lt;last)

{

mid=(first+last)/2;

if(a[mid]&gt;a[last])

{

first=mid+1;

}

else

{

last=mid;

}

}

return first;

}

No comments: