This C program implements Alexander Bogomolyn’s unordered permutation algorithm to find all permutations of numbers from 1 to n.
Here is the source code of the C program to display all possible permutations of numbers from 1 to n using Alexander Bogomolyn’s unordered permutation algorithm. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C program to implement Alexander Bogomolyn's unordered permutation algorithm
* to find all permutations of numbers from 1 to n
*/
#include <stdio.h>
/* Function to print all the permutations */
void print(const int *v, const int size)
{
int i;
if (v != 0) {
for (i = 0; i < size; i++) {
printf("%4d", v[i] );
}
printf("\n");
}
}
/* End of print() */
/* Function to generate permutations */
void visit(int *Value, int N, int k)
{
static level = -1;
int i;
level = level+1; Value[k] = level;
if (level == N)
print(Value, N);
else
for (i = 0; i < N; i++)
if (Value[i] == 0)
visit(Value, N, i);
level = level-1; Value[k] = 0;
}
/* End of visit() */
/* The main() begins */
int main()
{
int Value[20], i, N;
printf("Enter the value of N: ");
scanf("%d", &N);
for (i = 0; i < N; i++) {
Value[i] = 0;
}
visit(Value, N, 0);
}
$ gcc permute_alex.c $ a.out Enter the value of N: 3 1 2 3 1 3 2 2 1 3 3 1 2 2 3 1 3 2 1
Sanfoundry Global Education & Learning Series – 1000 C Algorithms.
advertisement
If you wish to look at all C Algorithms and Solutions, go to C Algorithms.
Related Posts:
- Apply for C Internship
- Practice Computer Science MCQs
- Watch Advanced C Programming Videos
- Check Computer Science Books
- Practice BCA MCQs