-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGFG Graph-6 MinSwapsSort.cpp
65 lines (55 loc) · 1.3 KB
/
GFG Graph-6 MinSwapsSort.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdio.h>
#include <stdlib.h>
#include <bits/stdc++.h>
#include <vector>
#include <queue>
using namespace std;
int minswaps(vector <int> v)
{
int n=v.size();
pair<int,int> a[n]; //Have pair's array and not vector of pairs as sort function works on pair's array and not vector of pairs
for (int i=0;i<n;i++)
{
a[i].first = v[i];
a[i].second = i;
}
sort(a,a+n);
/* pair <int,int> it;
for (int i=0;i<n;i++)
{
// it=a[i];
// cout << it.first << " " << it.second << endl; //a.push_back(make_pair(v[i],i));
} */
int ans=0,csize,j;
vector <bool> vis;
vis.assign(n,false);
for (int i=0;i<n;i++)
{
if (vis[i]==true || a[i].second == i) //i.e is not visited and not in already sorted place
continue;
csize=0; j=i;
while ( vis[j]!=true)
{
vis[j]=true;
j=a[j].second;
csize++;
}
if (csize>0)
ans+=csize-1;
}
return ans;
}
int main()
{
int n,t;
vector <int> v;
cin >> n;
for (int i=0;i<n;i++)
{
cin >> t;
v.push_back(t);
}
int ans = minswaps(v);
cout << "Minimum swaps required are: " << ans << endl;
return 0;
}