Java中打亂一個數(shù)組的2種公平算法分享
公平算法,打亂數(shù)組
這是前幾天面試的時候遇見的一道題目,看到這個題首先想到了洗牌程序:
方法一:洗牌程序原理
在java.util包中的Collections類中的 shuffle方法,現(xiàn)在手工實(shí)現(xiàn)以下代碼如下:
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 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;
}
}
swap方法用于交換數(shù)組中的兩個數(shù), shuffle方法 用于 根據(jù)隨機(jī)源 生成的隨機(jī)數(shù)進(jìn)行交換。
輸出結(jié)果如下:
before redistribute: 1 5 4 3 6 9 8 7 0 8 5 6 7 2 after redistribute: 9 8 7 8 0 6 1 6 5 5 2 3 7 4
方法二:生成隨機(jī)索引交換
該方法利用Set集合的特性:Set集合中的數(shù)據(jù)不重復(fù),生成數(shù)組的索引,根據(jù)生成的索引進(jìn)行交換數(shù)據(jù)。
實(shí)現(xiàn)方式如下:
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 the result;
System.out.println("after redistribute:");
for(int i = 0 ; i<s.length; i++){
System.out.print(out[i]+" ");
}
}
}
這個方法首先生成索引,然后根據(jù)新索引進(jìn)行數(shù)據(jù)交換,代碼都寫在main方法里了,不是太好。
生成結(jié)果如下:
before redistribute: 1 5 4 3 6 9 8 7 0 8 5 6 7 2 redistribute the index [6, 2, 9, 1, 10, 5, 11, 4, 12, 3, 7, 8, 0, 13] after redistribute: 8 4 8 5 5 9 6 6 7 3 7 0 1 2
關(guān)于隨機(jī)數(shù)的生成,用了java類中的隨機(jī)數(shù)的生成的工具類,這個隨機(jī)類需要單獨(dú)研究一下。
相關(guān)文章
springboot無法從靜態(tài)上下文中引用非靜態(tài)變量的解決方法
這篇文章主要介紹了springboot無法從靜態(tài)上下文中引用非靜態(tài)變量的解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-06-06
Java數(shù)據(jù)結(jié)構(gòu)之鏈表(動力節(jié)點(diǎn)之Java學(xué)院整理)
這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)之鏈表(動力節(jié)點(diǎn)之Java學(xué)院整理)的相關(guān)資料,需要的朋友可以參考下2017-04-04
IDEA基于支付寶小程序搭建springboot項(xiàng)目的詳細(xì)步驟
這篇文章主要介紹了IDEA基于支付寶小程序搭建springboot項(xiàng)目的詳細(xì)步驟,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-04-04
為Java應(yīng)用創(chuàng)建Docker鏡像的3種方式總結(jié)
Docker的使用可以將應(yīng)用程序做成鏡像,這樣可以將鏡像發(fā)布到私有或者公有倉庫中,在其他主機(jī)上也可以pull鏡像,并且運(yùn)行容器,運(yùn)行程,下面這篇文章主要給大家總結(jié)介紹了關(guān)于為Java應(yīng)用創(chuàng)建Docker鏡像的3種方式,需要的朋友可以參考下2023-06-06
如何使用SpringBoot進(jìn)行優(yōu)雅的數(shù)據(jù)驗(yàn)證
這篇文章主要介紹了如何使用SpringBoot進(jìn)行優(yōu)雅的數(shù)據(jù)驗(yàn)證,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11
SpringBoot中多環(huán)境yml的配置與打包問題
這篇文章主要介紹了SpringBoot中多環(huán)境yml的配置與打包問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09
java實(shí)現(xiàn)1M圖片壓縮優(yōu)化到100kb實(shí)現(xiàn)示例
這篇文章主要為大家介紹了java實(shí)現(xiàn)1M圖片壓縮優(yōu)化到100kb示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
Spring Boot 配置和使用多線程池的實(shí)現(xiàn)
這篇文章主要介紹了Spring Boot 配置和使用多線程池的實(shí)現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06

