Fixed shuffling to properly account for the offset when shuffling with a

non-zero offset.  Added a few rudimentary tests.


git-svn-id: https://samskivert.googlecode.com/svn/trunk@763 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
shaper
2002-05-29 00:08:17 +00:00
parent 9c33c3da0f
commit 51eb070a53
@@ -1,5 +1,5 @@
//
// $Id: ArrayUtil.java,v 1.3 2002/05/28 22:32:14 shaper Exp $
// $Id: ArrayUtil.java,v 1.4 2002/05/29 00:08:17 shaper Exp $
//
// samskivert library - useful routines for java programs
// Copyright (C) 2001 Walter Korman
@@ -22,6 +22,8 @@ package com.samskivert.util;
import java.util.Random;
import com.samskivert.Log;
/**
* Miscellaneous utility routines for working with arrays.
*/
@@ -47,11 +49,14 @@ public class ArrayUtil
*/
public static void shuffle (byte[] values, int offset, int length)
{
for (int ii = (offset + length); ii > (offset + 1); ii--) {
byte temp = values[ii - 1];
int idx = _rnd.nextInt(ii);
values[ii - 1] = values[idx];
values[idx] = temp;
// starting from the end of the specified region, repeatedly swap
// the element in question with a random element previous to it
// (in the specified region) up to and including itself
for (int ii = offset + length - 1; ii > offset; ii--) {
int idx = offset + _rnd.nextInt(ii - offset + 1);
byte tmp = values[ii];
values[ii] = values[idx];
values[idx] = tmp;
}
}
@@ -75,11 +80,14 @@ public class ArrayUtil
*/
public static void shuffle (int[] values, int offset, int length)
{
for (int ii = (offset + length); ii > (offset + 1); ii--) {
int temp = values[ii - 1];
int idx = _rnd.nextInt(ii);
values[ii - 1] = values[idx];
values[idx] = temp;
// starting from the end of the specified region, repeatedly swap
// the element in question with a random element previous to it
// (in the specified region) up to and including itself
for (int ii = offset + length - 1; ii > offset; ii--) {
int idx = offset + _rnd.nextInt(ii - offset + 1);
int tmp = values[ii];
values[ii] = values[idx];
values[idx] = tmp;
}
}
@@ -103,14 +111,62 @@ public class ArrayUtil
*/
public static void shuffle (long[] values, int offset, int length)
{
for (int ii = (offset + length); ii > (offset + 1); ii--) {
long temp = values[ii - 1];
int idx = _rnd.nextInt(ii);
values[ii - 1] = values[idx];
values[idx] = temp;
// starting from the end of the specified region, repeatedly swap
// the element in question with a random element previous to it
// (in the specified region) up to and including itself
for (int ii = offset + length - 1; ii > offset; ii--) {
int idx = offset + _rnd.nextInt(ii - offset + 1);
long tmp = values[ii];
values[ii] = values[idx];
values[idx] = tmp;
}
}
public static void main (String[] args)
{
// test shuffling two elements
int[] values = new int[] { 0, 1 };
int[] work = (int[])values.clone();
shuffle(work, 0, 1);
Log.info("first-half shuffle: " + StringUtil.toString(work));
work = (int[])values.clone();
shuffle(work, 1, 1);
Log.info("second-half shuffle: " + StringUtil.toString(work));
work = (int[])values.clone();
shuffle(work);
Log.info("full shuffle: " + StringUtil.toString(work));
// test shuffling three elements
values = new int[] { 0, 1, 2 };
work = (int[])values.clone();
shuffle(work, 0, 2);
Log.info("first-half shuffle: " + StringUtil.toString(work));
work = (int[])values.clone();
shuffle(work, 1, 2);
Log.info("second-half shuffle: " + StringUtil.toString(work));
work = (int[])values.clone();
shuffle(work);
Log.info("full shuffle: " + StringUtil.toString(work));
// test shuffling ten elements
values = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
work = (int[])values.clone();
shuffle(work, 0, 5);
Log.info("first-half shuffle: " + StringUtil.toString(work));
work = (int[])values.clone();
shuffle(work, 5, 5);
Log.info("second-half shuffle: " + StringUtil.toString(work));
work = (int[])values.clone();
shuffle(work);
Log.info("full shuffle: " + StringUtil.toString(work));
}
/** The random object used when shuffling an array. */
protected static Random _rnd = new Random();
}