Created
December 17, 2014 19:20
-
-
Save esse/417d958fcb5d617941a7 to your computer and use it in GitHub Desktop.
merge ruby
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
def merge(a,b) | |
merged_array = [] | |
j = 0 | |
k = 0 | |
while j < a.size || k < b.size | |
if a[j] && b[k] && a[j] <= b[k] | |
merged_array << a[j] | |
j+= 1 | |
elsif b[k] | |
merged_array << b[k] | |
k += 1 | |
else | |
merged_array << a[j] | |
j += 1 | |
end | |
end | |
return merged_array | |
end | |
p merge([1,3,5],[2,4,6,8,9]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment