Source: Rshift.cpp
#include"stdafx.h"#include<stdio.h>/************************************************************************//*array loop right shift algorithm*//************************************************************************//** Requirements: Only one element size of the auxiliary space, and the time complexity of O (n)*///************************************//Method: Seek greatest common divisor (divide)//FULLNAME:GCD//Access:public//Returns:int//Qualifier://Parameter:int M//parameter:int N//************************************intgcdintMintN) { returnN? GCD (n, m%N): M;}//************************************//method: Cyclic Right Shift solution one (popular solution)//FULLNAME:RSHIFT1//Access:public//returns:void//Qualifier://Parameter:int array[]//parameter:int Length//Parameter:int Shift//************************************voidRSHIFT1 (intArray[],intLengthintshift) { //minimum number of loops to move the chain//Set Length=m,shift=n//[ 0], [n%m], [2n%m]--[mn%m]=[0]= start//assuming kn%m==0, m=gcd*x,n=gcd*y, then X and Y are mutually exclusive//then k*y%x==0,y and x mutually exclusive, so x can be divisible by K, so K=M/GCD//k is the length of a chain, so the number of chains is M/K=GCD intLeast_movement = gcd (length, shift);//minimum number of loops to move the chain inti; for(i =0; i < least_movement; i++) { intSwap_a =i; intSwap_b = (i + Shift)%length; intTMP =Array[swap_a]; while(Swap_b! =i) {array[swap_a]=Array[swap_b]; Swap_a=swap_b; Swap_b= (swap_b + shift)%length; } Array[swap_a]=tmp; }}//************************************//method: Cyclic Right Shift Solution II (flip solution)//fullname:rshift2//Access:public//returns:void//Qualifier://Parameter:int array[]//parameter:int Length//Parameter:int Shift//************************************voidRSHIFT2 (intArray[],intLengthintshift) { //Palm-Flipping algorithm//set Length=m,shift=n (n=n%m)//method: Split array = array1[0 ... n-1] | array2[n. m-1]//array1 Flip, array2 Flip, then overall flip// //Original: 1,2,3,4,.., n-1 | n,..., m-2,m-1//respective flip: n-1,n-2,..., 2,1 | m-1,m-2,..., n//Flip All: n,n+1,..., m-2,m-1 |,..., n-2,n-1//This completes the task of moving the N units right. inti; Shift%=length; inttmp; for(i =0; I < (Shift-1) /2; i++) {tmp=Array[i]; Array[i]= Array[shift-i-1]; Array[shiftI1] =tmp; } for(i = shift; I < SHIFT + (Length-shift)/2; i++) {tmp=Array[i]; Array[i]= Array[length + Shift-i-1]; Array[length+ Shift-i-1] =tmp; } for(i =0; I < length/2; i++) {tmp=Array[i]; Array[i]= Array[length-i-1]; Array[lengthI1] =tmp; }}voidPrint_array (intArray[],intlength) { inti; for(i =0; i < length; i++) {printf ("%d", Array[i]); } printf ("\ n");}intMainintargcChar*argv[]) { intA[] = {1,2,3,4,5,6 }; printf ("=====================\n"); Print_array (A,6); printf ("========== rshift ==========\n"); Rshift1 (A,6,3); Print_array (A,6); printf ("========== rshift ==========\n"); Rshift2 (A,6,3); Print_array (A,6); return 0;}
Linear table (i)--array loop right SHIFT algorithm