The main problem is these lines:
int val = t.compare(arr[j - 1], arr[j]);
System.out.println(val);
if (val > 1) t.swap(arr[j - 1], arr[j]);
Since your compare
and swap
methods actually take array indices, these should be:
int val = t.compare(j - 1, j);
System.out.println(val);
if (val > 0) t.swap(j - 1, j);
Otherwise, you are using the array elements as array indices, and the values in the array are greater than the number of elements in the array.
Note the change in the condition on the last line too: the only value of val
that you return which is > 1
is 4, and that only occurs for types other than String
and Integer
.
In your swap
method, the first line:
o[i] = temp;
should be:
temp = o[i];
1
solved I am trying to implement a buublesort using a custom made compare method but i always keep getting ArrayIndexOutOfBound Exception [duplicate]