public class Sort{
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;
}
}
Output:
I wrote a program that executes bubbleSort method and returns the time it took in ms. I want to print the return value but I get 0 for all arrays. When I debug the program, I can see that its returning some other number. But when it comes to print, its printing 0. I dont understand the problem. Can someone please help me?
