Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Main {
- public static void main(String[] args) {
- int [] a = new int[]{4,3,1,2};
- minimumSwaps(a);
- }
- static int minimumSwaps(int[] arr) {
- int swapsCount = 0;
- int arraySize = arr.length;
- int[] sortedArray = arr.clone();
- Arrays.sort(sortedArray);
- for(int i = 0; i < arraySize; i++) {
- if (sortedArray[i] != arr[i])
- swapsCount++;
- for(int j = i+1; j < arraySize; j++) {
- if (arr[j] == sortedArray[i]) {
- int temp = arr[j];
- arr[j] = arr[i];
- arr[i] = temp;
- break;
- }
- }
- }
- return swapsCount;
- }
- }
Add Comment
Please, Sign In to add comment