-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperm.c
99 lines (85 loc) · 1.89 KB
/
perm.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
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
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define N 100
// Returns an integer from [a,b] using library function rand() and operator %
// if a > b return INT_MIN
// if b - a > RAND_MAX return INT_MAX
// if a == b return a
// else return integer from [a,b]
int rand_from_interval(int a, int b) {
if (a > b) return INT_MIN;
else if ((b - a) > RAND_MAX) return INT_MAX;
else if (a == b) return a;
else return rand() % (b - a + 1) + a;
}
void swap (int array[], int i, int k) {
int tmp = array[i];
array[i] = array[k];
array[k] = tmp;
}
// random permutation of integers from [0, n-1]
void rand_permutation(int n, int array[]) {
if (n < 0) return;
for (int i = 0; i < n; i++){
array[i] = i;
}
for (int i = 0; i < n - 1; i++){
int k = rand_from_interval(i, n - 1);
swap(array, i, k);
}
}
// bubble sort (increasing order)
// returns number of iterations of the external loop
// after which the array is sorted
// for { 0 1 2 3 7 4 5 6 } -> 1,
// for { 1 2 3 7 4 5 6 0 } -> 7,
// for { 0 1 2 3 4 5 6 7 } -> 0.
int bubble_sort(int n, int array[]) {
int cnt = -1;
int flag = 1;
while (flag){
flag = 0;
cnt++;
int i = 0;
while (i < n - 1){
if (array[i] > array[i + 1]){
swap(array, i, i + 1);
flag = 1;
}
i++;
}
}
return cnt;
}
int main(void) {
int to_do, seed;
int a, b, m, n;
int array[N];
scanf("%d %d", &to_do, &seed);
srand((unsigned) seed); // set the seed
switch(to_do) {
case 1:
scanf("%d %d %d",&a, &b, &m);
for(int i = 0; i < m; ++i) {
printf("%d ", rand_from_interval(a, b));
}
printf("\n");
break;
case 2:
scanf("%d", &n);
rand_permutation(n, array);
for(int i = 0; i < n; ++i) printf("%d ", array[i]);
printf("\n");
break;
case 3:
scanf("%d", &n);
rand_permutation(n, array);
printf("%d\n", bubble_sort(n, array));
break;
default:
printf("NOTHING TO DO!\n");
break;
}
return 0;
}