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.

20 lines
409 B
Ruby

#Fitcher-Yates shuffling algorithm
#(start from last element,swap with random element from the array,decrement array size by one and repeat until array size is 1)
#Time-complexity: O(n)
def shuffle(arr)
n=arr.length
for i in (n-1).downto(1)
j=Random.rand(i+1)
swap(arr,i,j)
end
print 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])