【问题标题】:Time taken for sorting using bubble sort and selection sort [closed]使用冒泡排序和选择排序进行排序所花费的时间[关闭]
【发布时间】:2017-05-01 10:14:53
【问题描述】:

为什么选择和冒泡排序的时间相同。我所有的排序方法都正常工作,但我得到了冒泡排序和选择排序的准确时间..

import java.util.Scanner;
public class sorting{
    public static void selectionsort(double[] list, int a, int b, double temp, int length){
        double min;
        int index=0;
        /*for(int p=0;p<list.length;p++){
            System.out.println("input="+list[p]);
        }*/
        int n=0;
        int status=0;
        while(n<length){
            min=list[n];
            for(int j=n;j<(length-1);j++){
                if(list[j+1]<min){
                    min=list[j+1];
                    index=(j+1);
                    status=1;
                }

            }
            if(min!=list[n]){
                temp=list[n];
                //System.out.println("Original val="+temp);
                //System.out.println("Before n="+n);
                list[n]=min;
                //System.out.println("After n="+n);
                //System.out.println("switch val="+list[n]);
                list[index]=temp;
                //System.out.println("new switch val at="+index);
                n++;
                //System.out.println("Updated");
                /*for(int k=0;k<list.length;k++){
                    System.out.println("Output="+list[k]);
                }*/
            }
            else 
                n++;
        }
        //System.out.println("Done selection:");
        /*for(int k=0;k<list.length;k++){
            System.out.println("Output="+list[k]);
        }*/
    }
    public static void bubblesort(double[] list, int a, int b, double temp, double length){
        /*for(int p=0;p<list.length;p++){
            System.out.println("inputb="+list[p]);
        }*/
        while(length>=0){
            for(int k=0;k<=(length-2);k++){
                if(list[k]>list[k+1]){
                    temp=list[k+1];
                    list[k+1]=list[k];
                    list[k]=temp;
                }
            }
            length--;
        }
        //System.out.println("Done bubble:");
        /*for(int k=0;k<list.length;k++){
            System.out.println("Outputb="+list[k]);
        }*/
    }
    public static void main(String[] args){
        Scanner in=new Scanner(System.in);
        System.out.println("Lower bound");
        int a=in.nextInt();
        System.out.println("High bound");
        int b=in.nextInt();
        System.out.println("how many elements?");
        int n=in.nextInt();
        double[] list=new double[n];
        double[] sel=new double[n];
        double[] bub=new double[n];
        for(int i=0;i<list.length;i++){//intializes the array 
            list[i]=((Math.random()*(b-a)))+a;
        }
        for(int h=0;h<sel.length;h++){//selection
            sel[h]=list[h];
        }
        for(int l=0;l<bub.length;l++){//bubble
            bub[l]=list[l];
        }
        double temp=0.0;
        int length=list.length;
        long startTime = System.nanoTime();
        selectionsort(sel,a,b,temp,length);
        long duration = System.nanoTime() - startTime;
        System.out.println("Time for selection="+(duration*1.0E-9));
        long startTime1 = System.nanoTime();
        bubblesort(bub,a,b,temp,length);
        long duration1 = System.nanoTime() - startTime1;
        System.out.println("Time for bubble="+(duration*1.0E-9));
    }
}

【问题讨论】:

  • 我发现了你的错误..."Time for bubble="+(duration*1.0E-9) 应该是"Time for bubble="+(duration1*1.0E-9)。其次,使用几百万个样本
  • 首先,运行一次基准测试不太可能对除 JIT 之外的任何内容进行基准测试。你用什么参数运行?几百万?
  • 您会在更大的样本上看到性能速度的差异。有关此问题的讨论,请参见 stackoverflow.com/questions/10428336/…

标签: java sorting


【解决方案1】:

因为您要打印两次duration,所以在第二次打印时您应该使用duration1

此外,正如鲍里斯在 cmets 部分中所写:这不是进行基准测试的方法。最好使用基准测试框架,该框架将负责预热 JVM 并多次运行这两种方法以找到平均运行时间等。

【讨论】:

    猜你喜欢
    • 2015-09-13
    • 2015-02-14
    • 2015-04-25
    • 2016-11-01
    • 2015-09-16
    • 1970-01-01
    • 2017-02-07
    相关资源
    最近更新 更多