📌  相关文章
📜  按照给定顺序排列数组的元素

📅  最后修改于: 2021-06-26 20:49:06             🧑  作者: Mango

排列是将序列成员重新排列为新序列。例如,有[a,b,c,d]的24个排列。其中一些是[b,a,d,c][d,a,b,c][a,d,b,c]
排列可以由数组P []指定,其中P [i]表示元素在排列中索引i处的位置。

例如,数组[ 3,2,1,0 ]表示将索引0的元素映射到索引3,将索引1的元素映射到索引2,将索引2的元素映射到索引1和将索引的元素映射到的排列。 3到索引0。

给定N个元素的数组arr []和置换数组P [] ,任务是基于置换数组P []置换给定的数组arr []

例子:

方法:每个置换可以由一组独立的置换来表示,每个置换都是循环的,即它通过固定的偏移量环绕所有元素。要找到并应用指示输入i的循环,只需继续前进(从iP [i] ),直到我们回到i为止。完成当前循环后,找到另一个尚未应用的循环。要对此进行检查,请在应用P [i]后减去n 。这意味着,如果P [i]中的条目为负,则我们执行了相应的移动。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to permute the the given
// array based on the given conditions
int permute(int A[], int P[], int n)
{
    // For each element of P
    for (int i = 0; i < n; i++) {
        int next = i;
  
        // Check if it is already
        // considered in cycle
        while (P[next] >= 0) {
  
            // Swap the current element according
            // to the permutation in P
            swap(A[i], A[P[next]]);
            int temp = P[next];
  
            // Subtract n from an entry in P
            // to make it negative which indicates
            // the corresponding move
            // has been performed
            P[next] -= n;
            next = temp;
        }
    }
}
  
// Driver code
int main()
{
    int A[] = { 5, 6, 7, 8 };
    int P[] = { 3, 2, 1, 0 };
    int n = sizeof(A) / sizeof(int);
  
    permute(A, P, n);
  
    // Print the new array after
    // applying the permutation
    for (int i = 0; i < n; i++)
        cout << A[i] << " ";
  
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
  
// Function to permute the the given
// array based on the given conditions
static void permute(int A[], int P[], int n)
{
    // For each element of P
    for (int i = 0; i < n; i++)
    {
        int next = i;
  
        // Check if it is already
        // considered in cycle
        while (P[next] >= 0)
        {
  
            // Swap the current element according
            // to the permutation in P
            swap(A, i, P[next]);
            int temp = P[next];
  
            // Subtract n from an entry in P
            // to make it negative which indicates
            // the corresponding move
            // has been performed
            P[next] -= n;
            next = temp;
        }
    }
}
  
static int[] swap(int []arr, int i, int j)
{
    int temp = arr[i];
    arr[i] = arr[j];
    arr[j] = temp;
    return arr;
} 
  
// Driver code
public static void main(String[] args)
{
    int A[] = { 5, 6, 7, 8 };
    int P[] = { 3, 2, 1, 0 };
    int n = A.length;
  
    permute(A, P, n);
  
    // Print the new array after
    // applying the permutation
    for (int i = 0; i < n; i++)
        System.out.print(A[i]+ " ");
  
}
}
  
// This code is contributed by 29AjayKumar


Python 3
# Python 3 implementation of the approach
  
# Function to permute the the given
# array based on the given conditions
def permute(A, P, n):
      
    # For each element of P
    for i in range(n):
        next = i
  
        # Check if it is already
        # considered in cycle
        while (P[next] >= 0):
              
            # Swap the current element according
            # to the permutation in P
            t = A[i]
            A[i] = A[P[next]]
            A[P[next]] = t
              
            temp = P[next]
  
            # Subtract n from an entry in P
            # to make it negative which indicates
            # the corresponding move
            # has been performed
            P[next] -= n
            next = temp
  
# Driver code
if __name__ == '__main__':
    A = [5, 6, 7, 8]
    P = [3, 2, 1, 0]
    n = len(A)
  
    permute(A, P, n)
  
    # Print the new array after
    # applying the permutation
    for i in range(n):
        print(A[i], end = " ")
          
# This code is contributed by Surendra_Gangwar


C#
// C# implementation of the approach 
using System;
  
class GFG 
{ 
      
    // Function to permute the the given 
    // array based on the given conditions 
    static void permute(int []A, int []P, int n) 
    { 
        // For each element of P 
        for (int i = 0; i < n; i++) 
        { 
            int next = i; 
      
            // Check if it is already 
            // considered in cycle 
            while (P[next] >= 0) 
            { 
      
                // Swap the current element according 
                // to the permutation in P 
                swap(A, i, P[next]); 
                int temp = P[next]; 
      
                // Subtract n from an entry in P 
                // to make it negative which indicates 
                // the corresponding move 
                // has been performed 
                P[next] -= n; 
                next = temp; 
            } 
        } 
    } 
      
    static int[] swap(int []arr, int i, int j) 
    { 
        int temp = arr[i]; 
        arr[i] = arr[j]; 
        arr[j] = temp; 
        return arr; 
    } 
      
    // Driver code 
    public static void Main() 
    { 
        int []A = { 5, 6, 7, 8 }; 
        int []P = { 3, 2, 1, 0 }; 
        int n = A.Length; 
      
        permute(A, P, n); 
      
        // Print the new array after 
        // applying the permutation 
        for (int i = 0; i < n; i++) 
            Console.Write(A[i]+ " "); 
      
    } 
} 
  
// This code is contributed by AnkitRai01


输出:
8 7 6 5

时间复杂度: O(n)

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。