Matt g
Posts: 1
Nickname: mattg1981
Registered: Oct, 2003
|
|
Re: java code for a search engine
|
Posted: Oct 23, 2003 1:05 PM
|
|
Here is the code for a binary search ...
This is from code I was using earlier in the year to search for a students last name. value = to the name you want to find, student[] was an array full of students, and had a 'getter' called lastName.
It will return the index of the what you were searching for, or a -1 if the string could not be found.
public int binarySearch(String value) { int a = 0, mid; int z = numStudents;
while (a <= z) { mid = (a+z)/2;
if (value.compareToIgnoreCase(student[mid].lastName()) == 0) return mid; else if (value.compareToIgnoreCase(student[mid].lastName()) >= 1) a = mid + 1; else z = mid - 1; } return -1; }
|
|