Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java气泡排序问题_Java_Runtime_Bubble Sort - Fatal编程技术网

Java气泡排序问题

Java气泡排序问题,java,runtime,bubble-sort,Java,Runtime,Bubble Sort,我编写了一个冒泡排序程序,可以将10000个唯一值按顺序排序 我已经运行了这个程序,它给了我一个时间输出(使用nanoTime)作为程序完成所需的时间,但我希望在程序代码中添加另一个输出;程序从开始到结束排序所需的移动次数 代码如下: public class BubbleSort { public static void main(String[] args) { int BubArray[] = new int[]{#here are 10000 integers#}; Sy

我编写了一个冒泡排序程序,可以将10000个唯一值按顺序排序

我已经运行了这个程序,它给了我一个时间输出(使用nanoTime)作为程序完成所需的时间,但我希望在程序代码中添加另一个输出;程序从开始到结束排序所需的移动次数

代码如下:

public class BubbleSort {
public static void main(String[] args) {
    int BubArray[] = new int[]{#here are 10000 integers#};
    System.out.println("Array Before Bubble Sort");
        for(int a = 0; a < BubArray.length; a++){
             System.out.print(BubArray[a] + " ");
        }

         double timeTaken = bubbleSortTimeTaken(BubArray);
            bubbleSort(BubArray);
            System.out.println("");               
            System.out.println("Array After Bubble Sort");
    System.out.println("    Time taken for Sort : " + timeTaken + " milliseconds.");                
            for(int a = 0; a < BubArray.length; a++){
                    System.out.print(BubArray[a] + " ");
        }
}

private static void bubbleSort(int[] BubArray) {

        int z = BubArray.length;
        int temp = 0;

        for(int a = 0; a < z; a++){
                for(int x=1; x < (z-a); x++){

                        if(BubArray[x-1] > BubArray[x]){

                                temp = BubArray[x-1];
                                BubArray[x-1] = BubArray[x];
                                BubArray[x] = temp;

                        }      
                }
        }
}
public static double bubbleSortTimeTaken(int[] BubArray) {
long startTime = System.nanoTime();
    bubbleSort(BubArray);
long timeTaken = System.nanoTime() - startTime;
return timeTaken;
}
}
但我希望在代码中添加另一个输出,它告诉我完成多少步(基本上是一个移动计数器),即:

Time taken for Sort : 1.6788472E7 milliseconds.
Total number of moves: 3000
这有意义吗? 感谢您的帮助。

试试以下方法:

// returns the number of switches
private int bubbleSort(int[] BubArray) {

        int z = BubArray.length;
        int temp = 0;
        int timesSwitched = 0;
        for(int a = 0; a < z; a++){
                for(int x=1; x < (z-a); x++){

                        if(BubArray[x-1] > BubArray[x]){

                                temp = BubArray[x-1];
                                BubArray[x-1] = BubArray[x];
                                BubArray[x] = temp;
                                timesSwitched++
                        }      
                }
        }
     return timesSwitched;
}
//返回开关的数量
私有int bubbleSort(int[]Bubaray){
int z=Bubaray.length;
内部温度=0;
int=0;
对于(int a=0;abubaray[x]){
温度=Bubaray[x-1];
布巴雷[x-1]=布巴雷[x];
Bubaray[x]=温度;
时间交换++
}      
}
}
返回时间切换;
}

我将更改
bubbleSort
以返回一个int并将您的值分配给它


对什么有意义?为什么您对这样一个值感兴趣?听起来您需要声明一个变量来跟踪移动次数,并在算法移动值时向其添加一个(++?)。然后您只需在末尾输出该变量两次?一次在
bubbleSortTimetake
中,一次在
main
中?如果您想获得运行时间,只需从
main
“itrs”中删除一个调用,我猜您用于整数的名称是什么?我很清楚,“时间复杂性”是指每次完成的移动次数?如:排序所用时间:3.8208782E7毫秒。时间复杂度:1447551哇,你帮了我很多忙!我对你感激不尽。此问题已回答,现在可以结束。谢谢你!不客气。很高兴能帮上忙。顺便说一句,这是作业吗?没错,我一直在努力计算执行按键操作的时间。我知道如何执行执行时间,但不是每一步。非常感谢。谢谢你的投入,我感谢你的帮助和迅速的反应。虽然foampile的回答确实解决了我的问题。
// returns the number of switches
private int bubbleSort(int[] BubArray) {

        int z = BubArray.length;
        int temp = 0;
        int timesSwitched = 0;
        for(int a = 0; a < z; a++){
                for(int x=1; x < (z-a); x++){

                        if(BubArray[x-1] > BubArray[x]){

                                temp = BubArray[x-1];
                                BubArray[x-1] = BubArray[x];
                                BubArray[x] = temp;
                                timesSwitched++
                        }      
                }
        }
     return timesSwitched;
}
private static int bubbleSort(int[] BubArray) {

        int z = BubArray.length;
        int temp = 0;

        int itrs = 0;

        for(int a = 0; a < z; a++){
                for(int x=1; x < (z-a); x++){

                        if(BubArray[x-1] > BubArray[x]){

                                temp = BubArray[x-1];
                                BubArray[x-1] = BubArray[x];
                                BubArray[x] = temp;


                        }    

                        itrs++;
                }
        }

        return itrs;
int itrs = bubbleSort(BubArray);
            System.out.println("");               
            System.out.println("Array After Bubble Sort");
    System.out.println("    Time taken for Sort : " + timeTaken + " milliseconds.");  
    System.out.println("Time complexity: " + itrs);