Java冒泡排序麻烦

时间:2012-12-06 21:51:01

标签: java runtime bubble-sort

我写了一个冒泡排序程序,按顺序对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;
}
}

代码执行并输出我想要的方式:

Array Before Bubble Sort
13981 6793 2662 10986 733 10107 2850 ... 
Array After Bubble Sort
10 11 17 24 35 53 57 60 61 78 83 89 128 131 138 141 ....
Time taken for Sort : 1.6788472E7 milliseconds.

但是我希望在代码中添加另一个输出,它告诉我完成了多少动作(基本上是一个移动计数器),即:

Time taken for Sort : 1.6788472E7 milliseconds.
Total number of moves: 3000

这有意义吗? 任何帮助将不胜感激,谢谢。

2 个答案:

答案 0 :(得分:1)

试试这个:

// 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;
}

答案 1 :(得分:1)

我会更改bubbleSort以返回int并将time complexity分配给它。

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;

}

然后在main

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);