TheAlgorithms-Ruby/other/fisher_yates.rb

13 lines
311 B
Ruby
Raw Normal View History

2018-10-05 03:45:00 +02:00
# Fisher and Yates Shuffle is one of the simplest and most popular shuffling algorithm
def fisher_yates_shuffle(array)
n = array.length
2021-02-07 08:05:54 +01:00
while n > 0
i = rand(n -= 1)
array[i], array[n] = array[n], array[i]
end
2021-02-07 08:05:54 +01:00
array
2018-10-05 03:45:00 +02:00
end
arr = [1, 2, 40, 30, 20, 15, 323, 12, 3, 4]
2021-02-07 08:05:54 +01:00
puts fisher_yates_shuffle(arr)