Question:
how to use recursive algorithm to determine all of the permutations?
samurai
2006-05-06 23:53:00 UTC
suppose there are n elements,how can you use recursive algorithm to
determine all of the n elements' arrangements.
for example,if there is 1 element,say, A1,there is only one
permutation, namely,{A1};
if there are 2 elements,say,A1 and A2,there are two
arrangements,namely, {A1,A2},{A2,A1};
3 elements, A1,A2 and A3, six permutations
exist,{A1,A2,A3},{A2,A1,A3},{A3,A1,A2},{A3,A2,A1},{A1,A3,A2},{A2,A3,A1};
.......
so if there are n elements, how can we use recursive algorithm to determine all of these permutations?
of course We know that there are n! permutations of n elements,but i
just want to make a program to print what does every permutation look
like,like
this,{A1,A2,A3},{A2,A1,A3},{A3,A1,A2},{A3,A2,A1},{A1,A3,A2},{A2,A3,A1}.
can anyone give me some tips?
Four answers:
hespy
2006-05-07 01:41:14 UTC
The fasting is Quicksort, but also the most complicated. If you are looking at sorting hundreds of numbers or more, Quicksort with its recursive elements are the way to go.
brainteaser
2006-05-07 00:00:58 UTC
store elements in an array... then use nested for loops to print the permutation... look up on how a buble sort works... you can use something similar here
2016-05-20 15:33:03 UTC
here the algo is: if n is the number of distinct element in a string.Then permutation of string length n = put nth number in each n places and do the permutation of string length n-1 on the rest n-1 numbers. Here i give u a c++ code to generate permutations.. I have done it in Dev C++ compiler. Please change the header files as needed .. e.g for turbo CPP compiler put the iostream.h,string.h headers and replace the last system("pause") command by getch(); #include using namespace std; char temp[20]; void permut(char *s,int start) { if(start==strlen(s)) { temp[start]='\0'; //check whether temp array contains duplicate values; for(int i=0;i
KalyanC
2006-05-06 23:58:45 UTC
boy!! this sure looks like some homework.



but is interesting. will follow-up for sure ;)



Cheers


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...