In the game, you need to disrupt a known set of data, summarizing the following methods, both before and now.
Method One, the most stupid rookie method, is also easy to think of (fortunately I did not think of this method:))
A random number from a known array, then add it to another array, and check to see if you have joined before joining.
This method has a lot of luck, and the larger the data, the lower the efficiency, the more than a certain number, the program can hardly execute, will always be stuck there, code:[Java] View plain copy package com.test; import java.util.random; public class testarray { public static int runcount =0;//used to record method operations public int [] &NBSP;M1 (Int [] arr) { random ran = new random (); int length = arr.length; int [] newArr = new int[length]; int count =0; while (true) { runCount ++; int r = ran.nextint (length) +1; if (!exist (R, newarr)) { newArr [count] = r; count++; & nbsp; } if (count==length) { break; } } system.out.println ("m1 Operation Times = "+runcount); return newArr; } public static boolean exist (Int a,int [] arr) { for (int i = 0; i < arr.length; i++) { if (arr[i] ==a) { return true; } } return false; }
Method Two, this method is I encountered this problem to think of, the idea is: first random out of the given group of subscript value, and then take out the number into another array, and then remove the number from the known array. The calculation of this method is based on the length of the array, the disadvantage is that most operations are consumed in the judgment of deletion, mainly because the array does not like the list, can delete a specified position of the value. Code:
[Java] View Plain copy //----------------------------------------------- public int [] &NBSP;&NBSP;M2 (Int [] arr) { random ran = new random (); int [] newArr = new int[arr.length]; int k = 0; while (arr.length>0) { runCount++; int index = ran.nextint (arr.length)//immediately array subscript newarr[k++] = arr[index]; arr = removeelement (Arr[index], arr); } system.out.println ("M2 Number of operations = " +runcount); return newarr; } public int [] removeelement (int Ele,int [] arr) { int [] arr2 =new int[arr.length-1]; int k = 0; for (int i = 0; i < arr.length; i++) { runCount++; if (arr[i]!=ele) { arr2[k++] = arr[i]; } } return arr2; }
Method Three, this method is very ingenious, is seen in the Code of C, translated into Java code, it is clever: each time from the known array random number, and then assign the last value of the array to the previous random number of positions, and then the length of 1, and then from the original array subscript 1 of the array of random. The number of operations is the length of the array, which is really smart, and here's the code:[Java] View Plain copy //----------------------------------------------- public int [] &NBSP;&NBSP;M3 (Int [] arr) { int [] arr2 =new int[ arr.length]; int count = arr.length; int cbrandcount = 0;// index int cbPosition = 0;// position int k =0; do { runCount++; random rand = new random (); int r = count - cbrandcount; cbposition = rand.nextint (r); arr2[ k++] = arr[cbposition]; cbRandCount++; arr[cbposition] = arr[r - 1];// assigns the last value to the cbposition } while that has been used ( cbrandcount < count); system.out.println ("M3 operand = " +runcount); return arr2; }
Test the following code:[Java] View plain copy // ---------------------------------------------- public& Nbsp;static void main (String[] args) { int[] arr = new int[10]; for (int i = 0; i < arr.length; i++) { arr[i] = i + 1; } Testarray t = new testarray (); arr = t.m1 (arr ); print (arr); arr = t.m2 (arr) ; print (arr); arr = t.m3 (arr); print (arr); } public static VoiD print (Int[] arr) { for (int i = 0; i < arr.length; i++) { system.out.print (arr[i] + " "); } system.out.println (); runCount = 0; }
Results: [Java] view Plain Copy m1 count = 3 2 1 5 9 6 8 4 7 m2 operation times = 1 3 8 2 5 9 6 7 4 M3 operation Times = 10 10 7 8 3 1 5 6 9 2 4
The third method is still worth learning, if you have other good methods and ideas Welcome to add ~ ~