As a sort of practice, I want to compare the average speeds of my implementations of Radix-, Merge- and Quicksort. To do that, I wrote something that reads an array of integers from a file and then passes it to the three algorithms. It repeats the process of sorting for a certain number of times and stores the time that the algorithm took in a separate list. At the end, it just displays the average run time for each algorithm.
The whole thing is working, except for when I want to have the average time of more than 10 runs. In that case, Java just spits out an IndexOutOfBoundsException.
Output for a sucessful run:
Doing 10 runs...
10 runs done. Calculating average run times...
Average run time for Radixsort: 87462 ns
Average run time for Mergesort: 42679 ns
Average run time for Quicksort: 116013 ns
And the output for a failed run:
Doing 11 runs...
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:653)
at java.util.ArrayList.get(ArrayList.java:429)
at sorter.Radix.sort(Radix.java:8)
at main.LoopHelper.call(LoopHelper.java:21)
at main.MainClass.main(MainClass.java:40)
For a better understanding:
- At MainClass.java:40 it tries to run a helper class (because I decided to go with multi-threading)
- At LoopHelper.java:21 the program tries to call the sorting algorithm specified when initializing the helper class
- At Radix.java:8 the Radixsort implementation tries to get the length of the first number (as a String) of the ArrayList passed as a first step on getting the max length of any number in the List
This is the point where I have no idea why the program crashes. The list that was passed on both example runs is exactly the same (here it had 10 elements) and on the first run it had no problems.I even let Java spit out the list that it got from the file to confirm there were no issues when getting the list.
So, does anyone have an idea what might be going on here?
(It seems to crash on the 11th run if that helps)