我正在用Java编写一个演示类来分析以下排序算法:
我在另一个名为Sort的类中实现了静态方法。
我希望通过使用omicron公式确定运算符和分析komplexity来比较每种算法的Best,Average和Worst-Case。
在演示类中,我只想确定每个算法需要按照数组中最佳,平均和最坏情况顺序对不同长度的整数数组进行排序的时间(以纳秒为单位)。 p>
//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);
}
现在我有一些问题:
答案 0 :(得分:1)
答案 1 :(得分:0)
此类数组可以显示InsertionSort和BubbleSort的最差和最佳情况。 MergeSort和SelectionSort的典型实现对于所有阵列具有相同的复杂性。最简单的QuickSort实现的最坏情况是排序(或反向排序)数组
Wiki page with useful table
请注意,这些数组太短,无法发现运行时间的任何差异。使数组具有10 ^ 3-10 ^ 6个元素(分别用于慢速和快速算法)。
查看Fisher-Yates shuffle获取随机序列
答案 2 :(得分:0)
@MBo @Jean Logeart
您如何看待这个:
//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;
}
你有没有想过这样?