# Number Of Swaps To Sort An Array L Code Example

Snippet 1
``` ``` // C++ program to find  minimum number of swaps
// required to sort an array
#include

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 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 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++;
}

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;
}
``` ```
Snippet 2
``` ``` # Python3 program to find the minimum
# number of swaps required to sort
# the given array

# Function to find minimum swaps
def minimumSwaps(arr):

# Initialise count variable
count = 0;
i = 0;
while (i < len(arr)):

# If current element is
# not at the right position
if (arr[i] != i + 1):

while (arr[i] != i + 1):
temp = 0;

# Swap current element
# with correct position
# of that element
temp = arr[arr[i] - 1];
arr[arr[i] - 1] = arr[i];
arr[i] = temp;
count += 1;

# Increment for next index
# when current element is at
# correct position
i += 1;

return count;

# Driver code
if __name__ == '__main__':
arr = [ 2, 3, 4, 1, 5 ];

# Function to find minimum swaps
print(minimumSwaps(arr));

# This code is contributed by 29AjayKumar
``` ```

## Similar Snippets

Find Duplicate And Repeating Number In Array Code Example - java

Create Copy Of Array From Another Array Code Example - java

Array Fot String Code Example - java

Java Get Random Index From Array Code Example - java

How To Round A Number Javascript Code Example - java

Simpledateformat Example Java Code Example - java

Js How To Prevent A Code From Runing Code Example - java

While Loop Continue Js Code Example - java

Find Duplicates In Arraylist Java Code Example - java

Firestore Find Doc And Set Data Code Example - java

Different Types Of Writing If Else Statements Jav Code Example - java

Remove Last Letter From String Java Code Example - java

For Each Javascript Code Example - java

Java Creat A Folder Code Example - java

How To Take A Image As A Background In Tkinter In Python Code Example - java

Java How To Get Files In Resources Code Example - java

Creating Java Main Method Code Example - java

Switch Case Accepts Byte In Java Code Example - java