This class implements:
Output a row of array data, according to the input subscript, the following position is the end, the original array is divided into two sets of sub-arrays.
and swaps the positions of two sub-arrays, keeping the ordinal number of the elements in the subarray unchanged.
For example: The original array is 7,9,8,5,3,2 the following 3 is the split point, divided into sub-array one: 7,9,8,5. and sub-array two: 3,2.
The result after the switching algorithm should be: 3,2,7,9,8,5
There are two kinds of switching algorithms
<1> Pre-interpolation: 3,2 the Subarray to another temporary array, moving the original array 7,9,8,5,3,2 each bit backward by two positions
Then insert the sub-array 3,2 into the original array where you moved the good element.
<2> Inverse method: 7,9,8,5,3,2 The original array to 2,3,5,8,9,7
Two sub-arrays that are separated by the inverse of each other, the result should be: 3,2,7,9,8,5
Package order table; import java.util.arraylist;import java.util.scanner;/** * @param args * @author Liu Yanbing * @date 2015-2-2 19:43 *// * * This type of implementation: * Output a row of array data, according to the input subscript, the following position is the end, the original array is divided into two sets of sub-arrays. * and swap the position of two sub-arrays, keeping the ordinal of the elements in the sub-array unchanged. * For example: The original array is 7,9,8,5,3,2 the following 3 is the split point, divided into sub-array one: 7,9,8,5. and sub-array two: 3,2. * The results after the switching algorithm should be: 3,2,7,9,8,5 * * There are two switching algorithms * <1> pre-interpolation: 3,2 the sub-array to another temporary array, the original array 7,9,8,5,3,2 each bit backwards two positions * <2> inverse: The original array 7 , the 9,8,5,3,2 is reversed to 2,3,5,8,9,7 * and the two sub-arrays are respectively inverted, the result should be: 3,2,7,9,8,5 */public class Resetorderlistpostion {/* * int []order: Array stores the original array of user input * int postion: Store user input delimited subscript */static int []order;static int postion;/* * forward interpolation */public void Frontinsert (int []or Derinsert,int postion) {/* * use ArrayList linked list to store delimited subarray one */arraylist<integer> lista=new arraylist<integer> () ; for (int i=postion+1;i<orderinsert.length;i++) {lista.add (orderinsert[i]);} int a[]=new int[lista.size ()];for (int i=0;i<lista.size (); i++) {a[i]=lista.get (i);//Move the original array back to the length of the sub-array for each element (int j= orderinsert.length-1;j>0;j--) {orderinsert[j]=orderinsert[j-1];}} Inserts a sub-array into theMove the good sub-array for (int k=a.length-1;k>=0;k--) {orderinsert[k]=a[k];} Note the end of the last element, the number System.out.println ("Using the pre-interpolation method---Swap position after the array result is as follows:"); for (int j=0;j<orderinsert.length;j++) {if (j== orderinsert.length-1) System.out.print (Orderinsert[j]); ElseSystem.out.print (orderinsert[j]+ ",");}} /* * Inverse method */public void inversion (int []orderinversion,int postion) {//Invert entire original array for (int i=0;i<orderinversion.length/2;i + +) {int t=orderinversion[i];orderinversion[i]=orderinversion[orderinversion.length-1-i];orderinversion[ orderinversion.length-1-i]=t;} Inverse sub-array one for (int i=0;i< (orderinversion.length-postion)/2;i++) {int t=orderinversion[i];orderinversion[i]= orderinversion[orderinversion.length-postion-2-i];orderinversion[orderinversion.length-postion-2-i]=t;} Inverse sub-array two for (int i=0;i< (postion+1)/2;i++) {int t=orderinversion[orderinversion.length-1-i];orderinversion[ orderinversion.length-1-i]=orderinversion[orderinversion.length-postion-1+i];orderinversion[ orderinversion.length-postion-1+i]=t;} Notice that the last element is eliminated, number System.out.println ("UseThe inverse method---the exchange position with the following result: "); for (int i=0;i<orderinversion.length;i++) {if (i==orderinversion.length-1) System.out.print ( Orderinversion[i]); ElseSystem.out.print (orderinversion[i]+ ",");} System.out.println ();} public static void Main (string[] args) {//TODO auto-generated method stubresetorderlistpostion rp=new Resetorderlistpost Ion (); System.out.println ("Please enter an array, press 1 to end the input"); Arraylist<integer>list=new arraylist<integer> (); Scanner sc=new Scanner (system.in); int m=sc.nextint (); while (M!=-1) {list.add (M); M=sc.nextint ();} int []order=new int[list.size ()];for (int i=0;i<list.size (); i++) {order[i]=list.get (i);} System.out.println ("The array data you entered is:"); for (int i=0;i<order.length;i++) {if (i==order.length-1) System.out.print (order [i]); ElseSystem.out.print (order[i]+ ",");} System.out.println (); System.out.println ("Please enter the subscript to separate the original array into two arrays (note that the input subscript cannot be less than 0 and cannot be greater than or equal to the array length):"); int postion=sc.nextint (); System.out.println ("You enter the partition subscript as: \ n" +postion);//Determine the input of the delimited subscript validity if (postion<0| | Postion>=order.length) System.out.println ("wrong input!"); ElSe{system.out.println ("******************** Please select the array position exchange Algorithm ********************"); System.out.println ("********************1--: Pre-insertion Method ********************"); System.out.println ("********************2--: Inverse Method ********************"); int n=sc.nextint (); switch (n) {case 1:{ Rp.frontinsert (order, postion); Case 2:{rp.inversion (order, postion); DEFAULT:SYSTEM.OUT.PRINTLN ("Wrong input!");}}}
Java implementation The original array is separated into two sub-arrays according to the subscript and the position of two sub-arrays is exchanged in the original array