Minimum Swap Sort

Sorting Algorithms Array

Problem Description

On November 12, as we celebrate technological progress and problem-solving in tech, your task is to work with a sorting-related challenge. Given an array of distinct integers, determine the minimum number of swaps required to sort the array in ascending order. In each swap, you can exchange the positions of any two elements in the array.

Input

  • An array of distinct integers.

Output

  • An integer representing the minimum number of swaps required to sort the array in ascending order.

Example

For the input:

[4, 3, 1, 2]

One way to sort the array is:

  1. Swap index 0 and index 2: [1, 3, 4, 2]
  2. Swap index 1 and index 3: [1, 2, 4, 3]
  3. Swap index 2 and index 3: [1, 2, 3, 4]

This requires a total of 3 swaps, so the output should be 3.

Starter Code

Below is some language-agnostic starter code to help you begin your solution.