-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmain.cpp
88 lines (83 loc) · 3.08 KB
/
main.cpp
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
#include <vector>
using namespace std;
/*
Permutation of an array using the backtracking technique which
finds all possible solutions without repeating.
*/
ostream& operator << (ostream& stream, const vector<int>& v) {
for (int i = 0; i < (int)v.size(); i++) {
stream << v[i] << " ";
}
return stream;
}
void permutation(vector<int>& v, int index, int arraySize, string space = "") {
cout << space << "permutation(v, " << index << ", " << arraySize << ")" << endl;
/*
permutation([1,2,3], 0, 3, "")
i == 0
swap(array[0], array[0]);
permutation([1,2,3], 1, 3, "") -> index + 1
i = 1
swap(array[1], array[1]);
permutation([1,2,3], 2, 3, "") -> index + 1
base case ->> print([1,2,3])
swap(array[1], array[1]); -> ->> [1,2,3] again (backtracking)
i = 2
swap(array[1], array[2]);
permutation([1,3,2], 2, 3, "") -> index + 1
base case ->> print([1,3,2])
swap(array[1], array[2]); ->> [1,2,3] again (backtracking)
i = 3
break loop;
i == 1
swap(array[0], array[1]);
permutation([2,1,3], 1, 3, "") -> index + 1
i = 1
swap(array[1], array[1]);
permutation([2,1,3], 2, 3, "") -> index + 1
base case ->> print([2,1,3])
swap(array[1], array[1]); ->> [2,1,3] again (backtracking)
i = 2
swap(array[1], array[2]);
permutation([2,3,1], 2, 3, "") -> index + 1
base case ->> print([2,3,1])
swap(array[1], array[2]); ->> [2,1,3] again (backtracking)
i = 3
break loop;
i == 2
swap(array[0], array[2]);
permutation([3,1,2], 1, 3, "") -> index + 1
i = 1
swap(array[1], array[1]);
permutation([3,1,2], 2, 3, "") -> index + 1
base case ->> print([3,1,2])
swap(array[1], array[1]); ->> [3,1,2] again (backtracking)
i = 2
swap(array[1], array[2]);
permutation([3,2,1], 2, 3, "") -> index + 1
base case ->> print([3,2,1])
swap(array[1], array[2]); ->> [3,1,2] again (backtracking)
i = 3
break loop;
i == 3
break loop;
finish function
*/
// Base case
if(index == arraySize - 1) {
cout << "->>>> " << v << endl;
} else {
for (int i = index; i < arraySize; i++) {
swap(v[index], v[i]);
permutation(v, index + 1, arraySize, space + " ");
swap(v[index], v[i]); // Backtrack phase (reordering array)
}
}
}
int main()
{
vector<int> v = {1,2,3};
permutation(v, 0, 3);
return 0;
}