Skip to content

Commit 0041c93

Browse files
committed
add algoritm permutation in cpp
1 parent 20e9877 commit 0041c93

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

BackTracking/Permutation/permuta.cpp

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <stdio.h>
2+
3+
void troca(int vetor[], int i, int j)
4+
{
5+
int aux = vetor[i];
6+
vetor[i] = vetor[j];
7+
vetor[j] = aux;
8+
}
9+
10+
void permuta(int vetor[], int inf, int sup)
11+
{
12+
if(inf == sup)
13+
{
14+
for(int i = 0; i <= sup; i++)
15+
printf("%d ", vetor[i]);
16+
printf("\n");
17+
}
18+
else
19+
{
20+
for(int i = inf; i <= sup; i++)
21+
{
22+
troca(vetor, inf, i);
23+
permuta(vetor, inf + 1, sup);
24+
troca(vetor, inf, i);
25+
}
26+
}
27+
}
28+
29+
int main(int argc, char *argv[])
30+
{
31+
int v[] = {1, 2, 3, 4};
32+
int tam_v = sizeof(v) / sizeof(int);
33+
34+
permuta(v, 0, tam_v - 1);
35+
36+
return 0;
37+
}

0 commit comments

Comments
 (0)