As part of a school exercise I would like to compare and contrast sorting algorithms as a Java exercise.
I implemented the sorting algorithms myself and we sort objects of a class Person
which implements the Comparable
interface.
So far so good, but what I can't explain is why during the first call to my sorting methods, the sorting takes longer than on subsequent calls?
The output bellow represents my results.
Best, Worst and Avg refer to the unsorted array that is passed to the sorting method:
- Best: the array is already sorted
- Worst: the array is sorted in reverse order
- Avg: the objects in the array are at random order
This is my output:
1-call of the sorting methods
InsertionSort Best:1799ms Worst:78ms Avg:789ms
MergeSort Best:10ms Worst:3ms Avg:5ms
2-call of the sorting methods
InsertionSort Best:1065ms Worst:39ms Avg:691ms
MergeSort Best:3ms Worst:2ms Avg:5ms
3-call of the sorting methods
InsertionSort Best:1066ms Worst:39ms Avg:692ms
MergeSort Best:3ms Worst:2ms Avg:5ms
4-call of the sorting methods
InsertionSort Best:1065ms Worst:39ms Avg:691ms
MergeSort Best:3ms Worst:2ms Avg:5ms
Is the JVM doing any optimizations on subsequent calls?
I am puzzled and would greatly appreciate any help!
Edit: Thanks for suggestions and answers so far!
To make a few points clear - each of the calls in my output refer to the time it take for a complete sorting!
After each sorting I make a new call with UNSORTED arrays again!
My source code can be downloaded as an eclipse project as a zip file, at the following Dropbox link: dropbox link to eclipse project.zip
P.S. I have no experience with profilers - if you could point me to a tutorial or so that would be great.