First commit of partial RosettaCode contents.
Pushing this for testing purposes. Lots of work still needed.
This commit is contained in:
commit
1e05ecd7ee
781 changed files with 9080 additions and 0 deletions
24
Task/Knuth_shuffle/Java/knuth_shuffle.java
Normal file
24
Task/Knuth_shuffle/Java/knuth_shuffle.java
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
import java.util.Random;
|
||||
|
||||
public static final Random gen = new Random();
|
||||
|
||||
// version for array of ints
|
||||
public static void shuffle (int[] array) {
|
||||
int n = array.length;
|
||||
while (n > 1) {
|
||||
int k = gen.nextInt(n--); //decrements after using the value
|
||||
int temp = array[n];
|
||||
array[n] = array[k];
|
||||
array[k] = temp;
|
||||
}
|
||||
}
|
||||
// version for array of references
|
||||
public static void shuffle (Object[] array) {
|
||||
int n = array.length;
|
||||
while (n > 1) {
|
||||
int k = gen.nextInt(n--); //decrements after using the value
|
||||
Object temp = array[n];
|
||||
array[n] = array[k];
|
||||
array[k] = temp;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue