samedi 7 mars 2015

Java -- Sort time discrepancy



I've written a program for a school assignment that prints out the time it takes to sort an array of integers using six different sorting algorithms: selection sort, bubble sort, merge sort, quick sort, heap sort, and radix sort. The integer arrays range from 50,000 to 300,000 elements.


What's bothering me is with the first result of the merge sort, heap sort, radix sort, and the Collections.sort() method.


The first array, the 50,000 element array, takes longer to sort than the subsequent, longer arrays. Each subsequent, larger array takes an increasing amount of time to sort, as I'd expect. I'm wondering what's causing this, whether it's overhead that I'm not accounting for or whether there's a problem with my algorithms or program.


I've attached a link to a screenshot showing the results


Below is an example of the code



int[] array = generateIntegers(50000);
long start = System.currentTimeMillis();
radixSort(array);
long end = System.currentTimeMillis();
System.out.println(end - start);

int[] arrayTwo = generateIntegers(100000);
start = System.currentTimeMillis();
radixSort(arrayTwo);
end = System.currentTimeMillis();
System.out.println(end - start);

int[] arrayThree = generateIntegers(150000);
start = System.currentTimeMillis();
radixSort(arrayThree);
end = System.currentTimeMillis();
System.out.println(end - start);


Console:



40
10
13


And the generateIterators(n) method



public static int[] generateIntegers(int size)
{
int[] arr = new int[size];

Random rand = new Random();
for (int i = 0; i < size; i++)
arr[i] = rand.nextInt(integerRange);
return arr;
}


Thanks for any input!




Aucun commentaire:

Enregistrer un commentaire