我已经编写了一个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 ... ]
答案 0 :(得分:1)
编辑代码......
public class BubbleSort {
static double bestTime = 10000000, worstTime = 0; //global variables
public static void main(String[] args) {
int BubArray[] = new int[]{3,5,3,2,5,7,2,5,8};
System.out.println("Array Before Bubble Sort");
for(int a = 0; a < BubArray.length; a++){
System.out.print(BubArray[a] + " ");
}
System.out.println("\n Entering Loop...");
for(int i=0; i<1000;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)/1000000d;
if(timeTaken > 0)
{
if(timeTaken > worstTime)
{
worstTime = timeTaken;
}
else if(timeTaken < bestTime)
{
bestTime = timeTaken;
}
}
System.out.println("Loop number: "+n + " Time Taken: " + timeTaken);
}
}
答案 1 :(得分:-1)
移动下面的方法
private static int bubbleSort(int[] BubArray)
到其他一些扩展Thread
的班级。也许你可以在每次执行完成时创建新线程。根类中的静态实例变量可用于保存时间。