Minimum Swaps 2

  • + 0 comments

    My solution in java:

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