ITERATIVE PERMUTATION

closed account (ivDwAqkS)
I hope this code helps someone with iterative permutation (it is not simple),but with basic source-code and use only one basic library, this permutation only works for 4 elements (Absolutely for beginners who wants a challenge)It took me 2 days for doing this code, because I didn't want help of how to create my own permutation code, even though it is not perfect because of the length of the array, however I understood the point. I will leave you the code if you catch the idea you will be able to write an iterative permutation code for more elements, and also inputting the elements :D.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
using namespace std;

char* letter[4]={"A", "B", "C", "D"};

int main(){
    int i = 0, k = 1, j = 2, x = 3;
    while (i<4){
        while(k<4){
            while(j<4){
                while(x<4){
                    if(i!= x && i!= k && i!=j){
                        if(k!=i && k!=x && k!=j){
                            if(j!=i && j!=k && j!=x){
                                if(x!=i && x!=k && x!=j)
                                    cout << letter[i] << " " << letter[k] << " " << letter[j] << " " << letter[x] << endl;
                            }
                        }
                    }
                    if(x==3){
                        x=0;
                        break;
                    }
                    x++;
                }
                if(j==3){
                    j=0;
                    break;
                }
                j++;
            }
            if(k==3){
                k=0;
                break;
            }
            k++;
        }
        i++;
    }
    return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

int main() {
   for (char v = 'A'; v <= 'D'; v ++)
      for (char x = 'A'; x <= 'D'; x ++)
         for (char y = 'A'; y <= 'D'; y ++)
            for (char z = 'A'; z <= 'D'; z ++)
               if (v != x && v != y && v != z && x != y && x != z && y != z)
                  std::cout << v << " " << x << " " << y << " " << z << "\n";

   return 0;
}


Could be modified for the permutations of the array easy enough, but there is a simpler way using for loops and a single if statement. Just trying to help you broaden your horizons.
closed account (ivDwAqkS)
Volatile_Pusle Thanks :D that was new for me, and giblit I didn't want to use the algorithm library or recursively function, coz I think it is much more easy to do it at that way, don't you think? I was trying to do it iterively.
And once again, thanks Volatile_Pulse for your help, but when you are going to use array you could not use it with your code. :p.
but when you are going to use array you could not use it with your code. :p.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

int main() {
   char letters[] = {'A', 'B', 'C', 'D'};

   for (int v = 0; v < 4; v ++)
      for (int x = 0; x < 4; x ++)
         for (int y = 0; y < 4; y ++)
            for (int z = 0; z < 4; z ++)
               if (v != x && v != y && v != z && x != y && x != z && y != z)
                  std::cout << letters[v] << " " << letters[x] << " " << letters[y] << " " << letters[z] << "\n";

   return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

int main() {
   std::string letters = "ABCD";

   for (int v = 0; v < 4; v ++)
      for (int x = 0; x < 4; x ++)
         for (int y = 0; y < 4; y ++)
            for (int z = 0; z < 4; z ++)
               if (v != x && v != y && v != z && x != y && x != z && y != z)
                  std::cout << letters[v] << " " << letters[x] << " " << letters[y] << " " << letters[z] << "\n";

   return 0;
}

And with strings.
Last edited on
closed account (ivDwAqkS)
;D kool, now i need to learn everything about recursive, do you know some good books that only talk about recursive funcion ? i mean only recursive to go deeply or maybe a forum with many examples explaining them.
Last edited on
Books or websites, not really. I did do a recursion program here though, and here is the link: http://www.cplusplus.com/forum/beginner/74852/

Here is the solution: http://www.cplusplus.com/forum/beginner/74852/2/#msg402505

It was my first attempt at recursion...just google it, you'll get some results back.
closed account (ivDwAqkS)
Thanks Volatile i appreciate your help. i will start with it.
There is also a section on the tutorials. Basically recursive is just calling a function inside itself. For example a factorial function.
http://www.cplusplus.com/doc/tutorial/functions/ --Very bottom.
http://www.learncpp.com/cpp-tutorial/710-recursion/
Last edited on
Long time ago, I was bored. So I decided to do what you did, except I wanted to copy what std::next_permutation does. So I found some tutorials and I made this:

https://www.dropbox.com/s/gpi1gyv7pji1iyx/Lexi_Permute.cc

It even comes with it's own cute template sorting function (bubble sort) and it's own swap function. And for some reason, I used a lot of underscores back then to.

Not to long ago, I found a permutation algorithm that is able to find the nth permutation of a set of values; but this one only works for strings of length no more than 20 because 20! is larger than long long. But if done in java, you can use BigInteger library and it should be good to go
Topic archived. No new replies allowed.