Created
September 24, 2012 13:44
-
-
Save tomelm/3776030 to your computer and use it in GitHub Desktop.
Ruby version of Jesse's n way merge from Java
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 n_way_merge(arrays, chunks, chunk_size, new_array) | |
indices = [] | |
(0..chunk_size-1).each_with_index do |n,i| | |
min = (1..chunks).select { |j| | |
indices[j] < chunk_size && arrays[chunk_size * j + indices[j]] < arrays[chunk_size * min + indices[min]] | |
}.first | |
new_array[i] = arrays[chunk_size * min + indices[min]] | |
indices[min] += 1 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment