minimum swaps to sort an array
C++
# 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++ program to find minimum number of swaps
// required to sort an array
#include<bits/stdc++.h>
using namespace std;
// Function returns the minimum number of swaps
// required to sort the array
int minSwaps(int arr[], int n)
{
// Create an array of pairs where first
// element is array element and second element
// is position of first element
pair<int, int> arrPos[n];
for (int i = 0; i < n; i++)
{
arrPos[i].first = arr[i];
arrPos[i].second = i;
}
// Sort the array by array element values to
// get right position of every element as second
// element of pair.
sort(arrPos, arrPos + n);
// To keep track of visited elements. Initialize
// all elements as not visited or false.
vector<bool> vis(n, false);
// Initialize result
int ans = 0;
// Traverse array elements
for (int i = 0; i < n; i++)
{
// already swapped and corrected or
// already present at correct pos
if (vis[i] || arrPos[i].second == i)
continue;
// find out the number of node in
// this cycle and add in ans
int cycle_size = 0;
int j = i;
while (!vis[j])
{
vis[j] = 1;
// move to next node
j = arrPos[j].second;
cycle_size++;
}
// Update answer by adding current cycle.
if (cycle_size > 0)
{
ans += (cycle_size - 1);
}
}
// Return result
return ans;
}
// Driver program to test the above function
int main()
{
int arr[] = {1, 5, 4, 3, 2};
int n = (sizeof(arr) / sizeof(int));
cout << minSwaps(arr, n);
return 0;
}
Also in C++:
- Title
- c++ how to get a random number
- Category
- C++
- Title
- compile c++ linux
- Category
- C++
- Title
- private and public in namespace cpp
- Category
- C++
- Title
- sort function in cpp
- Category
- C++
- Title
- least number of coins to form a sum
- Category
- C++
- Title
- object slicing in c++
- Category
- C++
- Title
- min heap declaration in c++ stl
- Category
- C++
- Title
- popualte an array c++
- Category
- C++
- Title
- c++ create array
- Category
- C++
- Title
- restting a queue stl
- Category
- C++
- Title
- pairs in c++
- Category
- C++
- Title
- retu7rn this c++
- Category
- C++
- Title
- initialize 3d vector c++
- Category
- C++
- Title
- c++ allocate dynamic with initial values
- Category
- C++
- Title
- c++ client service ros
- Category
- C++
- Title
- what are the different ways to traverse a binary tree
- Category
- C++
- Title
- iterar un map c++
- Category
- C++
- Title
- accumulate in cpp
- Category
- C++
- Title
- what is time complexity of insertion sort
- Category
- C++
- Title
- SFML window
- Category
- C++
- Title
- c++ while loop code
- Category
- C++
- Title
- how to sort in descending order c++
- Category
- C++
- Title
- how to sort an array c++
- Category
- C++
- Title
- create new file c++
- Category
- C++
- Title
- c++ yes no question
- Category
- C++
- Title
- copy a part of a vector in another in c++
- Category
- C++
- Title
- msdn parse command line
- Category
- C++
- Title
- c++ read matttrix from text file
- Category
- C++
- Title
- two sum problem in c++
- Category
- C++
- Title
- gta san andreas
- Category
- C++
- Title
- building native binary with il2cpp unity
- Category
- C++
- Title
- iostream library in cpp
- Category
- C++
- Title
- getch c++ library
- Category
- C++
- Title
- C++ w3schools
- Category
- C++
- Title
- pow c++
- Category
- C++
- Title
- quick sort predefined function in c++
- Category
- C++
- Title
- screen record ios simulator
- Category
- C++
- Title
- lopping over an array c++
- Category
- C++
- Title
- list conda environments
- Category
- C++
- Title
- max in c++
- Category
- C++
- Title
- regexp_like oracle c++
- Category
- C++
- Title
- how to compile opencv c++ in ubuntu
- Category
- C++
- Title
- insertion sort in c++ program
- Category
- C++
- Title
- how to append two vectors in c++
- Category
- C++
- Title
- % operator in c++
- Category
- C++
- Title
- how can make string value in cpp
- Category
- C++
- Title
- initialize vector of vector c++
- Category
- C++
- Title
- C++ string format ctime
- Category
- C++
- Title
- how to create a vector in c++
- Category
- C++
- Title
- initialise 2d vector in c++
- Category
- C++
- Title
- c++ do you not inherit constructor
- Category
- C++
- Title
- syntax c++
- Category
- C++
- Title
- switch statement c++
- Category
- C++
- Title
- c++ assert
- Category
- C++
- Title
- matrix class in c++
- Category
- C++
- Title
- how do for loops on c++
- Category
- C++
- Title
- how to get a letter from the user c++ string
- Category
- C++
- Title
- modular exponentiation c++
- Category
- C++
- Title
- c++ iterate through constant list
- Category
- C++
- Title
- how to reverse a vector
- Category
- C++
- Title
- find upper bound c++ vector
- Category
- C++
- Title
- iterate through unordered_map c++ in reverse order
- Category
- C++
- Title
- declaration vs. definition cpp
- Category
- C++
- Title
- c++ char to string
- Category
- C++
- Title
- equal elements in two arrays in c++
- Category
- C++
- Title
- templates of templates c++
- Category
- C++
- Title
- find in vector in c++
- Category
- C++
- Title
- c++ argv
- Category
- C++
- Title
- dfs in c++
- Category
- C++
- Title
- findung the mode in c++
- Category
- C++
- Title
- hashmap in c++
- Category
- C++
- Title
- max element in array c++ stl
- Category
- C++
- Title
- type id c++
- Category
- C++
- Title
- what is a header in c++
- Category
- C++
- Title
- glfw initialize in c++
- Category
- C++
- Title
- c++ program to input and print text using Dynamic Memory Allocation.loop
- Category
- C++
- Title
- bitset c++
- Category
- C++
- Title
- system("pause") note working c++
- Category
- C++
- Title
- flushing output in c++
- Category
- C++
- Title
- cloud hosting
- Category
- C++
- Title
- va_arg
- Category
- C++
- Title
- programa para saber si un numero es primo
- Category
- C++
- Title
- c++ set console title
- Category
- C++
- Title
- stringstream in c++ with delimiter
- Category
- C++
- Title
- c++ how to add something at the start of a vector
- Category
- C++
- Title
- c++ program for addition of two numbers using functions
- Category
- C++
- Title
- reverse a linked list using recursion
- Category
- C++
- Title
- range based for loop c++
- Category
- C++
- Title
- timer in c++
- Category
- C++
- Title
- assegnare valori in c++
- Category
- C++
- Title
- how to put a class in a .h file c++
- Category
- C++
- Title
- leveling system c++
- Category
- C++
- Title
- sort a string alphabetically c++
- Category
- C++
- Title
- basic data types in c++ hackerrank solution
- Category
- C++
- Title
- how to use a new node c++
- Category
- C++
- Title
- coping 2d vector in cpp
- Category
- C++
- Title
- how to check sqrt of number is integer c++
- Category
- C++
- Title
- c++ give options
- Category
- C++
- Title
- convert stirng to int c++
- Category
- C++
- Title
- capitalize first letter c++
- Category
- C++