I am getting a weird array index out of bound exception when I use the length of the array as the index
I have twice checked everything and printed some stuff like the array.length to the console
Here is the problematic code
public static boolean doJumpSearch(int array[], int number) {
int start = 0;
int end = (int) Math.sqrt(array.length);
while(array[end] <= number && end < array.length) {
start = end;
end += (int)Math.sqrt(array.length);
if(end > array.length - 1)
end = array.length;
}
for(int i = start; i<end; i++) {
if(array[i] == number)
return true;
}
return false;
}
Here is the runner code
public static void main(String[] args) {
int[] a = {1, 2, 3};
if(search.doJumpSearch(a, 3)) System.out.println("Yup");
}
Here is the error
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at Rector.array.search.doJumpSearch(search.java:46)
at dummy.main(dummy.java:12)