We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Hi everyone. I keep getting wrong answers even though my outputs are exactly the same as expected output. Does anyone has the same issue ? Here is my solution:
static void countSwaps(int[] a) {
int n = a.length;
int counter = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - 1; j++) {
if (a[j] > a[j + 1]) {
int temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
counter++;
}
}
}
System.out.println("Array is sorted in " + counter + " swaps.");
System.out.println("First element: " + a[0]);
System.out.println("Last element: " + a[n - 1]);
return;
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Sorting: Bubble Sort
You are viewing a single comment's thread. Return to all comments →
Hi everyone. I keep getting wrong answers even though my outputs are exactly the same as expected output. Does anyone has the same issue ? Here is my solution:
static void countSwaps(int[] a) {