**
Design an algorithm. This algorithm shifts K to the right of all elements in array a [0 .. n-1]. The time complexity of the algorithm is O (n), and the number of auxiliary spaces cannot exceed 1.
* The easiest way to think of is to shift right of the loop (the time complexity is O (kN), and the auxiliary space is 1)
Void circulate (elemtype A [], int N, int K) <br/>{< br/> int T, J; // cyclically K times (shifted right K bit) <br/> elemtype temp; <br/> for (t = 1; t <= K; t ++) {<br/> temp = A [n-1]; // save the last element of the array <br/> for (j = n-2; j> = 0; j --) {<br/> A [J + 1] = A [J]; // shifts all elements of the array to one right. <br/> A [0] = temp; <br/>}< br/>}
* Dolphin algorithm shift (time complexity O (N), 1 auxiliary space)
Void circulate (elemtypea [], int N, int K) <br/>{< br/> int COUNT = 1, I = 0, j = 0; <br/> elemtype temp; <br/> while (count <n) {<br/> J = (J + k) % N; <br/> if (I! = J) {<br/> temp = A [I]; <br/> A [I] = A [J]; <br/> A [J] = temp; // exchange the positions of a [I] And a [J], which are in place at a time. Because the painting process is like a dolphin jumping, it is named O (╯ □╰) O <br/>}< br/> else {<br/> I ++; <br/> J ++; <br/>}< br/> count ++;/* accumulate counter count once */<br/>}< br/>}
* Debuggable viewing effect edition:
# Include <stdio. h> <br/> # include <stdlib. h> <br/> # define N 10 <br/> int main () {<br/> int COUNT = 1, TMP; <br/> int A [n]; <br/> int I, j, k; <br/> do {<br/> for (I = 0; I <n; I ++) {<br/> A [I] = rand () % (n * n); // pseudo-random number generation (less than N * n) <br/> printf ("% d", a [I]); <br/>}< br/> printf ("/noffest :"); <br/> scanf ("% d", & K); // input displacement <br/> I = 0; j = 0; <br/> while (count <n) {// dolphin shift <br/> J = (J + k) % N; <br/> if (I! = J) {<br/> TMP = A [I]; <br/> A [I] = A [J]; <br/> A [J] = TMP; <br/>}< br/> else {<br/> I ++; <br/> J ++; <br/>}< br/> count ++; <br/>}< br/> for (I = 0; I <n; I ++) <br/> printf ("% d", a [I]); <br/> printf ("/n") <br/>}while (1); <br/> return 0; <br/>}