I am getting some weird running times for my mergesort algorithm…I want to run the algorithm (say 1000 times) on the same permuted ArrayList. Right now, I just create an array of ArrayList of size 1000, then time how long it takes to sort each of these, then take the average of the times.
So my question is: am I sorting the same list over and over? If I change one instance of the ArrayList in the array say at index 0, will the other ArrayLists in the array stay the same? I would assume so, but I want to be certain that’s not happening? Thanks.
for (int N = 1000; N <= 10000; N += 1000) {
//copy the array
@SuppressWarnings("unchecked")
ArrayList<Integer>[] container = (ArrayList<Integer>[])new ArrayList[N];
ArrayList<Integer> testArray1 = generatePermutedOrder(N);
for(int j=0; j<timesToLoop; j++){
container[j] = testArray1;
}
System.out.print(N + "\t");
// let things stabilize
startTime = System.nanoTime();
while (System.nanoTime() - startTime < 1000000000)
;
// time the routine
startTime = System.nanoTime();
for (int i = 0; i < timesToLoop; i++) {
mergesort(container[i]);
}
Yes, modifying ArrayList at index 0 won’t effect the other indexes. (but make sure that other indexes don’t have reference to same ArrayList)
Scenerio 1: (Modifying index 0 will effect index 1)
Scenerio 2: (Modifying index 0 will NOT effect index 1)
So, make sure it’s Scenerio 2 in your code.
EDIT:
You are adding same reference to each index. So, modifying at any index will effect remaining indexes.