-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy patharray_rhr.c
48 lines (39 loc) · 873 Bytes
/
array_rhr.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
/*
* array_rhr.c - Right shift all elements in an array, using 1 swap element
* space. Also must keep the complexity O(n).
*
* This algorithm is copied from the Internet,
* http://blog.sina.com.cn/s/blog_3ee549da01009msh.html
* Many thanks to the author.
*/
#include <stdio.h>
#define ARRAY_LEN 6
void Reverse(int *arr, int a, int b)
{
int temp=0;
while(a < b) {
temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
a++;
b--;
}
}
void RightShift(int *arr, int n, int k)
{
k %= n;
Reverse(arr, 0, n-k-1);
Reverse(arr, n-k, n-1);
Reverse(arr, 0, n-1);
}
int main(int argc, char *argv[])
{
int test[ARRAY_LEN] = {0, 1, 2, 3, 4, 5};
int i = 0;
RightShift(test, 6, 2);
for (i = 0; i < ARRAY_LEN; i++) {
printf("%d ", test[i]);
}
printf("\n");
exit(0);
}