Hi I am using the following insertion sort algorithm and I would like to record the elements being compared. Basically I want the comparisons to be stored in two array lists, arrList1 and arrList2. If an element is in its correct place then both array lists will have the same element. If its not then arrList1 will have the chosen element and arrayList2 will have the element its comparing itself to. I am currently struggling to do this, so I was wondering if anyone can help? Thanks
public static ArrayList<Integer> arrList1 = new ArrayList<Integer>();
public static ArrayList<Integer> arrList2 = new ArrayList<Integer>();
...
public static void insertSort(int[] A){
for(int i = 1; i < A.length; i++){
int value = A[i];
int j = i - 1;
while(j >= 0 && A[j] > value){
A[j + 1] = A[j];
j = j - 1;
}
A[j + 1] = value;
}
}
EDIT:
For example: If my array had the numbers 1,3,2,4,6,5 then when comparing, this is how I want my two array lists to look:
arrList1 arrList2
#1 1 1
#2 3 3
#3 2 3 (As 2 goes before 3 then it must be compared to 3)
#4 4 4
#5 6 6
#6 5 6 (As 5 is lower then 6 then it must be compared to 6)
Sorted array: 1, 2, 3, 4, 5, 6
As you can see, arrList1 is basically the order of the input array whilst arrList2 is the element it compares to if its not in correct position. If its in correct position then arrList 2 will be same value.
There you have…
The output of this code for me is:
Array list 1: [1, 3, 2, 4, 6, 5]
Array list 2: [1, 3, 3, 4, 6, 6]
Sorted array: [1, 2, 3, 4, 5, 6]