公共类排序{
public static void main(String[] args) {
int[] random = { 8, 32, 26, 1870, 3, 80, 46, 37 };
int[] decreasing = { 1870, 80, 46, 37, 32, 26, 8, 3 };
int[] increasing = { 3, 8, 26, 32, 37, 46, 80, 1870 };
System.out.println("|\t\t| random |decreasing|increasing|");
System.out.println("|---------------|-----------|-----------|----------|");
System.out.println("|Bubblesort\t| " + bubbleSort(random) + " ms\t|" + bubbleSort(decreasing) + " ms\t|"
+ bubbleSort(increasing) + " ms\t|");
}
public static long bubbleSort(int[] arr) {
long start = System.currentTimeMillis();
for (int j = 0; j < arr.length - 1; j++) {
for (int i = 0; i < arr.length - j - 1; i++) {
if (arr[i + 1] < arr[i]) {
arr[i + 1] = arr[i] + arr[i + 1];
arr[i] = arr[i + 1] - arr[i];
arr[i + 1] = arr[i + 1] - arr[i];
}
}
}
return System.currentTimeMillis() - start;
}
}
输出:
我写了一个程序,它执行 bubbleSort 方法并返回它所花费的时间,以 ms 为单位。我想打印返回值,但我得到所有数组的 0。当我调试程序时,我可以看到它返回了一些其他数字。但是当涉及到打印时,它的打印为 0。我不明白这个问题。有人可以帮帮我吗?
