Loop right shift problem for array elements
First, the problem description
Moves an array with length elements to the right by n bits, requiring a time complexity of O (n)
Second, the source code
1 Packagecn.com.zfc.day009;2 3 Importjava.util.Arrays;4 ImportJava.util.Scanner;5 6 /**7 * array element loop right Shift8 * 9 * @authorZFCTen * @date November 3, 2017 pm 4:57:55 One */ A Public classMovearray { - Public Static voidMain (string[] args) { -Scanner Scanner =NewScanner (system.in); theSystem.out.println ("Please enter the length of the array:"); - intLength =scanner.nextint (); - int[] Array =New int[length]; -System.out.println ("Please enter the number of units to move to the right of each element:"); + intn =scanner.nextint (); -System.out.println ("Please enter" + length + "integer:"); + for(inti = 0; i < length; i++) { AArray[i] =scanner.nextint (); at } - scanner.close (); - //Method 1 -System.out.println ("---------------------Method 1---------------------"); - moveArray1 (array, n); - //Method 2 inSystem.out.println ("---------------------Method 2---------------------"); - moveArray2 (array, n); to //Method 3 +System.out.println ("---------------------Method 3---------------------"); - moveArray3 (array, n); the } * $ /**Panax Notoginseng * Method 1: Time complexity, O (length*n) - * the * @paramarray: Arrays to move + * @paramN: Number of units to move right for each element A */ the Private Static voidMoveArray1 (int[] arr,intN) { + int[] Array =arrays.copyof (arr, arr.length); -System.out.println ("original array:" +arrays.tostring (array)); $ for(inti = n; i > 0; i--) { $ intt = array[array.length-1]; - for(intj = array.length-1; J > 0; j--) { -ARRAY[J] = array[j-1]; the } -Array[0] =T;Wuyi } theSystem.out.println ("Move right after array:" +arrays.tostring (array)); - } Wu - /** About * Method 2: Time complexity, O (n*n) $ * - * @paramarray: Arrays to move - * @paramN: Number of units to move right for each element - */ A Private Static voidMoveArray2 (int[] arr,intN) { + int[] Array =arrays.copyof (arr, arr.length); theSystem.out.println ("original array:" +arrays.tostring (array)); - //Right Shift n is the same effect as right shift n% length $N%=Array.Length; the for(inti = n; i > 0; i--) { the intt = array[array.length-1]; the for(intj = array.length-1; J > 0; j--) { theARRAY[J] = array[j-1]; - } inArray[0] =T; the } theSystem.out.println ("Move right after array:" +arrays.tostring (array)); About } the the /** the * Method 3: Time complexity, O (n) + * - * @paramarray: Arrays to move the * @paramN: Number of units to move right for each elementBayi */ the Private Static voidMoveArray3 (int[] arr,intN) { the int[] Array =arrays.copyof (arr, arr.length); -System.out.println ("original array:" +arrays.tostring (array)); -N%=Array.Length; theReverse (array, 0, array.length-n-1); theReverse (array, array.length-n, array.length-1); theReverse (array, 0, array.length-1); theSystem.out.println ("Move right after array:" +arrays.tostring (array)); - } the the Private Static voidReverseint[] arr,intXinty) { the for(; x < y; n + +, y--) {94 inttemp =Arr[y]; theArr[y] =Arr[x]; theARR[X] =temp; the }98 } About}
Third, the Operation effect
Loop right shift problem for array elements