java shuffle list_Java中 shuffle 算法的使用

public class javaShuffle {

public static int temp = 0;

public static long start;

public static long end;

public static void main(final String args[]) {

Object changeTemp;

List numList = new ArrayList();

List firstList = new ArrayList();

List secondList = new ArrayList();

List thirdList = new ArrayList();

List fourthList = new ArrayList();

for (int i = 1; i <= 100000; i++) {

numList.add(i);

firstList.add(i);

secondList.add(i);

thirdList.add(i);

fourthList.add(i);

}

// first shuffle,use changeTemp

getStartTime();

int randInt = 0;

for (int i = 0, length = firstList.size(); i < length; i++) {

randInt = getRandom(i, firstList.size());

changeTemp = firstList.get(i);

firstList.set(i, firstList.get(randInt));

firstList.set(randInt, javaShuffle.temp);

}

getEndTime("first shuffle run time ");

// second shuffle,exchange list

getStartTime();

for (int i = 0, length = secondList.size(); i < length; i++) {

randInt = getRandom(i, secondList.size());

secondList.set(i, secondList.set(randInt, secondList.get(i)));

}

getEndTime("second shuffle run time");

// third shuffle, change generate random int

getStartTime();

Object[] tempArray = thirdList.toArray();

Random rand = new Random();

int j = 0;

for (int i = tempArray.length - 1; i > 0; i--) {

j = rand.nextInt(i + 1);

thirdList.set(i, thirdList.set(j, thirdList.get(i)));

}

getEndTime("third shuffle run time ");

// fourth shuffle, simulate java shuffle

getStartTime();

Random random = new Random();

if (!(fourthList instanceof RandomAccess)) {

Object[] array = fourthList.toArray();

for (int i = array.length - 1; i > 0; i--) {

int index = random.nextInt(i + 1);

if (index < 0) {

index = -index;

}

Object temp = array[i];

array[i] = array[index];

array[index] = temp;

}

int i = 0;

ListIterator it = (ListIterator) fourthList.listIterator();

while (it.hasNext()) {

it.next();

it.set((Integer) array[i++]);

}

} else {

List rawList = (List) fourthList;

for (int i = rawList.size() - 1; i > 0; i--) {

int index = random.nextInt(i + 1);

if (index < 0) {

index = -index;

}

rawList.set(index, rawList.set(i, rawList.get(index)));

}

}

getEndTime("fourth shuffle run time");

// java shuffle

getStartTime();

Collections.shuffle(numList);

getEndTime("java shuffle run time  ");

}

public static void swap(int a, int b) {

javaShuffle.temp = a;

a = b;

b = javaShuffle.temp;

}

public static int getRandom(final int low, final int high) {

return (int) (Math.random() * (high - low) + low);

}

public static void getStartTime() {

javaShuffle.start = System.nanoTime();

}

public static void getEndTime(final String s) {

javaShuffle.end = System.nanoTime();

System.out.println(s + ": " + (javaShuffle.end - javaShuffle.start) + "ns");

}

}

如果数值较小,例如100000级别,则输出大概是:

first shuffle run time : 85029499ns

second shuffle run time: 80909474ns

third shuffle run time : 71543926ns

fourth shuffle run time: 76520595ns

java shuffle run time  : 61027643ns

first shuffle run time : 82326239ns

second shuffle run time: 78575611ns

third shuffle run time : 95009632ns

fourth shuffle run time: 105946897ns

java shuffle run time  : 90849302ns

first shuffle run time : 84539840ns

second shuffle run time: 85965575ns

third shuffle run time : 101814998ns

fourth shuffle run time: 113309672ns

java shuffle run time  : 35089693ns

first shuffle run time : 87679863ns

second shuffle run time: 79991814ns

third shuffle run time : 73720515ns

fourth shuffle run time: 78353061ns

java shuffle run time  : 64146465ns

first shuffle run time : 84314386ns

second shuffle run time: 80074803ns

third shuffle run time : 74001283ns

fourth shuffle run time: 79931321ns

java shuffle run time  : 86427540ns

first shuffle run time : 84315523ns

second shuffle run time: 81468386ns

third shuffle run time : 75052284ns

fourth shuffle run time: 79461407ns

java shuffle run time  : 66607729ns

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值