I am writing a demo class in Java to analyze the following sorting algorithms:
- InsertionSort
- SelectionSort
- BubbleSort
- MergeSort
- QuickSort
which I have implemented as static methods in another class named Sort.
I want to compare the Best-, Average- and Worst-Cases of each algorithm by determining the runtime with the analytical komplexity using the omicron formula.
In the demo class, I only want to determine the time (in nanoseconds) each algorithm needs to sort an Integer Array with different lengths in the Best-, Average- and Worst-Case order of numbers in the Array.
//Best-Case
int[] arrbc0 = {1};
int[] arrbc1 = {1, 2};
int[] arrbc2 = {1, 2, 3};
int[] arrbc3 = {1, 2, 3, 4, 5};
int[] arrbc4 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int[] arrbc5 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
//Average-Case
int[] arrac1 = {1, 2};
int[] arrac2 = {3, 1, 2};
int[] arrac3 = {4, 2, 3, 1, 5};
int[] arrac4 = {9, 1, 10, 6, 2, 4, 8, 3, 7, 5};
int[] arrac5 = {13, 12, 1, 15, 5, 6, 7, 2, 14, 10, 3, 8, 4, 9, 11};
//Worst-Case
int[] arrwc1 = {2, 1};
int[] arrwc2 = {3, 2, 1};
int[] arrwc3 = {5, 4, 3, 2, 1};
int[] arrwc4 = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
int[] arrwc5 = {15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
//InsertionSort:
isNanoTime(arrbc0); //first load
isNanoTime(arrbc1);
isNanoTime(arrbc2);
//...
public static void isNanoTime(int[] arr) {
long a1 = System.nanoTime();
Sort.insertionSort(arr);
long a2 = System.nanoTime() - a1;
System.out.println(a2);
}
Now I have some questions:
- Can I use these Arrays for all Best-, Average- and Worst-Cases of these Algorithms, or has f.e. the Worst-Case of MergeSort another order?!
- Is there an easy way to unsort the arrays, after sorting them once?
- Is this anyway the "right way" to determine the time-complexity (maybe someone has a better idea)?