I'm looking for an automated test to validate that merge sort was actually implemented. I know how to validate that a sorting algorithm actually sorts, but how do I validate that merge sort was used instead of, say, bubble sort?
I looked back at my old lectures and they hardcoded a target time of 0.01s for a 20,000 element list. They did not leave any comment of how they came up with 0.01s. So my question is how do I validate that merge sort was implemented?
The following code shows this "ghost" 0.01 that the lecture came up with. Where the did this 0.01 come from?
FYI, BIG_SORT_SIZE = 20,000
/** @return true if test passes, else false */
private boolean testTimeToSortBigList() {
final int bigNum = BIG_SORT_SIZE; //okay, not THAT big
final double maxTime = 0.02;
final double targetTime = 0.01;
try {
IndexedUnsortedList<Integer> list1 = newList();
Random rand = new Random(123);
for (int i = 0; i < bigNum; i++) {
list1.add(new Integer(rand.nextInt()));
}
long startTime = System.nanoTime();
Sort.sort(list1);
long endTime = System.nanoTime();
long totalTime = endTime - startTime;
double seconds = (double)totalTime/10e9;
System.out.printf("\nTime to sort %d random integers: %.3f seconds\n", bigNum, seconds);
System.out.printf("Target time < %.3f seconds. Time > %.3f suggests O(n^2) runtime.\n", targetTime, maxTime);
return (seconds < maxTime);
} catch (Exception e) {
System.out.printf("caught unexpected %s\n", e.toString());
return false;
}
}
You could probably try with worst case examples to test the time differences for various algorithms . For example bubble sort on a very long sequence of decreasing numbers will perform much slower than merge sort for the same sequence. It is not automated but if you want to check the algorithm , this might be a brute force approach to it.