Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Why when I pass in the following arguments to the bSearch method does an out of

ID: 3813043 • Letter: W

Question

Why when I pass in the following arguments to the bSearch method does an out of bounds error occur, or it will get stuck in a continuous loop! I am trying to implement a binary search algorithm into my word unscrambler program, but I can't seem to get the binary search to work correctly. Can someone help point me in the correct direction?

static int b Search ArrayList ansk, String canon int low 0; int high ansk, size() int mid while low high mid low high 2 if canon. compareTo ansk. get (mid)) 0 high mid 1 else if canon. compare To ansk. get (mid) 0 low mid 1; else return mid; return -1

Explanation / Answer

change the below line in the bSearch method

1) int high=ansK.size()-1;
Explanation: ansK.size() give the number of element in the arrayList. but the arrayList index start from 0 till n-1 where n is the num of element.

2) high = mid-1
Explanation : we should go to left index when the canon is lesser than ansK.get(mid)

3) low = mid+1
Explanation : we should go to right index when the canon is greater than ansK.get(mid)

Note: I am just helping you with the error part. Let me know if you want the entire program. Feel free to ask any question in case of doubt