Skip to content

Instantly share code, notes, and snippets.

@ShinJJang
Created January 29, 2020 07:04
Show Gist options
  • Save ShinJJang/c3e46ecc93833b57af680657d0fcf0d2 to your computer and use it in GitHub Desktop.
Save ShinJJang/c3e46ecc93833b57af680657d0fcf0d2 to your computer and use it in GitHub Desktop.
static int minimumSwaps(int[] arr) {
if (arr.length == 1) {
return 0;
}
int swapCount = 0;
int temp;
for (int i = 1; i <= arr.length; i++) {
if (i != arr[i-1]) {
temp = arr[i-1];
arr[i-1] = arr[temp-1];
arr[temp-1] = temp;
swapCount++;
i--;
}
}
return swapCount;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment