Arrays/Java/FisherYates
From charlesreid1
Contents
Fisher Yates Shuffle
Implementation of a classic shuffle algorithm using various Java data structures.
Pseudocode
The Fisher Yates shuffle algorithm walks through an entire list of numbers, starting at the back. The pointer points to the index being shuffled. A random index between 0 and the current index being shuffled is selected, and the two values are swapped. The pointer then decrements by one (moves one index closer to the front of the list).
Note that the index i should not cover the first item in the list (i=0).
The pseudocode for shuffling an array of length n is:
for i in n..1: j = random number in 0..i swap array[i], array[j] decrement i
Arrays
Example of class: https://git.charlesreid1.com/cs/java/src/master/arrays/fisher-yates/FisherYates.java
Deck of Cards
This example tests the Fisher Yates shuffle method by creating a deck of cards as an array of Strings, and passes the array to the method to shuffle it in place.
import java.io.*; import java.util.*; /** * Fisher Yates Shuffle class. * * This class implements the Fisher-Yates shuffle * as a static method. * * This accepts an array as input, and shuffles it in-place. */ public class FisherYates { /** swap two "Objects" */ private static void swap(Object[] arr, int x, int y){ if(x != y) { Object temp = arr[x]; arr[x] = arr[y]; arr[y] = temp; } } /** * Fisher Yates shuffle : this is getting complicated. * How to implement this with arrays of generic types? */ public static void shuffleFY(String[] inputs) { Random r = new Random(); int n = inputs.length; for(int j=n-1; j>0; j--) { // Fisher Yates shuffle: // j starts at n-1 // swap with random element between 0 and j (INCLUSIVE) int k = r.nextInt(j+1); swap(inputs, j, k); } } public static void main(String[] args) { Deck d = new Deck(); System.out.println(d); System.out.println("-------------------"); shuffleFY(d.toArray()); System.out.println(d); } }
Driver to test
/** Hope you like cards. * * This implements a standard deck of 52 cards. */ class Deck { private String[] deck; // define how your cards work private final int capacity = 52; private final String[] SUITS = {"SPADES","CLUBS","HEARTS","DIAMONDS"}; private final String[] FACES = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"}; /** constructor don't need no arguments */ public Deck(){ deck = new String[capacity]; int card = 0; for(String suit : SUITS) { for(String face : FACES) { deck[card] = face + " of " + suit; card++; } } } /** turn into string array */ public String[] toArray() { return deck; } /** turn into string */ public String toString() { StringBuffer s = new StringBuffer(); for(String this_card : deck) { s.append(this_card+"\n"); } return s.toString(); } }
Flags
Data Structures Part of Computer Science Notes
This is the staging ground for computer science notes as part of the 2017 CS Study Plan.
Classes of data structures: Abstract Data Types Array-based and Link-based memory management: ArrayLists and Linked Lists Algorithmic Analysis of Data Structures: Algorithmic Analysis of Data Structures Advanced data structures: Advanced Data Structures
|
Arrays Part of Computer Science Notes
Series on Data Structures Python: Arrays/Python · Arrays/Python/Sizeof · Arrays/Python/AppendCost · Arrays/Python/CaesarCipher · Arrays/Python/CompactArrays · Arrays/Python/DynamicArray Java: Arrays/Java · Arrays/Java/CaesarCipher · Arrays/Java/FisherYates · Arrays/Java/PythonList · Arrays/Java/Repeatedly_Remove Categories: Category:Python Arrays
|
Stacks and Queues Part of Computer Science Notes
Series on Data Structures
Stacks and Queues: Python StacksQueues/Python · StacksQueues/Python/ArrayStack · StacksQueues/Python/ArrayQueue · StacksQueues/Python/ArrayDeque StacksQueues/Python/LinkedStack
Stacks and Queues: Java StacksQueues/Java · StacksQueues/Java/ArrayStack · StacksQueues/Java/ArrayQueue · StacksQueues/Java/ArrayQueueFS · StacksQueues/Java/ArrayDeque StacksQueues/Java/LinkedStack · StacksQueues/Java/LinkedQueue · StacksQueues/Java/LinkedDeque
Applications Postfix_Expressions#Stacks · StacksQueues/Subsets · StacksQueues/Subsets/Java
|
Priority Queues and Heaps Part of Computer Science Notes
Series on Data Structures
Java: Priority Queues/Java · Priority Queues/ADT · Priority Queues/Sorted · Priority Queues/Unsorted Performance: Priority Queues/Timing and Performance Applications: Maximum Oriented Priority Queue · Priority Queues/Stack
Priority Queues/Heap · Priority Queues/Java · Priority Queues/Comparators
|
Linked List Part of Computer Science Notes
Series on Data Structures Java: Linked Lists/Java · Linked Lists/Java/Single · Linked Lists/Java/Double · Linked Lists/Java/Circular Performance: Linked Lists/Java/Timing · Linked Lists/Java/Reverse Python: Linked Lists/Python · Linked Lists/Python/Single
|
Trees Part of Computer Science Notes
Series on Data Structures Abstract data type: Trees/ADT Concrete implementations: Trees/LinkedTree · Trees/ArrayTree · SimpleTree
Tree Traversal Preorder traversal: Trees/Preorder Postorder traversal: Trees/Postorder In-Order traversal: Binary Trees/Inorder Breadth-First Search: BFS Breadth-First Traversal: BFT Depth-First Search: DFS Depth-First Traversal: DFT OOP Principles for Traversal: Tree Traversal/OOP · Tree Traversal/Traversal Method Template Tree operations: Trees/Operations Performance · Trees/Removal
Tree Applications Finding Minimum in Log N Time: Tree/LogN Min Search
Abstract data type: Binary Trees/ADT Concrete implementations: Binary Trees/LinkedBinTree · Binary Trees/ArrayBinTree Binary Trees/Cheat Sheet · Binary Trees/OOP · Binary Trees/Implementation Notes
|
Search Trees Part of Computer Science Notes
Series on Data Structures
Binary Search Trees · Balanced Search Trees Trees/OOP · Search Trees/OOP · Tree Traversal/OOP · Binary Trees/Inorder
(Note that heaps are also value-sorting trees with minimums at the top. See Template:PriorityQueuesFlag and Priority Queues.)
|
Maps and Dictionaries Part of Computer Science Notes
Series on Data Structures
Maps/Dictionaries Maps · Maps/ADT · Maps in Java · Maps/OOP · Maps/Operations and Performance Map implementations: Maps/AbstractMap · Maps/UnsortedArrayMap · Maps/SortedArrayMap Dictionary implementations: Dictionaries/LinkedDict · Dictionaries/ArrayDict
Hashes Hash Maps/OOP · Hash Maps/Operations and Performance Hash Maps/Dynamic Resizing · Hash Maps/Collision Handling with Chaining Hash functions: Hash Functions · Hash Functions/Cyclic Permutation Hash map implementations: Hash Maps/AbstractHashMap · Hash Maps/ChainedHashMap
Skip Lists · Java/ConcurrentSkipList · Java implementations: SkipList
Sets Sets · Sets/ADT · Sets in Java · Sets/OOP · Multisets
|