Created
January 29, 2020 07:04
-
-
Save ShinJJang/c3e46ecc93833b57af680657d0fcf0d2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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