Alexander Bogomolny 的 C++ 中无序排列算法
在此,我们给出一个数字 N。我们的任务是使用Alexander Bogomolny 的无序排列算法查找 N 的无序排列。
让我们先讨论排列,
排列是集合中项目可以唯一排列的方式的数量称为排列。
示例 − {4,9,2} 的排列将是 {4,9,2}、{4,2,9}、{9,4,2}、{9,2,4}、{2,4,9} 和 {2,9,4}。
排列已用于定义计算机网络中的交换网络、并行处理,还用于各种加密算法中。
Alexander Bogomolny 的无序排列算法
此算法计算前 N 个自然数的所有可能排列。给定数字 N,排列将从 1 到 N 计算。
我们举一个例子来理解这个问题,
输入
N = 3
输出
1,2,3 ; 1,3,2 ; 2,1,3 ; 2,3,1 ; 3,1,2 ; 3,2,1
算法
1. Build a function with an array, number N, and an integer k as parameters 2. Initialize the level, as level increases permute the rest of the values 3. When the recursion condition is reached all its values are printed.
示例
显示我们算法实现的程序 −
#include <iostream>
using namespace std;
int level = -1;
void AlexanderBogomolyn(int permutations[], int N, int k) {
level = level + 1;
permutations[k] = level;
if (level == N) {
for (int i = 0; i < N; i++)
cout<<permutations[i]<<"\t";
cout<<endl;
}
else{
for (int i = 0; i < N; i++)
if (permutations[i] == 0)
AlexanderBogomolyn(permutations, N, i);
}
level = level - 1;
permutations[k] = 0;
}
int main(){
int N = 4;
int permutations[N] = { 0 };
cout<<"All permutations are :\n";
AlexanderBogomolyn(permutations, N, 0);
return 0;
}输出
All permutations are : 1 2 3 4 1 2 4 3 1 3 2 4 1 4 2 3 1 3 4 2 1 4 3 2 2 1 3 4 2 1 4 3 3 1 2 4 4 1 2 3 3 1 4 2 4 1 3 2 2 3 1 4 2 4 1 3 3 2 1 4 4 2 1 3 3 4 1 2 4 3 1 2 2 3 4 1 2 4 3 1 3 2 4 1 4 2 3 1 3 4 2 1 4 3 2 1
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP