Skip to content
Open
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
19 changes: 19 additions & 0 deletions Fisher-Yates-Shuffle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import java.util.Random;

public class FisherYatesShuffle {
public static void shuffleArray(int[] array) {
Random random = new Random();
for (int i = array.length - 1; i > 0; i--) {
int index = random.nextInt(i + 1);
int temp = array[index];
array[index] = array[i];
array[i] = temp;
}
}

public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5};
shuffleArray(array);
System.out.println("Shuffled Array: " + Arrays.toString(array));
}
}