-
Notifications
You must be signed in to change notification settings - Fork 118
/
31.c
66 lines (55 loc) · 1.53 KB
/
31.c
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
#include <stdio.h>
void swap(int *a, int *b) {
int c = *a;
*a = *b;
*b = c;
}
void nextPermutation(int* nums, int numsSize) {
if (nums == NULL || numsSize == 0) return;
int i, j;
int left, right;
/* from right to left, find first i for nums[i] < nums[i+1] */
for (i = numsSize - 2; i >= 0; i--) {
if (nums[i] < nums[i + 1]) break;
}
/* can't find such i, reverse whole array and return */
if (i == -1) {
left = 0;
right = numsSize - 1;
for (i = left; i <= left + (right - left) / 2; i++) {
swap(&nums[i], &nums[right - i + left]);
}
return;
}
/* from right to left, find first j for nums[j] > nums[i] */
for (j = numsSize - 1; j >= 0; j--) {
if (nums[j] > nums[i]) break;
}
/* swap nums[i] and nums[j] */
swap(&nums[i], &nums[j]);
/* reverse nums[i+1] to nums[n-1] */
left = i + 1;
right = numsSize - 1;
for (i = left; i <= left + (right - left) / 2; i++) {
swap(&nums[i], &nums[right - i + left]);
}
}
int main() {
int nums[] = { 1, 2, 3, 4 };
int size = sizeof(nums) / sizeof(nums[0]);
int i = size;
int count = 1;
while (i) {
count *= i;
i--;
}
count++; /* test lowest possible order case */
while (count--){
for (i = 0; i < size; i++){
printf("%d ", nums[i]);
}
printf("\n");
nextPermutation(nums, size);
}
return 0;
}