Minimum Swaps 2

  • + 0 comments

    Python solution with all the base conditions :

    def minimumSwaps(arr):
        
        arr_sorted = False
        current_pointer = 0
        swaps = 0
        
        while not arr_sorted :
            if (current_pointer == len(arr)) :
                arr_sorted = True
                break
            
            if (current_pointer+1 == arr[current_pointer]) :
                current_pointer += 1
            else :
                ind = arr[current_pointer] - 1
                arr[current_pointer], arr[ind] = arr[ind], arr[current_pointer]
                swaps += 1
            
        return swaps