Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions 23 Others/FisherAndYatesShuffle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import java.util.Random;
/*
Fisher and Yates is a popular and simple way to shuffle arrays
*/
public class FisherAndYatesShuffle{
public static int[] fisherYatesShuffle(int[] array){
Random random = new Random();
for (int n = 0; n < array.length; n++){
int i = n + random.nextInt(array.length - n);
int tmp = array[i];
array[i] = array[n];
array[n] = tmp;
}
return array;

}
public static void main(String[] args) {
int[] numbersToShuffle = {2, 230, 540, 10, 6, 7, 8, 11, 45};
for (int i:fisherYatesShuffle(numbersToShuffle)){
System.out.println(i);
}
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.