Support us


to write
more tutorials




to create new
visualizers




to keep sharing
free knowledge
for you


every dollar helps
Explore the English language on a new scale using AI-powered English language navigator.

Algorithm to merge sorted arrays

In the article we present an algorithm for merging two sorted arrays. One can learn how to operate with several arrays and master read/write indices. Also, the algorithm has certain applications in practice, for instance in merge sort.

Merge algorithm

Assume, that both arrays are sorted in ascending order and we want resulting array to maintain the same order. Algorithm to merge two arrays A[0..m-1] and B[0..n-1] into an array C[0..m+n-1] is as following:

  1. Introduce read-indices i, j to traverse arrays A and B, accordingly. Introduce write-index k to store position of the first free cell in the resulting array. By default i = j = k = 0.
  2. At each step: if both indices are in range (i < m and j < n), choose minimum of (A[i], B[j]) and write it to C[k]. Otherwise go to step 4.
  3. Increase k and index of the array, algorithm located minimal value at, by one. Repeat step 2.
  4. Copy the rest values from the array, which index is still in range, to the resulting array.

Enhancements

Algorithm could be enhanced in many ways. For instance, it is reasonable to check, if A[m - 1] < B[0] or B[n - 1] < A[0]. In any of those cases, there is no need to do more comparisons. Algorithm could just copy source arrays in the resulting one in the right order. More complicated enhancements may include searching for interleaving parts and run merge algorithm for them only. It could save up much time, when sizes of merged arrays differ in scores of times.

Complexity analysis

Merge algorithm's time complexity is O(n + m). Additionally, it requires O(n + m) additional space to store resulting array.

Code snippets

Java implementation

// size of C array must be equal or greater than

// sum of A and B arrays' sizes

public void merge(int[] A, int[] B, int[] C) {

      int i, j, k, m, n;

      i = 0;

      j = 0;

      k = 0;

      m = A.length;

      n = B.length;

      while (i < m && j < n) {

            if (A[i] <= B[j]) {

                  C[k] = A[i];

                  i++;

            } else {

                  C[k] = B[j];

                  j++;

            }

            k++;

      }

      if (i < m) {

            for (int p = i; p < m; p++) {

                  C[k] = A[p];

                  k++;

            }

      } else {

            for (int p = j; p < n; p++) {

                  C[k] = B[p];

                  k++;

            }

      }

}

C++ implementation

// m - size of A

// n - size of B

// size of C array must be equal or greater than

// m + n

void merge(int m, int n, int A[], int B[], int C[]) {

      int i, j, k;

      i = 0;

      j = 0;

      k = 0;

      while (i < m && j < n) {

            if (A[i] <= B[j]) {

                  C[k] = A[i];

                  i++;

            } else {

                  C[k] = B[j];

                  j++;

            }

            k++;

      }

      if (i < m) {

            for (int p = i; p < m; p++) {

                  C[k] = A[p];

                  k++;

            }

      } else {

            for (int p = j; p < n; p++) {

                  C[k] = B[p];

                  k++;

            }

      }

}

Contribute to AlgoList

Liked this tutorial? Please, consider making a donation. Contribute to help us keep sharing free knowledge and write new tutorials.


Every dollar helps!

Leave a reply

Your name (optional):
Your e-mail (optional):
Message: