Minimum Swaps 2

  • + 0 comments

    Java solution:

        static int minimumSwaps(int[] arr) {
            int swaps = 0;
            for (int i = 0; i < arr.length; i++) {
                while (arr[i] != i + 1) {
                    int temp = arr[arr[i] - 1];
                    arr[arr[i] - 1] = arr[i];
                    arr[i] = temp;
                    swaps++;
                }
            }
            return swaps;
        }