Merge pull request #37 from pablohildo/master

Added Fisher and Yates Shuffle
This commit is contained in:
Christian Bender 2019-02-14 21:48:05 +01:00 committed by GitHub
commit d75f5a5cca
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

12
other/fisher_yates.rb Normal file
View file

@ -0,0 +1,12 @@
# Fisher and Yates Shuffle is one of the simplest and most popular shuffling algorithm
def fisher_yates_shuffle(array)
n = array.length
while n > 0
i = rand(n-=1)
array[i], array[n] = array[n], array[i]
end
return array
end
arr = [1, 2, 40, 30, 20, 15, 323, 12, 3, 4]
puts fisher_yates_shuffle(arr)