Minimum number of swaps required to sort an array of first N number
Last Updated :
16 Aug, 2021
Given an array arr[] of distinct integers from 1 to N. The task is to find the minimum number of swaps required to sort the array.
Example:
Input: arr[] = { 7, 1, 3, 2, 4, 5, 6 }
Output: 5
Explanation:
i arr swap (indices)
0 [7, 1, 3, 2, 4, 5, 6] swap (0, 3)
1 [2, 1, 3, 7, 4, 5, 6] swap (0, 1)
2 [1, 2, 3, 7, 4, 5, 6] swap (3, 4)
3 [1, 2, 3, 4, 7, 5, 6] swap (4, 5)
4 [1, 2, 3, 4, 5, 7, 6] swap (5, 6)
5 [1, 2, 3, 4, 5, 6, 7]
Therefore, total number of swaps = 5
Input: arr[] = { 2, 3, 4, 1, 5 }
Output: 3
Approach:
- For each index in arr[].
- Check if the current element is in it's right position or not. Since the array contains distinct elements from 1 to N, we can simply compare the element with it's index in array to check if it is at its right position.
- If current element is not at it's right position then swap the element with the element which has occupied its place.
- Else check for next index.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
// Function to find minimum swaps
int minimumSwaps(int arr[],int n)
{
// Initialise count variable
int count = 0;
int i = 0;
while (i < n)
{
// If current element is
// not at the right position
if (arr[i] != i + 1)
{
while (arr[i] != i + 1)
{
int temp = 0;
// Swap current element
// with correct position
// of that element
temp = arr[arr[i] - 1];
arr[arr[i] - 1] = arr[i];
arr[i] = temp;
count++;
}
}
// Increment for next index
// when current element is at
// correct position
i++;
}
return count;
}
// Driver code
int main()
{
int arr[] = { 2, 3, 4, 1, 5 };
int n = sizeof(arr)/sizeof(arr[0]);
// Function to find minimum swaps
cout << minimumSwaps(arr,n) ;
}
// This code is contributed by AnkitRai01
Java
// Java program to find the minimum
// number of swaps required to sort
// the given array
import java.io.*;
import java.util.*;
class GfG {
// Function to find minimum swaps
static int minimumSwaps(int[] arr)
{
// Initialise count variable
int count = 0;
int i = 0;
while (i < arr.length) {
// If current element is
// not at the right position
if (arr[i] != i + 1) {
while (arr[i] != i + 1) {
int temp = 0;
// Swap current element
// with correct position
// of that element
temp = arr[arr[i] - 1];
arr[arr[i] - 1] = arr[i];
arr[i] = temp;
count++;
}
}
// Increment for next index
// when current element is at
// correct position
i++;
}
return count;
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 2, 3, 4, 1, 5 };
// Function to find minimum swaps
System.out.println(minimumSwaps(arr));
}
}
Python3
# Python3 program to find the minimum
# number of swaps required to sort
# the given array
# Function to find minimum swaps
def minimumSwaps(arr):
# Initialise count variable
count = 0;
i = 0;
while (i < len(arr)):
# If current element is
# not at the right position
if (arr[i] != i + 1):
while (arr[i] != i + 1):
temp = 0;
# Swap current element
# with correct position
# of that element
temp = arr[arr[i] - 1];
arr[arr[i] - 1] = arr[i];
arr[i] = temp;
count += 1;
# Increment for next index
# when current element is at
# correct position
i += 1;
return count;
# Driver code
if __name__ == '__main__':
arr = [ 2, 3, 4, 1, 5 ];
# Function to find minimum swaps
print(minimumSwaps(arr));
# This code is contributed by 29AjayKumar
C#
// C# program to find the minimum
// number of swaps required to sort
// the given array
using System;
class GfG
{
// Function to find minimum swaps
static int minimumSwaps(int[] arr)
{
// Initialise count variable
int count = 0;
int i = 0;
while (i < arr.Length)
{
// If current element is
// not at the right position
if (arr[i] != i + 1)
{
while (arr[i] != i + 1)
{
int temp = 0;
// Swap current element
// with correct position
// of that element
temp = arr[arr[i] - 1];
arr[arr[i] - 1] = arr[i];
arr[i] = temp;
count++;
}
}
// Increment for next index
// when current element is at
// correct position
i++;
}
return count;
}
// Driver code
public static void Main(String[] args)
{
int []arr = { 2, 3, 4, 1, 5 };
// Function to find minimum swaps
Console.WriteLine(minimumSwaps(arr));
}
}
// This code is contributed by 29AjayKumar
JavaScript
<script>
// javascript program to find the minimum
// number of swaps required to sort
// the given array
// Function to find minimum swaps
function minimumSwaps(arr)
{
// Initialise count variable
let count = 0;
let i = 0;
while (i < arr.length)
{
// If current element is
// not at the right position
if (arr[i] != i + 1)
{
while (arr[i] != i + 1)
{
let temp = 0;
// Swap current element
// with correct position
// of that element
temp = arr[arr[i] - 1];
arr[arr[i] - 1] = arr[i];
arr[i] = temp;
count++;
}
}
// Increment for next index
// when current element is at
// correct position
i++;
}
return count;
}
// Driver code
let arr = [2, 3, 4, 1, 5 ];
// Function to find minimum swaps
document.write(minimumSwaps(arr));
</script>
Time Complexity: O(N) where N is the size of array.
Auxiliary Space: O(1)
Similar Reads
Minimum number of swaps required to sort an array | Set 2 Given an array of N distinct elements, find the minimum number of swaps required to sort the array. Note: The problem is not asking to sort the array by the minimum number of swaps. The problem is to find the minimum swaps in which the array can be sorted. Examples: Input: arr[] = {4, 3, 2, 1} Outpu
7 min read
Minimum number of moves required to sort Array by swapping with X Given an integer array, arr[] of size N and an integer X. The task is to sort the array in increasing order in a minimum number of moves by swapping any array element greater than X with X any number of times. If it is not possible print -1. Examples: Input: arr[] = {1, 3, 4, 6, 5}, X = 2Output: 3Ex
7 min read
Minimum number of swaps required to make a number divisible by 60 Given an integer N , the task is to find the minimum number of swaps required to make N divisible by 60. If not possible, then print "-1".Examples: Input: N = 603 Output: 2 Explanation: Two swap operations are required: In the first swap (0, 3): 630 In the second swap (6, 3): 360 Now 360 is divisibl
10 min read
Minimum adjacent swaps required to Sort Binary array Given a binary array, the task is to find the minimum number of swaps needed to sort this binary array. We are allowed to swap only adjacent elements.Examples: Input : arr[] = [0, 0, 1, 0, 1, 0, 1, 1]Output : 3Explanation:1st swap : [0, 0, 1, 0, 0, 1, 1, 1]2nd swap : [0, 0, 0, 1, 0, 1, 1, 1]3rd swap
5 min read
Minimum number of prefix reversals to sort permutation of first N numbers Given N numbers that have a permutation of first N numbers. In a single operation, any prefix can be reversed. The task is to find the minimum number of such operations such that the numbers in the array are in increasing order. Examples: Input : a[] = {3, 1, 2} Output : 2 Step1: Reverse the complet
10 min read