I have an arraylist containing 10 000 random values, ranging from 1-1000. I want to test various sorts (bubble, insertion, selection, etc) for their speed, and in order to do so fairly, I need to use the same array.
I create the array:
int num;
int[] data = new int [10000];
for (int i = 0; i < 10000; i++) {
num=(int)(Math.random()*1000+1);
data [i] = num;
I sort it using bubble sort:
{
int tempVarBS;
for (int i = 0; i < data.length-1; i++)
{
for(int j = 0; j < data.length-i-1; j++)
{
if(data[j] > data[j + 1])
{
tempVarBS = data [j + 1];
data [j + 1]= data [j];
data [j] = tempVarBS;
}
}
}
for (int i = 0; i < data.length; i++)
{
System.out.println(data[i]);
}
}
Now, the array has been sorted. When I go to test the insertion sort, the array has already been sorted. For a small number of tests, I figured I could simply copy the array x number of times for x number of sorts I want to test.
However, that would be horribly inefficient for a large number of tests, so is there a way for me to only instantiate a single array, then sort it multiple times?
Essentially, I want to sort the array, but preserve the original unsorted array and reuse it each time I sort.