You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

23 lines
325 B
Ruby

#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])