def my_sort(arr)
for j in 1...arr.length
key = arr[j]
i = j-1
while i>=0 and arr[i] > key
arr[i+1] = arr[i]
i = i-1
end
arr[i+1] = key
end
p arr
end
my_sort([3,2,1,5,7])
for j in 1...arr.length
key = arr[j]
i = j-1
while i>=0 and arr[i] > key
arr[i+1] = arr[i]
i = i-1
end
arr[i+1] = key
end
p arr
end
my_sort([3,2,1,5,7])
3 comments:
Wow! It's really fantastic sharing on Ruby on Rails Development because it will be good for newbies.
Or [3,2,1,5,7].sort
Seriously, why make it so hard for yourself. Just use C instead.
Sorting array without using any built in function on Ruby. To understand how sort will work
Post a Comment