Java 阵列排序性能

Java 阵列排序性能,java,arrays,sorting,Java,Arrays,Sorting,这是一个相当简单、直截了当的问题,但是,当然,我已经设法做了一些错事。首先,我生成了5个不同的10个随机数数组——从1到10,从1到100,从1到100000。然后,我对每个数组执行5种不同类型的排序(总共25种),计算执行排序所需的时间。我不明白为什么不管n的大小,每个结果都是0毫秒。我做错了什么 public class Lab16Sorting { public static void main(String[] args) { final int TOTAL_NUMBERS

这是一个相当简单、直截了当的问题,但是,当然,我已经设法做了一些错事。首先,我生成了5个不同的10个随机数数组——从1到10,从1到100,从1到100000。然后,我对每个数组执行5种不同类型的排序(总共25种),计算执行排序所需的时间。我不明白为什么不管n的大小,每个结果都是0毫秒。我做错了什么

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");        
}
公共类Lab16Sorting{
公共静态void main(字符串[]args)
{
最终整数总数=10;
整数计数;
int[]num=新的int[总数];
Random rand=新的Random();
//从1到10生成10个数字
System.out.println(“排序10”);
系统输出打印项次(“------------------------------”);
对于(计数=0;计数<总数;计数++)
num[count]=rand.nextInt(10);
System.out.println(“数组:+num”);
运行排序(num);
//从1到100生成10个数字
System.out.println(“\n传感器100”);
系统输出打印项次(“------------------------------”);
对于(计数=0;计数<总数;计数++)
num[count]=rand.nextInt(100);
System.out.println(“数组:+num”);
运行排序(num);
//从1到1000生成10个数字
System.out.println(“\n传感器1000”);
系统输出打印项次(“------------------------------”);
对于(计数=0;计数<总数;计数++)
num[count]=rand.nextInt(1000);
System.out.println(“数组:+num”);
运行排序(num);
//从1到10000生成10个数字
System.out.println(“\nSORT 10000”);
系统输出打印项次(“------------------------------”);
对于(计数=0;计数<总数;计数++)
num[count]=rand.nextInt(10000);
System.out.println(“数组:+num”);
运行排序(num);
//从1到100000生成10个数字
System.out.println(“\nSORT 100000”);
系统输出打印项次(“------------------------------”);
对于(计数=0;计数<总数;计数++)
num[count]=兰特·奈克斯汀(100000);
System.out.println(“数组:+num”);
运行排序(num);
}
/**
*运行排序算法
*/
私有静态void运行排序(int[]num)
{
很久以前;
很久以后;
//运行并显示选择排序
before=System.currentTimeMillis();
选择排序(num);
after=System.currentTimeMillis();
System.out.println(“选择排序时间”+(之前之后)+“毫秒”);
//运行并显示气泡排序
before=System.currentTimeMillis();
气泡运动(num);
after=System.currentTimeMillis();
System.out.println(“气泡排序时间”+(之前之后)+“毫秒”);
//运行并显示插入排序
before=System.currentTimeMillis();
插入排序(num);
after=System.currentTimeMillis();
System.out.println(“插入排序时间”+(之前之后)+“毫秒”);
//运行并显示合并排序
before=System.currentTimeMillis();
合并排序(num);
after=System.currentTimeMillis();
System.out.println(“合并排序时间”+(之前之后)+“毫秒”);
//运行并显示快速排序
before=System.currentTimeMillis();
快速排序(num);
after=System.currentTimeMillis();
System.out.println(“快速排序时间”+(之前之后)+“毫秒”);
}

我打印出了各种数组地址,发现它们都是一样的(因为我使用的是同一个数组对象,这很有意义)。我认为这就是问题所在,所以我尝试使用不同的数组(
int[]num
int[]num2
),并尝试在每次
runSort()之后重新初始化数组
num=new int[TOTAL\u NUMBERS]调用
method,这是因为
10
的大小太小,无法实际找出不同类型排序之间的计时差异。尝试将大小增加到
50000
100000
,以便能够看到差异(即使那样,也要过几秒钟)


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

RJ是正确的,您的数组太小,排序算法无关紧要

也看到这个帖子了吗

排序算法是一种经过调整的快速排序算法,改编自Jon L.Bentley和M.Douglas McIlroy的“设计排序函数”,软件实践与经验,第23卷(11)p.1249-1265(1993年11月)。该算法在许多数据集上提供n*log(n)性能,这些数据集会导致其他快速排序降级为二次性能。
上面是java文档。

但作业要求是:为以下每个长度生成一个随机整数数组:n=10,n=100,n=1000,n=10000,n=100000。由于我的导师提供了这些数字,我认为它们会起作用并提供不同的结果。我刚刚尝试了100000000,花了0毫秒。我的com是否可能计算机太快了?你没有注意到的是,数组的大小保持不变(10)
,只是其中生成的随机整数的范围在变化。所以这没什么大不了的。将你的
总数增加到
50000
,看看会发生什么(先尝试10000,因为你的系统可能会挂起50000)@stef2dotoh每次你的数组大小都是10,你只是用0到10,0到100的数字填充数组(等等)在每次测试中。@stef2dot如果你制作一个接收数组大小的方法并完成所有工作,那就更好了:创建数组,用随机数填充并排序,然后从你的主方法调用这个方法,用10,100,1000…顺便说一句,你的计算机没有你想象的那么快,不要撒谎:).啊!然后我错误地阅读了说明。我想我必须在不同的范围内生成10个数字。显然,每次对10个数字进行排序将得到相同的结果。谢谢!使用Sy