programming-examples/ruby/Arrays/KnuthShufflingAlgorithm.rb

23 lines
325 B
Ruby
Raw Normal View History

2019-11-15 12:59:38 +01:00
#Knuth Shuffling Algorithm(Choose random number between i and n-i and swap)
#Time-complexity: O(n)
def shuffle(arr)
len = arr.length
for i in 0...len
r = Random.rand(len-i)+i
swap(arr,i,r)
end
return arr
end
def swap(arr,i,j)
temp = arr[i]
arr[i] = arr[j]
arr[j] = temp
end
shuffle([1,2,3,4,5,6])