Number Of Exchanges To Sort Array Code Example

Snippet 1

  #include  
using namespace std; 
  
// Function to find minimum swaps 
int minimumSwaps(int arr[],int n) 
{ 
    // Initialise count variable 
    int count = 0; 
    int i = 0; 
      
    while (i < n)  
    { 
  
        // If current element is 
        // not at the right position 
        if (arr[i] != i + 1) 
        { 
  
            while (arr[i] != i + 1)  
            { 
                int 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++; 
            } 
        } 
  
        // Increment for next index 
        // when current element is at 
        // correct position 
        i++; 
    } 
    return count; 
} 
  
// Driver code 
int main()  
{ 
    int arr[] = { 2, 3, 4, 1, 5 }; 
  
    int n = sizeof(arr)/sizeof(arr[0]); 
      
    // Function to find minimum swaps 
    cout << minimumSwaps(arr,n) ; 
} 
  
// This code is contributed by AnkitRai01 
 

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 
 

Snippet 3

  // CPP 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

        Source: www.geeksforgeeks.org
         
     

Similar Snippets


Create Copy Of Array From Another Array Code Example - java

Creating Java Main Method Code Example - java

Find Duplicates In Arraylist Java Code Example - java

Java Creat A Folder Code Example - java

Firestore Find Doc And Set Data Code Example - java

Copyright © Code Fetcher 2020

 

 

Leave a comment

Your email address will not be published. Required fields are marked *