-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrotate_table.c
58 lines (47 loc) · 1.12 KB
/
rotate_table.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
// zad. 1 tablice
// b[(i +k) mod n] = a[i]
// NWW(n,k) / k
// a * b = NWD(a,b) * NWW(a,b)
int greatest_common_divisor(int a, int b) {
while (a != b) {
if (a > b)
a -= b;
else
b -= a;
}
return a;
}
void rotate_with_cycles(int n, int t[], int k) {
k %= n;
k += (k < 0) * n; // if (k < 0) k += n;
int temp, next, aux;
for (int i = 0; i < greatest_common_divisor(n, k); ++i) {
temp = t[i];
for (int j = 0; j < n / greatest_common_divisor(n, k); ++j) {
next = (i + (j + 1) * k) % n;
aux = t[next];
t[next] = temp;
temp = aux;
}
}
}
////////////////////////////////////////////////////////////////
void swap(int t[], int i, int j) {
int temp = t[i];
t[i] = t[j];
t[j] = temp;
}
void mirror(int t[], int left, int right) {
while (left < right) {
swap(t, left, right);
++left;
--right;
}
}
void rotate_with_mirror(int n, int t[], int k) {
k %= n;
if (k < 0) k += n;
mirror(t, 0, n - k - 1);
mirror(t, n - k, n - 1);
mirror(t, 0, n - 1);
}