Best shuffle: Difference between revisions

Content added Content deleted
m (→‎{{header|Icon}} and {{header|Unicon}}: fix order + description)
Line 419: Line 419:


=={{header|Icon}} and {{header|Unicon}}==
=={{header|Icon}} and {{header|Unicon}}==
The approach taken requires 2n memory and will run in O(n^2) time swapping once per final changed character. The algorithm is concise and conceptually simpler avoiding the lists of indices, sorting, cycles, groups, and special cases requiring rotation. It proceeds through the entire string swapping characters ensuring that neither of the two characters are swapped with another instance of themselves in the ''original'' string.
The approach taken requires 2n memory and will run in O(n^2) time swapping once per final changed character. The algorithm is concise and conceptually simple avoiding the lists of indices, sorting, cycles, groups, and special cases requiring rotation needed by many of the other solutions. It proceeds through the entire string swapping characters ensuring that neither of the two characters are swapped with another instance of themselves in the ''original'' string.


Additionally, this can be trivially modified to randomize the shuffle.
Additionally, this can be trivially modified to randomize the shuffle.