我编写了一个 BubbleSort 程序,它运行良好,给了我一个很好的输出并且充分地完成了它的工作。但是我无法在整理一次后重新执行程序。即程序完成了一种 10000 个唯一数字并输出它所花费的时间和所花费的步数,但不会再次执行,比如在那之后再执行 999 次?
简而言之,任何人都可以帮助我让我的程序运行 1000 次,以便我能够获得平均执行时间吗?
这是代码:
public class BubbleSort {
public static void main(String[] args) {
int BubArray[] = new int[] { #10000 unique values unsorted# };
System.out.println("Array Before Bubble Sort");
for (int a = 0; a < BubArray.length; a++) {
System.out.print(BubArray[a] + " ");
}
double timeTaken = bubbleSortTimeTaken(BubArray);
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("Time Taken for Sort : " + timeTaken
+ " milliseconds.");
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 double bubbleSortTimeTaken(int[] BubArray) {
long startTime = System.nanoTime();
bubbleSort(BubArray);
long timeTaken = System.nanoTime() - startTime;
return timeTaken;
}
}
以下是结果输出(注意它仅限于一次运行):
Unsorted List :
[13981, 6793, 2662, 733, 2850, 9581, 7744 .... ]
Sorted List with BubbleSort
Moves Taken to Sort : 1447551 Moves.
Time Taken to Sort : 1.2483121E7 Milliseconds.
[10, 11, 17, 24, 35, 53, 57, 60, 78, 89, 92 ... ]