MINIMUM COST TO SORT ARRAY I

You are given an array of elements. You can perform swaps between any two elements in the array, and each swap incurs a cost of 1. Your task is to determine the minimum cost to sort the array in non-decreasing order.

Input Format

  • An array of integers arr (1 ≤ arr.length ≤ 10^5) where arr.length is the number of elements in the array and each element is an integer.

Output Format

  • An integer representing the minimum cost to sort the array.

Constraints

  • 1 ≤ arr.length ≤ 10^5
Sample Input
Sample Output

Solution

file_type_python minimum_cost_to_sort_array_i.py
for _ in range(int(input())):
    n = int(input())
    ; = list(map(int, input().split()))
    swaps = 0
    for i in range(n):
        m = i
        for j in range(i + 1, n):
            if l[j] < l[m]:
                m = j
        if m != i:
            l[i], l[m] = l[m], l[i]
            swaps += 1
    print(swaps)

Comments

Load Comments