Posted by: kaloVale February 20, 2009
Login in to Rate this Post:
0
?
Not sure if you are looking for binary search. Looks like it is binary search though
Here is the function for binary search
int Multiwaysearch(keyList,start,end,searchKey)
{
int k = 0;
while( start <= end)
{
k = ( start + end)/2;
if (searchKey = = keyList[k])
return k; // returns this position if searchKey is found in keyList
if (searchKey < keyList[k])
end = k -1;
else
start = k+1;
}
return -1; // returns -1 indicating that no match was found
return ( -k) // this might return the position the number would be found if the number is not in the keyList.
}
You don't have to use compare function here, but if you need to you could now modify anyway.
Hope this helps
Here is the function for binary search
int Multiwaysearch(keyList,start,end,searchKey)
{
int k = 0;
while( start <= end)
{
k = ( start + end)/2;
if (searchKey = = keyList[k])
return k; // returns this position if searchKey is found in keyList
if (searchKey < keyList[k])
end = k -1;
else
start = k+1;
}
return -1; // returns -1 indicating that no match was found
return ( -k) // this might return the position the number would be found if the number is not in the keyList.
}
You don't have to use compare function here, but if you need to you could now modify anyway.
Hope this helps