Loop right shift-dolphin algorithm (doldolphin)

Source: Internet
Author: User

 

**

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/>} 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.