Skip to content

Instantly share code, notes, and snippets.

@tomelm
Created September 24, 2012 13:44
Show Gist options
  • Save tomelm/3776030 to your computer and use it in GitHub Desktop.
Save tomelm/3776030 to your computer and use it in GitHub Desktop.
Ruby version of Jesse's n way merge from Java
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