-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy patharray-left-hand-rotation-v1.c
More file actions
86 lines (75 loc) · 2.01 KB
/
array-left-hand-rotation-v1.c
File metadata and controls
86 lines (75 loc) · 2.01 KB
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
/**
* array-left-hand-rotation-v1.c
* @author Wang Guibao (https://github.com/wangguibao/)
* @brief Left-hand rotation of an integer array, using O(1) extra space and
* O(n) running time.
*
* This is an implementation of Question 2 of Ch 2, Programming Pearls 2e.
*/
#include <stdio.h>
#define MAX_ELE 16
void array_left_hand_rotation(int *ele, size_t size, int rotation_count)
{
int offset = 0;
int temp = 0;
int pos = 0;
if (rotation_count % size == 0) {
return;
}
rotation_count %= size;
for (; offset < rotation_count; ++offset) {
temp = ele[offset];
pos = offset;
while (1) {
if (((pos + rotation_count) % size) == offset) {
ele[pos % size] = temp;
break;
}
else {
ele[pos % size] = ele[(pos + rotation_count) % size];
pos += rotation_count;
}
};
if ((size % rotation_count) != 0) {
break;
}
}
}
int main()
{
int n = 0;
int rotation_count = 0;
int ele[MAX_ELE] = {0, };
int ret = 0;
int i = 0;
fprintf(stdout, "Number of elements (max %d): ", MAX_ELE);
ret = scanf("%d", &n);
if (ret != 1) {
fprintf(stderr, "Number of elements not specified\n");
return -1;
}
else if (n > MAX_ELE || n <= 0) {
fprintf(stderr, "Number of elements not valid\n");
return -1;
}
fprintf(stdout, "%d integers: ", n);
for (i = 0; i < n; ++i) {
ret = scanf("%d", &ele[i]);
if (ret != 1) {
fprintf(stderr, "Element %d error\n", i);
return -1;
}
}
fprintf(stdout, "Rotation count: ");
ret = scanf("%u", &rotation_count);
if (ret != 1) {
fprintf(stderr, "Rotation count not specified\n");
return -1;
}
array_left_hand_rotation(ele, n, rotation_count);
for (i = 0; i < n; ++i) {
fprintf(stdout, "%d ", ele[i]);
}
fprintf(stdout, "\n");
return 0;
}