Sunday, March 3, 2013

Ruby Sorting without built-in function

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

3 comments:

Unknown said...

Wow! It's really fantastic sharing on Ruby on Rails Development because it will be good for newbies.

Tim said...

Or [3,2,1,5,7].sort

Seriously, why make it so hard for yourself. Just use C instead.

sathishRoR said...

Sorting array without using any built in function on Ruby. To understand how sort will work