Thursday, May 22, 2008

Write an Optimized Binary Search algorithm.

int Search(int v[],int data,int N)
{
int start=0,end=N-1;
int mid=0;
mid = (start+end)/2;

if( (data <> v[end]) )
{
puts("NOT FOUND"); return -1;}

while(end-start>2)
{
if(data < v[mid])
end= mid -1;
else
start= mid + 1;
}

if(v[start] ==data)
{
puts("FOUND");
return start;
}

if(v[end] ==data)
{
puts("FOUND");
return end;
}
if(v[mid] ==data)
{
puts("FOUND");
return mid;
}

puts("NOT FOUND");
return -1;
}

1 comment:

Cindy Dy said...


I have been researching every aspect of a possible career move. This post is very helpful and shows that you have a lot of knowledge on the topic. Do you have any others?

Tam
www.gofastek.com