Created
May 10, 2014 07:56
-
-
Save takei-shg/ba867f4148250919ea11 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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