Professional Documents
Culture Documents
Insertion Sort
Insertion Sort
05
def insertionSort(a, n)
for i in 1..n-1 do
v = a[i]
j = i-1
while j >= 0 && a[j] > v do
a[j+1] = a[j]
j-=1
end
a[j+1] = v
end
return a
end
a=[5,2,4,6,1,3]
n=6
a
ruby insertionsort.rb
[1, 2, 3, 4, 5, 6]