mirror of
https://github.com/TheAlgorithms/Ruby
synced 2025-01-17 06:11:43 +01:00
Added Fisher and Yates Shuffle
This commit is contained in:
parent
7175100e8c
commit
57837d4a96
1 changed files with 12 additions and 0 deletions
12
other/fisher_yates.rb
Normal file
12
other/fisher_yates.rb
Normal 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)
|
Loading…
Reference in a new issue