### Merging two sorted arrays

• Merging 2 sorted arrays

• Problem description:

• You are given two sorted arrays

Example:

 ``` a = [2.5, 3.5, 6.4, 7.5] b = [1.1, 8.9, 9.2] ```

• Design an efficient algorithm to combine the 2 arrays into one single sorted array

• Solution:

• Make i and j point to the first element in each of the arrays.

• Compare a[i] and b[j]

 ``` i=0 j=0 | | V V [2.5, 3.5, 6.4, 7.5] [1.1, 8.9, 9.2] Result: [ ] ```

Copy the smallest value to the result array and shift the index belonging to the smallest value:

 ``` i=0 j=1 | | V V [2.5, 3.5, 6.4, 7.5] [1.1, 8.9, 9.2] Result: [1.1] ```

• Compare a[i] and b[j]

 ``` i=0 j=1 | | V V [2.5, 3.5, 6.4, 7.5] [1.1, 8.9, 9.2] Result: [1.1] ```

Copy the smallest value to the result array and shift the index belonging to the smallest value:

 ``` i=1 j=1 | | V V [2.5, 3.5, 6.4, 7.5] [1.1, 8.9, 9.2] Result: [1.1, 2.5] ```

• Compare a[i] and b[j]

 ``` i=1 j=1 | | V V [2.5, 3.5, 6.4, 7.5] [1.1, 8.9, 9.2] Result: [1.1, 2.5] ```

Copy the smallest value to the result array and shift the index belonging to the smallest value:

 ``` i=2 j=1 | | V V [2.5, 3.5, 6.4, 7.5] [1.1, 8.9, 9.2] Result: [1.1, 2.5, 3.5] ```

• Compare a[i] and b[j]

 ``` i=2 j=1 | | V V [2.5, 3.5, 6.4, 7.5] [1.1, 8.9, 9.2] Result: [1.1, 2.5, 3.5] ```

Copy the smallest value to the result array and shift the index belonging to the smallest value:

 ``` i=2 j=1 | | V V [2.5, 3.5, 6.4, 7.5] [1.1, 8.9, 9.2] Result: [1.1, 2.5, 3.5, 6.4] ```

• Compare a[i] and b[j]

 ``` i=2 j=1 | | V V [2.5, 3.5, 6.4, 7.5] [1.1, 8.9, 9.2] Result: [1.1, 2.5, 3.5, 6.4] ```

Copy the smallest value to the result array and shift the index belonging to the smallest value:

 ``` i=3 !! j=1 | | V V [2.5, 3.5, 6.4, 7.5] [1.1, 8.9, 9.2] Result: [1.1, 2.5, 3.5, 6.4, 7.5] ```

• Notice that:

 The first array has become exhausted !!!

• Caveat:

• If one of the input arrays becomes exhausted, you must not perform a compare operation.

 If one of the input arrays becomes exhausted, you should copy values from the other array over without performing a compare operation !!!

• The array merge algorithm

• Given:

 Two sorted arrays a and b An output array result of the size a.length + b.length

 ``` Merge( result[], a[], b[] ) { k = 0; // k = output location in result[] for ( every element in a[] and b[] ) { let a[i] be the first unprocessed element in a[] let b[j] be the first unprocessed element in a[] if ( a[] and b[] are not exhausted ) { if ( a[i] < b[j] ) result[k++] = a[i++]; // Copy smallest over and shift else result[k++] = b[j++]; // Copy smallest over and shift } else if ( a[] is exhausted ) result[k++] = b[j++]; // Copy over and shift else if ( b[] is exhausted ) result[k++] = a[j++]; // Copy over and shift } ```
 ``` public static void merge(double[] result, double[] a, double[] b) { int i, j, k; i = j = k = 0; while ( i < a.length || j < b.length ) { if ( i < a.length && j < b.length ) { // Both array have elements if ( a[i] < b[j] ) result[k++] = a[i++]; else result[k++] = b[j++]; } else if ( i == a.length ) result[k++] = b[j++]; // a is empty else if ( j == b.length ) result[k++] = a[i++]; // b is empty } } ```