[英]Measuring time complexity of some sorting algorithms
I am writing a demo class in Java to analyze the following sorting algorithms: 我正在用Java编写一个演示类来分析以下排序算法:
which I have implemented as static methods in another class named Sort. 我在另一个名为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. 我想通过使用omicron公式确定运行时的分析复杂性来比较每种算法的最佳,平均和最坏情况。
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: 现在我有一些问题:
Such arrays can demonstrate worst and best cases for InsertionSort and BubbleSort. 对于InsertionSort和BubbleSort,此类数组可以展示最坏的情况和最好的情况。 Typical implementations of MergeSort and SelectionSort have the same complexity for all arrays.
MergeSort和SelectionSort的典型实现对所有数组都具有相同的复杂性。 The worst case for the simplest implementation of QuickSort is sorted (or back-sorted) array.
对于QuickSort的最简单实现,最坏的情况是排序(或反向排序)数组。
Wiki page with useful table 带有有用表的Wiki页面
Note that these arrays are too short to notice any difference in run times. 请注意,这些数组太短,无法注意到运行时间的任何差异。 Make arrays with 10^3-10^6 elements (for slow and fast algorithms respectively).
用10 ^ 3-10 ^ 6个元素组成数组(分别用于慢速和快速算法)。
Look at Fisher-Yates shuffle to get random sequence 查看Fisher-Yates随机播放以获取随机序列
@MBo @Jean Logeart @MBo @Jean Logeart
What do you think about that: 您对此有何看法:
//Main:
for(int n = 100_000; n <= 1_000_000; n = n + 100_000) {
//f.e. average case of insertion sort:
int[] arr = randomArray(n);
insertionSortWithRuntime(arr);
}
/**
* For best cases using sorted numbers.
* @param n- the length in which the array should be created.
* @return
*/
public static int[] sortedArray(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = i;
}
return arr;
}
/**
* For average cases using random numbers.
* @param n - the length in which the array should be created.
* @return
*/
public static int[] randomArray(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = (int) (Math.random() * 9 + 1);
}
return arr;
}
/**
* For worst cases using reversed sorted numbers.
* @param n - the length in which the array should be created.
* @return
*/
public static int[] reversedSortedArray(int n) {
int[] arr = new int[n];
int length = n - 1;
for (int i = 0; i < n; i++) {
arr[i] = length;
length--;
}
return arr;
}
Have you imagined it this way? 你有没有想过这种方式?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.