TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
swap_sort.cpp
1// C++ program to find minimum number of swaps required to sort an array
2#include <algorithm>
3#include <iostream>
4#include <utility>
5#include <vector>
6
7// Function returns the minimum number of swaps
8// required to sort the array
9int minSwaps(int arr[], int n) {
10 // Create an array of pairs where first
11 // element is array element and second element
12 // is position of first element
13 std::pair<int, int> *arrPos = new std::pair<int, int>[n];
14 for (int i = 0; i < n; i++) {
15 arrPos[i].first = arr[i];
16 arrPos[i].second = i;
17 }
18
19 // Sort the array by array element values to
20 // get right position of every element as second
21 // element of pair.
22 std::sort(arrPos, arrPos + n);
23
24 // To keep track of visited elements. Initialize
25 // all elements as not visited or false.
26 std::vector<bool> vis(n, false);
27
28 // Initialize result
29 int ans = 0;
30
31 // Traverse array elements
32 for (int i = 0; i < n; i++) {
33 // already swapped and corrected or
34 // already present at correct pos
35 if (vis[i] || arrPos[i].second == i)
36 continue;
37
38 // find out the number of node in
39 // this cycle and add in ans
40 int cycle_size = 0;
41 int j = i;
42 while (!vis[j]) {
43 vis[j] = 1;
44
45 // move to next node
46 j = arrPos[j].second;
47 cycle_size++;
48 }
49
50 // Update answer by adding current cycle.
51 if (cycle_size > 0) {
52 ans += (cycle_size - 1);
53 }
54 }
55
56 delete[] arrPos;
57
58 // Return result
59 return ans;
60}
61
62// program to test
63int main() {
64 int arr[] = {6, 7, 8, 1, 2, 3, 9, 12};
65 int n = (sizeof(arr) / sizeof(int));
66 std::cout << minSwaps(arr, n);
67 return 0;
68}
int main()
Main function.