给定一个由N个不同元素组成的数组,请找到对数组进行排序所需的最小交换次数。
注意:问题不是要求按最小交换次数对数组进行排序。问题是找到可以对数组进行排序的最小交换。
例子:
Input: arr[] = {4, 3, 2, 1}
Output: 2
Explanation: Swap index 0 with 3 and 1 with
2 to get the sorted array {1, 2, 3, 4}.
Input: arr[] = { 3, 5, 2, 4, 6, 8}
Output: 3
Explanation:
Swap 4 and 5 so array = 3, 4, 2, 5, 6, 8
Swap 2 and 3 so array = 2, 4, 3, 5, 6, 8
Swap 4 and 3 so array = 2, 3, 4, 5, 6, 8
So the array is sorted.
在上一篇使用图形的文章中已经讨论了此问题。在本文中,讨论了解决此问题的另一种方法,该方法与循环方法略有不同。
方法:
这个想法是用C++创建一个成对的向量,其中第一个元素为数组值,第二个元素为数组索引。下一步是根据对中的第一个元素对向量进行排序。之后,遍历向量并检查与该值映射的索引是否正确,如果不正确,则继续进行交换,直到正确放置元素并继续计算交换次数。
算法:
- 创建一个成对的向量并遍历数组,并为数组中的每个元素在向量中插入一个元素-索引对
- 从头到尾遍历矢量(循环计数器为i)。
- 对于第二个元素(索引)不等于i的对中的每个元素。将向量的第i个元素与向量的第二个element(index)交换
- 如果第二个元素(索引)等于i,则跳过循环的迭代。
- 如果在交换之后第二个元素(索引)不等于i,则将i减1。
- 递增计数器。
执行:
C++
// C++ program to find the minimum number
// of swaps required to sort an array
// of distinct element
#include
using namespace std;
// Function to find minimum swaps to
// sort an array
int findMinSwap(int arr[] , int n)
{
// Declare a vector of pair
vector> vec(n);
for(int i=0;i
Java
// Java program to find the minimum number
// of swaps required to sort an array
// of distinct element
import java.util.*;
class GFG
{
static class Point implements Comparable
{
public int x, y;
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
public int compareTo(Point other)
{
return this.x - other.x;
}
}
// Function to find minimum swaps to
// sort an array
static int findMinSwap(int[] arr, int n)
{
// Declare a vector of pair
List vec = new ArrayList();
for(int i = 0; i < n; i++)
{
vec.add(new Point(arr[i], i));
}
// Sort the vector w.r.t the first
// element of pair
Collections.sort(vec);
int ans = 0;
for(int i = 0; i < n; i++)
{
// If the element is already placed
// correct, then continue
if (vec.get(i).y == i)
continue;
else
{
// Swap with its respective index
Point temp = vec.get(vec.get(i).y);
vec.set(vec.get(i).y,vec.get(i));
vec.set(i, temp);
}
// Swap until the correct
// index matches
if (i != vec.get(i).y)
--i;
// Each swap makes one element
// move to its correct index,
// so increment answer
ans++;
}
return ans;
}
// Driver Code
public static void main(String []args)
{
int[] arr = { 1, 5, 4, 3, 2 };
int n = arr.length;
System.out.println(findMinSwap(arr,n));
}
}
// This code is contributed by Pratham76
Python3
# Python3 program to find the minimum number
# of swaps required to sort an array
# of distinct element
# Function to find minimum swaps to
# sort an array
def findMinSwap(arr, n):
# Declare a vector of pair
vec = []
for i in range(n):
vec.append([arr[i], i])
# Sort the vector w.r.t the first
# element of pair
vec = sorted(vec)
ans, c, j = -1, 0, 0
for i in range(n):
# If the element is already placed
# correct, then continue
if(vec[i][1] == i):
continue
else:
# swap with its respective index
vec[i][0], vec[vec[i][1]][1] = \
vec[vec[i][1]][1], vec[i][0]
vec[i][1], vec[vec[i][1]][1] = \
vec[vec[i][1]][1], vec[i][1]
# swap until the correct
# index matches
if(i != vec[i][1]):
i -= 1
# each swap makes one element
# move to its correct index,
# so increment answer
ans += 1
return ans
# Driver code
arr = [1, 5, 4, 3, 2]
n = len(arr)
print(findMinSwap(arr,n))
# This code is contributed by mohit kumar 29
C#
// C# program to find the minimum number
// of swaps required to sort an array
// of distinct element
using System;
using System.Collections.Generic;
class GFG{
// Function to find minimum swaps to
// sort an array
static int findMinSwap(int[] arr, int n)
{
// Declare a vector of pair
List> vec = new List>();
for(int i = 0; i < n; i++)
{
vec.Add(new Tuple(arr[i], i));
}
// Sort the vector w.r.t the first
// element of pair
vec.Sort();
int ans = 0;
for(int i = 0; i < n; i++)
{
// If the element is already placed
// correct, then continue
if (vec[i].Item2 == i)
continue;
else
{
// Swap with its respective index
Tuple temp = vec[vec[i].Item2];
vec[vec[i].Item2] = vec[i];
vec[i] = temp;
}
// Swap until the correct
// index matches
if (i != vec[i].Item2)
--i;
// Each swap makes one element
// move to its correct index,
// so increment answer
ans++;
}
return ans;
}
// Driver Code
static void Main()
{
int[] arr = { 1, 5, 4, 3, 2 };
int n = arr.Length;
Console.Write(findMinSwap(arr,n));
}
}
// This code is contributed by divyeshrabadiya07
输出:
2
复杂度分析:
- 时间复杂度: O(n Log n)。
排序数组所需的时间为n log n。 - 辅助空间: O(n)。
将创建一个额外的数组或向量。因此,空间复杂度为O(n)
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。