简体   繁体   English

测量某些排序算法的时间复杂度

[英]Measuring time complexity of some sorting algorithms

I am writing a demo class in Java to analyze the following sorting algorithms: 我正在用Java编写一个演示类来分析以下排序算法:

  1. InsertionSort 插入排序
  2. SelectionSort 选择排序
  3. BubbleSort 冒泡
  4. MergeSort 归并
  5. QuickSort 快速排序

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: 现在我有一些问题:

  1. Can I use these Arrays for all Best-, Average- and Worst-Cases of these Algorithms, or has fe the Worst-Case of MergeSort another order?! 我可以将这些数组用于这些算法的所有最佳,平均和最坏情况吗,或者让MergeSort的最坏情况又有其他顺序吗?
  2. Is there an easy way to unsort the arrays, after sorting them once? 在对数组排序一次之后,是否有一种简单的方法可以对数组进行排序?
  3. Is this anyway the "right way" to determine the time-complexity (maybe someone has a better idea)? 反正这是确定时间复杂性的“正确方法”(也许有人有更好的主意)?
  1. Your arrays are way too short: it will take almost no time for any "modern" CPU to sort them, even in your worst case 您的数组太短了:即使在最坏的情况下,任何“现代” CPU几乎都不会花时间对它们进行排序
  2. To have pertinent time variations based on the shuffleness of the input, you need to set an input size that is fixed and that gives you measurable times (probably in order of seconds) 要基于输入的混洗产生相关的时间变化,您需要设置一个固定的输入大小,并为您提供可测量的时间(可能以秒为单位)
  3. You probably need to generate a set of thousands of random arrays, add maybe some specific array to this set (sorted, reversed sorted, ...). 您可能需要生成成千上万个随机数组的集合,向该集合中添加一些特定的数组(排序,反向排序等)。 Then you can run each algorithm on each array from this set and measure the time needed to sort them. 然后,您可以对该集合中的每个数组运行每种算法,并测量对它们进行排序所需的时间。 Doing so you can obtain a nice distribution graph for each algorithm on which you can see the behavior of each algorithm (bubble sort goes very high while heapsort is pretty stable ...). 这样做,您可以为每种算法获得一个不错的分布图,在该分布图上可以看到每种算法的行为(气泡排序非常稳定,而堆排序非常稳定...)。 The worst input for one algorithm is not necessarily the same for an other algorithm, hence the set. 一种算法的最差输入不一定与另一种算法相同,因此是该集合。
  1. 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个元素组成数组(分别用于慢速和快速算法)。

  2. 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM