M
Mark
i can't seem to get my binary insert to work properly.
any help would be appreciated.
this function should return the location where newBook should be
inserted.. for example, if i have an array {AA,AB,AC,AE} and i want to
insert AD, it should return 3 (the index of AE). AE is then bumped
over by my other function..which works fine, i tested it with a simple
linear algorithm.
private int findInsertLocation(Resource newBook, int index)
{
int low = 0, high = size[index]-1;
while( low < high )
{
int mid = (low+high)/2;
if( newBook.compareTo(book[index][mid]) >= 0 )
low = mid + 1;
else
high = mid;
}
}
and you might want these...
public String toStringForCompare() {
return lastName + firstName + title + editionNumber;
}
// comparisons
public int compareTo(Resource anotherBook) {
return
toStringForCompare().compareToIgnoreCase(anotherBook.toStringForCompare());
}
this however works fine..
int i=0;
while(i<size[index]&&newBook.compareTo(book[index]) > 0) i++;
return i;
but it's slower.
any help would be appreciated.
this function should return the location where newBook should be
inserted.. for example, if i have an array {AA,AB,AC,AE} and i want to
insert AD, it should return 3 (the index of AE). AE is then bumped
over by my other function..which works fine, i tested it with a simple
linear algorithm.
private int findInsertLocation(Resource newBook, int index)
{
int low = 0, high = size[index]-1;
while( low < high )
{
int mid = (low+high)/2;
if( newBook.compareTo(book[index][mid]) >= 0 )
low = mid + 1;
else
high = mid;
}
}
and you might want these...
public String toStringForCompare() {
return lastName + firstName + title + editionNumber;
}
// comparisons
public int compareTo(Resource anotherBook) {
return
toStringForCompare().compareToIgnoreCase(anotherBook.toStringForCompare());
}
this however works fine..
int i=0;
while(i<size[index]&&newBook.compareTo(book[index]) > 0) i++;
return i;
but it's slower.