Skip to content

Instantly share code, notes, and snippets.

@takei-shg
Created May 10, 2014 07:56
Show Gist options
  • Save takei-shg/ba867f4148250919ea11 to your computer and use it in GitHub Desktop.
Save takei-shg/ba867f4148250919ea11 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
def replace(index, list)
if (index == 0)
return list
end
if (list[index-1] < list[index])
return list
end
x = list[index-1]
list[index-1] = list[index]
list[index] = x
replace(index-1, list)
end
def insert(sorted, elem)
appended = sorted << elem
elem_index = appended.length - 1
return replace(elem_index, appended)
end
# orig = [5,1,10,49,23,44,2,4,3]
# orig = []
orig = [1]
p orig
result = []
for i in 0..orig.length-1
result = insert(result, orig[i])
end
p result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment