【问题标题】:Array Sorting Performance数组排序性能
【发布时间】:2013-03-14 03:09:55
【问题描述】:

这是一个非常简单直接的问题,但是,当然,我已经设法做错了。首先,我生成了 5 个包含 10 个随机数的不同数组——从 1 到 10、1 到 100、最多 1 到 100,000。然后我对每个数组执行 5 种不同类型的排序(总共 25 种),计算执行排序所需的时间。无论 n 的大小如何,我都无法弄清楚为什么每个结果都是 0ms。我做错了什么?

public class Lab16Sorting {

public static void main(String[] args) 
{
    final int TOTAL_NUMBERS = 10;
    int count;
    int[] num = new int[TOTAL_NUMBERS];
    Random rand = new Random();

    // Generate 10 numbers from 1 - 10  
    System.out.println("SORT 10");
    System.out.println("----------------");

    for (count = 0; count < TOTAL_NUMBERS; count++)
        num[count] = rand.nextInt(10);

    System.out.println("Array: " + num);
    runSort(num);

    // Generate 10 numbers from 1 - 100     
    System.out.println("\nSORT 100");
    System.out.println("----------------");

    for (count = 0; count < TOTAL_NUMBERS; count++)
        num[count] = rand.nextInt(100);

    System.out.println("Array: " + num);
    runSort(num);

    // Generate 10 numbers from 1 - 1,000       
    System.out.println("\nSORT 1,000");
    System.out.println("----------------");

    for (count = 0; count < TOTAL_NUMBERS; count++)
        num[count] = rand.nextInt(1000);

    System.out.println("Array: " + num);
    runSort(num);

    // Generate 10 numbers from 1 - 10,000  
    System.out.println("\nSORT 10,000");
    System.out.println("----------------");

    for (count = 0; count < TOTAL_NUMBERS; count++)
        num[count] = rand.nextInt(10000);

    System.out.println("Array: " + num);
    runSort(num);

    // Generate 10 numbers from 1 - 100,000     
    System.out.println("\nSORT 100,000");
    System.out.println("----------------");

    for (count = 0; count < TOTAL_NUMBERS; count++)
        num[count] = rand.nextInt(100000);

    System.out.println("Array: " + num);
    runSort(num);
}

/**
 * Run sort algorithms
 */

private static void runSort(int[] num)
{
    long before;
    long after;

    // Run and display selection sort
    before = System.currentTimeMillis();
    selectionSort(num);     
    after = System.currentTimeMillis();
    System.out.println("Selection sort took "+ (after-before) +" milliseconds");

    // Run and display bubble sort
    before = System.currentTimeMillis();
    bubbleSort(num);
    after = System.currentTimeMillis();
    System.out.println("Bubble sort took "+ (after-before) +" milliseconds");

    // Run and display insertion sort
    before = System.currentTimeMillis();
    insertionSort(num);     
    after = System.currentTimeMillis();
    System.out.println("Insertion sort took "+ (after-before) +" milliseconds");

    // Run and display merge sort
    before = System.currentTimeMillis();
    mergeSort(num);
    after = System.currentTimeMillis();
    System.out.println("Merge sort took "+ (after-before) +" milliseconds");

    // Run and display quick sort
    before = System.currentTimeMillis();
    quickSort(num);
    after = System.currentTimeMillis();
    System.out.println("Quick sort took "+ (after-before) +" milliseconds");        
}

我打印出各种数组地址,我发现它们都是一样的(这是有道理的,因为我使用的是同一个数组对象)。我认为这是问题所在,因此我尝试使用不同的数组(int[] numint[] num2...),并尝试在每次使用num = new int[TOTAL_NUMBERS] 调用runSort() 方法后重新初始化数组。

【问题讨论】:

  • 使用 System.nanoTime() 而不是 System.currentTimeMillis()

标签: java arrays sorting


【解决方案1】:

这是因为10 的大小太小,无法真正找出各种类型之间的时间差异。尝试将您的尺寸增加到 50,0001,00,000 左右的某个位置,以便真正能够看到差异(即使那样它也会在几秒钟内出现)。

如果你的机器可以承受足够的负载,那么就对10,00,000范围内的元素进行排序(仅用于测试时差是非常不可取的)。

【讨论】:

  • 但赋值要求是:为以下每个长度生成一个随机整数数组:n = 10,n = 100,n = 1,000,n = 10,000,n = 100,000。由于我的导师提供了这些数字,我认为它们会起作用并提供不同的结果。我刚刚尝试了 100,000,000 并且花了 0 毫秒。会不会是我的电脑太快了?
  • 您没有注意到的是,您的数组大小保持在same(10),只是其中生成的随机整数的范围在变化。所以没多大关系。将您的 TOTAL_NUMBERS 增加到 50000 看看会发生什么(先尝试 10000,因为您的系统可能会挂起 50000)。
  • @stef2dotoh 你的数组每次的大小都是 10,你只是在每次测试中用 0 到 10、0 到 100 (等等)的数字填充数组。
  • @stef2dotoh 如果您创建一个接收数组大小并完成所有工作的方法会更好:创建数组,用随机数填充并对其进行排序,然后从您的主要方法只需用 10、100、1000 调用此方法...顺便说一句,您的计算机并没有您想象的那么快,不要骗自己:)。
  • 啊!然后我错误地阅读了说明。我在想我必须在不同的范围内生成 10 个数字。每次清楚地排序 10 个数字将产生相同的结果。谢谢!
【解决方案2】:

RJ 是正确的,你的数组太小以至于排序算法无关紧要。

另见此贴 Test case for Insertion Sort, MergeSort and Quick Sort

【讨论】:

    【解决方案3】:

    排序算法是经过调整的快速排序,改编自 Jon L. Bentley 和 M. Douglas McIlroy 的“Engineering a Sort Function”,Software-Practice and Experience,Vol. 23(11) P. 1249-1265(1993 年 11 月)。该算法在许多数据集上提供 n*log(n) 性能,导致其他快速排序降低到二次性能。 以上是java文档。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-28
      • 2019-08-24
      • 1970-01-01
      • 2012-07-07
      • 2023-04-04
      • 2010-09-14
      相关资源
      最近更新 更多