mirror of
https://github.com/TheAlgorithms/Ruby
synced 2024-12-25 21:58:57 +01:00
12 lines
311 B
Ruby
12 lines
311 B
Ruby
# 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
|
|
array
|
|
end
|
|
|
|
arr = [1, 2, 40, 30, 20, 15, 323, 12, 3, 4]
|
|
puts fisher_yates_shuffle(arr)
|