First of all, I'm writing program to visualize sorting algorithms on Android I wrote method for insertion sort algorithm. I'm using MPAndroidChart for it.
void insertionSort(int[] arr) {
int i, j, newValue;
for (i = 1; i < arr.length; i++) {
newValue = arr[i];
j = i;
while (j > 0 && arr[j - 1] > newValue) {
arr[j] = arr[j - 1];
j--;
}
arr[j] = newValue;
}
}
...and it works fine. I want to redesign it to get the same result using ArrayList, but unfortunately, it crashes, and I have no idea why! This is my collection approach insertionSort:
void insertionSort(ArrayList<BarEntry> list) {
int i, j;
float newValue;
for (i = 1; i < list.size(); i++) {
newValue = list.get(i).getY();
j = i;
while (j > 0 && list.get(j-1).getY() > newValue) {
list.set(j, list.get(j-1));
j--;
}
BarEntry be = list.get(list.indexOf(newValue));
list.set(j, be);
}
}
BarEntry is class used to create charts. I want to sort my data by its Y coordinate, what I do by getY() function. Program shuts down there:
BarEntry be = list.get(list.indexOf(newValue)); list.set(j, be);
In a nutshell: How can I substitute
arr[j] = newValue;
to collection approach like this (not working):
list.set(j, list.get(list.indexOf(newValue)));