嗨我已经用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)已经被测试。我希望找到平均,最好和最坏的情况下绘制。
任何帮助,将不胜感激,谢谢。
修好了,谢谢。它现在输出正确的(我期望的)结果。你帮了我很多。我很感激! –
欢迎您:) – axiom