2 fair algorithms that disrupt an array in Java share _java

Source: Internet
Author: User
Tags set set shuffle

Fair algorithm, scrambling arrays

This is a few days before the interview met a problem, see this problem first thought of the Shuffle program:

Method One: Shuffle the procedure principle

In the shuffle method in the collections class in the Java.util package, the following code is now implemented manually as follows:

 package test.ms;

Import Java.util.Random; public class Redistribute2 {public static void main (string[] args) {//define The array int[] s = {1,5,4,3,6,9,8
		
		, 7,0,8,5,6,7,2};
		Before redistribute output System.out.println ("Before Redistribute:");
		for (int i = 0; i<s.length; i++) {System.out.print (s[i]+ "");
		
		}//Invoke the method shuffle (S,new Random ());
		
		System.out.println ();
		After the redistribute output System.out.println ("After Redistribute:");
		for (int i = 0; i<s.length; i++) {System.out.print (s[i]+ "");  }//using the random get the random number public static void Shuffle (int[] array, random random) {for (int i = Array.Length; I >= 1;
		i--) {Swap (Array,i-1,random.nextint (i)); }//The two number swap in the array public static void swap (int[] array, int i, int j) {int temp = Array[i
		
		];
		
		Array[i] = Array[j];
	 
		
	ARRAY[J] = temp; }
}


The swap method is used to exchange two numbers in an array, and the shuffle method is used to exchange random numbers generated from random sources.
The output results are as follows:

Before redistribute:
1 5 4 3 6 9 8 7 0 8 5 6 7 after 
redistribute:


method Two: Generate Random index exchange

This method utilizes the characteristics of the set set: The data in the set set is not duplicated, the index of the array is generated, and the data is exchanged according to the generated index.

The implementation method is as follows:

Package test.ms;
Import Java.util.Iterator;
Import Java.util.LinkedHashSet;
Import Java.util.Random;

Import Java.util.Set;
		public class Redistribute {public static void main (string[] args) {int[] s = {1,5,4,3,6,9,8,7,0,8,5,6,7,2};
	Redistribute (s);
		
		public static void Redistribute (int[] s) {Random Random = new Random ();
		
		set<integer> set = new linkedhashset<integer> ();
			Redistribute the index while (true) {int T =random.nextint (s.length);
			Set.add (t);
		if (set.size () = = s.length) break;
		} System.out.println ("Before Redistribute:");
		for (int i = 0; i<s.length; i++) {System.out.print (s[i]+ "");
		} System.out.println (); System.out.println ("Redistribute the Index");
		
		SYSTEM.OUT.PRINTLN (set);
		
		int [] out = new Int[s.length];
		
		int count = 0;
			for (iterator<integer> iterator = Set.iterator (); Iterator.hasnext ();) {Out[count] = S[iterator.next ()];
		count++;
		}//out of result; System.out.println ("After redistribute:");
		for (int i = 0; i<s.length; i++) {System.out.print (out[i]+ ""); }
	}
	
}

This method first generates the index, then data exchange based on the new index, the code is written in the main method, not too good.

The resulting results are as follows:

Before-Redistribute:
1 5 4 3 6 9 8 7 0 8 5 6 7 2, 6 2, 9, 1, 10., 5 
, 0] After
redistribute:


On the generation of random numbers, we use the tool class of random number in Java class, this random class needs to be studied separately.

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.