This is my first question here, and I truly am not sure if I am doing something wrong, or if its even possible, I saw it suggested here on stack overflow, however I am not sure if it works for my specific situation.
I am doing a challenge on Hackerrank.com, and It is making me enter an Array(not an ArrayList) and it wants me to swap the items until they are all in ascending order.
I know I can use a temp variable to hold one of the values and swap it out, but I saw someone recommend to use Collections.swap and here is how I tried to implement it:
for (int i = 0; i < n; i++){
int numSwaps = 0;
for(int j = 0; j < n - 2; j++) {
if(a[j] > a[j+1]) {
Collections.swap(Arrays.asList(a), j,j+1);
numSwaps += 1;
}
totalSwaps += numSwaps;
}
if(numSwaps == 0) {
break;
}
}
Am i doing using it wrong? Or is it something that is just not possible?
Thank you very much!