-
Notifications
You must be signed in to change notification settings - Fork 0
/
BCPERMU.cpp
108 lines (91 loc) · 2.96 KB
/
BCPERMU.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <iostream>
using namespace std;
void generateAllPermutations(int numberOfElements);
void generateFirstAscendingPermutation(int numberOfElements, int permutationArray[]);
void printCurrentPermutationArray(int numberOfElements, int permutationArray[]);
void generateNextPermutationArray(int numberOfElements, int permutationArray[], bool *stop);
int findPivot(int numberOfElements, int permutationArray[]);
int findRightmostSuccessorOfPivotInTheSuffix(int pivot, int numberOfElements, int permutationArray[]);
void swap(int *a, int *b);
void reverseTheSuffix(int index, int numberOfElements, int permutationArray[]);
int main()
{
int numberOfElements;
cin >> numberOfElements;
generateAllPermutations(numberOfElements);
}
void generateAllPermutations(int numberOfElements)
{
int permutationArray[numberOfElements];
generateFirstAscendingPermutation(numberOfElements, permutationArray);
bool stop = false;
while (!stop)
{
printCurrentPermutationArray(numberOfElements, permutationArray);
generateNextPermutationArray(numberOfElements, permutationArray, &stop);
}
}
void generateFirstAscendingPermutation(int numberOfElements, int permutationArray[])
{
for (int i = 0; i < numberOfElements; i++)
{
permutationArray[i] = i + 1;
}
}
void printCurrentPermutationArray(int numberOfElements, int permutationArray[])
{
for (int i = 0; i < numberOfElements; i++)
{
cout << permutationArray[i];
}
cout << endl;
}
void generateNextPermutationArray(int numberOfElements, int permutationArray[], bool *stop)
{
int pivot = findPivot(numberOfElements, permutationArray);
if (pivot < 0)
{
*stop = true;
}
else
{
int rightmostSuccessorOfPivotInTheSuffix = findRightmostSuccessorOfPivotInTheSuffix(pivot, numberOfElements, permutationArray);
swap(&permutationArray[pivot], &permutationArray[rightmostSuccessorOfPivotInTheSuffix]);
reverseTheSuffix(pivot, numberOfElements, permutationArray);
}
}
int findPivot(int numberOfElements, int permutationArray[])
{
int index = numberOfElements - 2;
while ((index >= 0) && (permutationArray[index] > permutationArray[index + 1]))
{
index--;
}
return index;
}
int findRightmostSuccessorOfPivotInTheSuffix(int pivot, int numberOfElements, int permutationArray[])
{
int rightmostSuccessorOfPivotInTheSuffix = numberOfElements - 1;
while (permutationArray[rightmostSuccessorOfPivotInTheSuffix] < permutationArray[pivot])
{
rightmostSuccessorOfPivotInTheSuffix--;
}
return rightmostSuccessorOfPivotInTheSuffix;
}
void swap(int *a, int *b)
{
int temp = *b;
*b = *a;
*a = temp;
}
void reverseTheSuffix(int index, int numberOfElements, int permutationArray[])
{
int left = index + 1;
int right = numberOfElements - 1;
while (left < right)
{
swap(&permutationArray[left], &permutationArray[right]);
left++;
right--;
}
}