Amazon Affiliate

Monday 20 June 2016

Insertion Sort in ruby.

# Insertion Sort in ruby.

def insertion_sort(array)
for i in 1..array.length - 1
j = i
temp = array[i]
while ((j>0) and (array[j-1] > temp))
array[j] = array[j-1]
j -= 1
end
array[j] = temp
end
return array
end

array = [6,5,3,1,8,7,2,4]
insertion_sort array
print array

No comments:

Post a Comment