/** * This method shuffles the items in the linked list using the Fisher-Yates shuffle. * * @access public * @static * @param ILinkedList\Type $xs the linked list to be shuffled * @return ILinkedList\Type the shuffled linked list * * @see http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle */ public static function shuffle(ILinkedList\Type $xs) : ILinkedList\Type { $buffer = $xs->unbox(); $length = count($buffer); for ($i = $length - 1; $i > 0; $i--) { $j = rand(0, $i); $value = $buffer[$j]; $buffer[$j] = $buffer[$i]; $buffer[$i] = $value; } return ILinkedList\Type::box($buffer); }