array merging algorithm