I'm trying to create a binary search that doesn't use an array. It also needs to count how many searches it does until it finds the number. I also have to use a randomaccessfile.
RandomAccessFile raf=new RandomAccessFile(new File("Project 10"),"rw");
//writing in random numbers. Different numbers will be used when it is being tested
raf.writeInt(-5);
raf.writeInt(-1);
raf.writeInt(122);
raf.writeInt(124);
raf.writeInt(125);
raf.writeInt(256);
user input what number do you want to search for. I need to do this without using the scanner class. the method to do this will be very helpful
I know this is how you do a binary search with an array. I need help figuring out how to do it without an array
public static int binarySearch(int[] list, int key) {
int low = 0;
int high = list.length - 1;
while (high >= low) {
int mid = (low + high) / 2;
if (key < list[mid])
high = mid - 1;
else if (key == list[mid])
return mid;
else
low = mid + 1;
}
return -low - 1; // Now high<low, key not found
}