i was also wondering if you can give me a pseudo-code to explain the algorithm?
public class BinarySearch
{
public static int search(int[ ] list, int key, int first, int last)
{
while (first < last) // Outer loop condition checks if list is finished
{
int mid = (first + last) / 2; // calculate mid(local var) point
if (key < list[mid]) //condition of inner loop if searched number is less than midpoint
{
last = mid; // if so discard other half and set smaller list in last and loop
}
else if (key > list[mid])// enter inner loop if searched number is greater than value searched
{
first = mid + 1; // new starting point coz value is not the same searched for and needs 2 be the next 1
}
else
{
return mid; //found number
}
}
return -1;// NOT_FOUND = -1
}
2007-12-13
04:18:49
·
3 answers
·
asked by
sprite
3
in
Computers & Internet
➔ Programming & Design
can you explain how do you test it?
2007-12-13
09:26:44 ·
update #1