2012-12-09 68 views
1

嗨我已经用JAVA写了一个气泡排序应用程序。该应用程序正在充分工作,并提供足够的输出,但它并没有给我一个平均时间。BubbleSort输出图形故障

这里是我的代码:

public class BubbleSort { 
static double bestTime = 10000000, worstTime = 0; 
public static void main(String[] args) { 
    int BubArray[] = new int[]{**#interger values are placed in here#**}; 
     System.out.println("Unsorted List Before Bubble Sort"); 
     for(int a = 0; a < BubArray.length; a++){ 
     System.out.print(BubArray[a] + " "); 
      } 

     System.out.println("\n Bubble Sort Execution ..."); 

        for(int i=0; i<10000;i++) { 
         bubbleSortTimeTaken(BubArray, i); 
         } 

     int itrs = bubbleSort(BubArray); 
    System.out.println("");    
    System.out.println("Array After Bubble Sort"); 
    System.out.println("Moves Taken for Sort : " + itrs + " Moves."); 
    System.out.println("BestTime: " + bestTime + " WorstTime: " + worstTime); 
    System.out.print("Sorted Array: \n"); 

     for(int a = 0; a < BubArray.length; a++){ 
       System.out.print(BubArray[a] + " "); 
     } 
} 

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

public static void bubbleSortTimeTaken(int[] BubArray, int n) 
{ 
long startTime = System.nanoTime(); 
    bubbleSort(BubArray); 
    double timeTaken = (System.nanoTime() - startTime); 
    if (timeTaken > 0) 
    { 
     worstTime = timeTaken; 
    } 
    else if (timeTaken < bestTime) 
    { 
     bestTime = timeTaken; 
    } 

    System.out.println(n + "," + timeTaken); 

} 
} 

但我不确定如何一)取得的平均时间为每也是我怎么会画出最好的,平均和最差情形结果在图上。

下面是一个示例输出:

Unsorted List Before Bubble Sort 
#Integers Values of Unsorted List# 

Bubble Sort Execution ... **#(execution number, time in nanoseconds)#** 
0,6336584.0 
1,5063668.0 
2,3364580.0 
3,3373289.0 
4,3755912.0 
5,3383866.0 
.... 
9995,3431772.0 
9996,3368312.0 
9997,3743469.0 
9998,4639362.0 
9999,3433638.0 

Moves Taken for Sort : 499500 Moves. 
BestTime: 1.0E7 WorstTime: 3433638.0 

而且我不知道,如果是1.0E7给出了程序的每次运行,无论整数数量的使用 我bubbleSortTimeTaken()功能的正常使用(100 ,1000,10000,100000,1000000)已经被测试。我希望找到平均,最好和最坏的情况下绘制。

任何帮助,将不胜感激,谢谢。

回答

1
if (timeTaken > 0) // <- PROBLEM, Shouldn't be zero 
    { 
     worstTime = timeTaken; 
    } 
    else if (timeTaken < bestTime) //<- PROBLEM! , the 2 comparisons are unrelated 
    { 
     bestTime = timeTaken; 
    } 

就在那里。如果timeTaken> 0,它将主要是,else if永远不会执行。
因此,bestTime永远不会更新,并保留原始值(1E7)。

为了解决这个问题,该功能应该被修改的样子:

public static void bubbleSortTimeTaken(int[] BubArray, int n) 
{ 
long startTime = System.nanoTime(); 
    bubbleSort(BubArray); 
    double timeTaken = (System.nanoTime() - startTime); 
    if (timeTaken > worstTime) 
    { 
     worstTime = timeTaken; 
    } 
    if (timeTaken < bestTime) 
    { 
     bestTime = timeTaken; 
    } 

    System.out.println(n + "," + timeTaken); 

} 
} 

注意,我添加了一个可能的修复约worstTime了。

+0

修好了,谢谢。它现在输出正确的(我期望的)结果。你帮了我很多。我很感激! –

+0

欢迎您:) – axiom

1
if (timeTaken > 0) 
{ 
    worstTime = timeTaken; 
} 

这应该是

if (timeTaken > worstTime) 
{ 
    worstTime = timeTaken; 
} 

否则会无法正确设置此。这就解释了为什么你的besttime总是10e7。

要查找平均值,只需执行一定次数,记录所有时间,然后将其添加并除以完成的次数。

+0

感谢您的意见,关于平均的部分已经帮了我很多! –