Minimum number of swaps required to sort an array of first N number
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;
int minimumSwaps( int arr[], int n)
{
int count = 0;
int i = 0;
while (i < n)
{
if (arr[i] != i + 1)
{
while (arr[i] != i + 1)
{
int temp = 0;
temp = arr[arr[i] - 1];
arr[arr[i] - 1] = arr[i];
arr[i] = temp;
count++;
}
}
i++;
}
return count;
}
int main()
{
int arr[] = { 2, 3, 4, 1, 5 };
int n = sizeof (arr)/ sizeof (arr[0]);
cout << minimumSwaps(arr,n) ;
}
|
Java
import java.io.*;
import java.util.*;
class GfG {
static int minimumSwaps( int [] arr)
{
int count = 0 ;
int i = 0 ;
while (i < arr.length) {
if (arr[i] != i + 1 ) {
while (arr[i] != i + 1 ) {
int temp = 0 ;
temp = arr[arr[i] - 1 ];
arr[arr[i] - 1 ] = arr[i];
arr[i] = temp;
count++;
}
}
i++;
}
return count;
}
public static void main(String[] args)
{
int arr[] = { 2 , 3 , 4 , 1 , 5 };
System.out.println(minimumSwaps(arr));
}
}
|
Python3
def minimumSwaps(arr):
count = 0 ;
i = 0 ;
while (i < len (arr)):
if (arr[i] ! = i + 1 ):
while (arr[i] ! = i + 1 ):
temp = 0 ;
temp = arr[arr[i] - 1 ];
arr[arr[i] - 1 ] = arr[i];
arr[i] = temp;
count + = 1 ;
i + = 1 ;
return count;
if __name__ = = '__main__' :
arr = [ 2 , 3 , 4 , 1 , 5 ];
print (minimumSwaps(arr));
|
C#
using System;
class GfG
{
static int minimumSwaps( int [] arr)
{
int count = 0;
int i = 0;
while (i < arr.Length)
{
if (arr[i] != i + 1)
{
while (arr[i] != i + 1)
{
int temp = 0;
temp = arr[arr[i] - 1];
arr[arr[i] - 1] = arr[i];
arr[i] = temp;
count++;
}
}
i++;
}
return count;
}
public static void Main(String[] args)
{
int []arr = { 2, 3, 4, 1, 5 };
Console.WriteLine(minimumSwaps(arr));
}
}
|
Javascript
<script>
function minimumSwaps(arr)
{
let count = 0;
let i = 0;
while (i < arr.length)
{
if (arr[i] != i + 1)
{
while (arr[i] != i + 1)
{
let temp = 0;
temp = arr[arr[i] - 1];
arr[arr[i] - 1] = arr[i];
arr[i] = temp;
count++;
}
}
i++;
}
return count;
}
let arr = [2, 3, 4, 1, 5 ];
document.write(minimumSwaps(arr));
</script>
|
Time Complexity: O(N) where N is the size of array.
Auxiliary Space: O(1)
Last Updated :
16 Aug, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...