Created
April 15, 2015 23:09
-
-
Save emaraschio/92c0a5a5391d10c17aeb to your computer and use it in GitHub Desktop.
Array sort with 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 new_sort(left_sorted, right_sorted) | |
ordered_array = [] | |
left = 0 | |
right = 0 | |
loop do | |
break if right >= right_sorted.length and left >= left_sorted.length | |
if right >= right_sorted.length or (left < left_sorted.length and left_sorted[left] < right_sorted[right]) | |
ordered_array << left_sorted[left] | |
left += 1 | |
else | |
ordered_array << right_sorted[right] | |
right += 1 | |
end | |
end | |
ordered_array | |
end | |
ary1 = [1,3,5,7] | |
ary2 = [2,4,6,8] | |
puts new_sort(ary1, ary2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment