Skip to content

Instantly share code, notes, and snippets.

@emaraschio
Created April 15, 2015 23:09
Show Gist options
  • Save emaraschio/92c0a5a5391d10c17aeb to your computer and use it in GitHub Desktop.
Save emaraschio/92c0a5a5391d10c17aeb to your computer and use it in GitHub Desktop.
Array sort with Ruby
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