📌  相关文章
📜  使用三重循环右交换对 N 个自然数进行排序排列

📅  最后修改于: 2021-09-04 08:31:29             🧑  作者: Mango

给定一个大小为N的数组 arr[] ,其中包含 N 个自然数的排列,任务是在三重循环右交换的帮助下对 N 个自然数的排列进行排序。

三重循环右移:指三重循环右移,其中 –

arr[i] -> arr[j] -> arr[k] -> arr[i]

例子:

方法:思想是遍历数组,找到数组中不在实际排序位置的元素,可以通过if检查arr[i] != i .因为数组中只有 N 个自然元素。最后,找到数组中所需的奇数长度循环旋转以获得数组的排序形式。如果需要任何偶数长度的循环旋转,则无法对数组的元素进行排序。

下面是上述方法的实现:

C++
// C++ implementation to find the
// number of operations required to
// sort the elements of the array
  
#include 
using namespace std;
#define ll long long
  
// Function to sort the permutation
// with the given operations
void sortPermutation(ll arr[], ll n)
{
    vector > >
        ans;
    vector p;
  
    // Vistied array to check the
    // array element is at correct
    // position or not
    bool visited[200005] = { 0 };
  
    // Loop to iterate over the elements
    // of the given array
    for (ll i = 1; i <= n; i++) {
  
        // Condition to check if the
        // elements is at its correct
        // position
        if (arr[i] == i) {
            visited[i] = 1;
            continue;
        }
        else {
  
            // Condition to check if the
            // element is included in any
            // previous cyclic rotations
            if (!visited[i]) {
                ll x = i;
                vector v;
  
                // Loop to find the cyclic
                // rotations in required
                while (!visited[x]) {
                    visited[x] = 1;
                    v.push_back(x);
                    x = arr[x];
                }
  
                // Condition to check if the
                // cyclic rotation is a
                // valid rotation
                if ((v.size() - 3) % 2 == 0) {
                    for (ll i = 1; i < v.size();
                         i += 2) {
  
                        ans
                            .push_back(
                                make_pair(
                                    v[0],
                                    make_pair(
                                        v[i], v[i + 1])));
                    }
                    continue;
                }
                p.push_back(v[0]);
                p.push_back(v[v.size() - 1]);
  
                // Loop to find the index of the
                // cyclic rotation
                // for the current index
                for (ll i = 1; i < v.size() - 1;
                     i += 2) {
                    ans
                        .push_back(
                            make_pair(
                                v[0],
                                make_pair(
                                    v[i], v[i + 1])));
                }
            }
        }
    }
  
    // Condition to if the cyclic
    // rotation is a valid rotation
    if (p.size() % 4) {
        cout << -1 << "\n";
        return;
    }
  
    // Loop to find all the valid operations
    // required to sort the permutation
    for (ll i = 0; i < p.size(); i += 4) {
        ans.push_back(
            make_pair(p[i],
                      make_pair(p[i + 1], p[i + 2])));
        ans.push_back(
            make_pair(p[i + 2],
                      make_pair(p[i], p[i + 3])));
    }
  
    // Total operation required
    cout << ans.size() << "\n";
    for (ll i = 0; i < ans.size(); i++) {
        cout << ans[i].first << " "
             << ans[i].second.first << " "
             << ans[i].second.second << "\n";
    }
}
  
// Driver Code
int main()
{
    ll arr[] = { 0, 3, 2, 4, 1 };
    ll n = 4;
  
    // Function Call
    sortPermutation(arr, n);
    return 0;
}


Python3
# Python3 implementation to find the
# number of operations required to
# sort the elements of the array
  
# Function to sort the permutation
# with the given operations
def sortPermutation(arr, n):
  
    ans = []
    p = []
  
    # Vistied array to check the
    # array element is at correct
    # position or not
    visited = [0] * 200005
  
    # Loop to iterate over the elements
    # of the given array
    for i in range(1, n + 1):
  
        # Condition to check if the
        # elements is at its correct
        # position
        if (arr[i] == i):
            visited[i] = 1
            continue
  
        else:
  
            # Condition to check if the
            # element is included in any
            # previous cyclic rotations
            if (visited[i]==False):
                x = i
                v = []
  
                # Loop to find the cyclic
                # rotations in required
                while (visited[x] == False):
                    visited[x] = 1
                    v.append(x)
                    x = arr[x]
  
                # Condition to check if the
                # cyclic rotation is a
                # valid rotation
                if ((len(v) - 3) % 2 == 0):
                    for i in range(1, len(v), 2):
                        ans.append([v[0], v[i], v[i + 1]])
                    continue
  
                p.append(v[0])
                p.append(v[len(v) - 1])
  
                # Loop to find the index of the
                # cyclic rotation
                # for the current index
                for i in range(1, len(v) - 1, 2):
                    ans.append([v[0], v[i], v[i + 1]])
  
    # Condition to if the cyclic
    # rotation is a valid rotation
    if (len(p) % 4):
        print(-1)
        return
  
    # Loop to find athe valid operations
    # required to sort the permutation
    for i in range(0, len(p), 4):
        ans.append([p[i], p[i + 1], p[i + 2]])
        ans.append(p[i [+ 2], p[i], p[i + 3]])
  
    # Total operation required
    print(len(ans))
    for i in ans:
        print(i[0], i[1], i[2])
  
# Driver Code
if __name__ == '__main__':
    arr=[0, 3, 2, 4, 1]
    n = 4
  
    # Function Call
    sortPermutation(arr, n)
  
# This code is contributed by Mohit Kumar


输出:
1
1 3 4

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live