# Sorted merge in one array

Given two sorted arrays, A and B, where A has a large enough buffer at the end to hold B.
Merge B into A in sorted order.

Examples:

```Input : a[] = {10, 12, 13, 14, 18, NA, NA, NA, NA, NA}
b[] = {16, 17, 19, 20, 22};;
Output : a[] = {10, 12, 13, 14, 16, 17, 18, 19, 20, 22}
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

One way is to merge the two arrays by inserting the smaller elements to front of A, but the issue with this approach is that we have to shift every element to right after every insertion.

So, instead from comparing which one is smaller element, we can compare which one is larger and then inserting that element to end of A.

Below is the solution for above approach.

## C++

 `// Merging b[] into a[]  ` `#include ` `using` `namespace` `std; ` `#define NA -1 ` ` `  `void` `sortedMerge(``int` `a[], ``int` `b[], ``int` `n, ``int` `m) { ` `  ``int` `i = n - 1; ` `  ``int` `j = m - 1; ` ` `  `  ``int` `lastIndex = n + m - 1; ` ` `  `  ``/* Merge a and b, starting from last element  ` `     ``in each */` `  ``while` `(j >= 0) { ` ` `  `    ``/* End of a is greater than end of b */` `    ``if` `(i >= 0 && a[i] > b[j]) { ` `      ``a[lastIndex] = a[i]; ``// Copy Element ` `      ``i--; ` `    ``} ``else` `{ ` `      ``a[lastIndex] = b[j]; ``// Copy Element ` `      ``j--; ` `    ``} ` `    ``lastIndex--; ``// Move indices ` `  ``} ` `} ` ` `  `/* Helper function to print the array */` `void` `printArray(``int` `*arr, ``int` `n) { ` `  ``cout << ``"Array A after merging B in sorted"` `        ``" order : "` `<< endl; ` `  ``for` `(``int` `i = 0; i < n; i++) ` `    ``cout << arr[i] << ``" "``; ` `} ` ` `  `int` `main() { ` `  ``int` `a[] = {10, 12, 13, 14, 18, NA, NA, NA, NA, NA}; ` `  ``int` `n = 5; ` `  ``int` `size_a = 10; ` `  ``int` `b[] = {16, 17, 19, 20, 22}; ` `  ``int` `m = 5; ` `  ``sortedMerge(a, b, n, m); ` `  ``printArray(a, size_a); ` `  ``return` `0; ` `} `

## Java

 `// Java  program to merge B  ` `// into A in sorted order. ` `import` `java.io.*; ` ` `  `class` `GFG  ` `{ ` `    ``static` `int` `NA =-``1``; ` `    ``static` `void` `sortedMerge(``int` `a[], ``int` `b[], ``int` `n, ``int` `m)  ` `    ``{ ` `        ``int` `i = n - ``1``; ` `        ``int` `j = m - ``1``; ` `         `  `        ``int` `lastIndex = n + m - ``1``; ` `         `  `        ``// Merge a and b, starting  ` `        ``// from last element in each  ` `        ``while` `(j >= ``0``)  ` `        ``{ ` `         `  `            ``/* End of a is greater than end of b */` `            ``if` `(i >= ``0` `&& a[i] > b[j])  ` `            ``{ ` `                ``// Copy Element ` `                ``a[lastIndex] = a[i];  ` `                ``i--; ` `            ``} ``else`  `            ``{   ` `                ``// Copy Element ` `                ``a[lastIndex] = b[j];  ` `                ``j--; ` `            ``} ` `            ``// Move indices ` `            ``lastIndex--;  ` `        ``} ` `    ``} ` `     `  `    ``// Helper function to print the array  ` `    ``static` `void` `printArray(``int` `arr[], ``int` `n)  ` `    ``{ ` `        ``System.out.println ( ``"Array A after merging B in sorted order : "` `) ; ` `        ``for` `(``int` `i = ``0``; i < n; i++) ` `            ``System.out.print(arr[i] +``" "``); ` `    ``} ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `main (String[] args)  ` `    ``{ ` `        ``int` `a[] = {``10``, ``12``, ``13``, ``14``, ``18``, NA, NA, NA, NA, NA}; ` `        ``int` `n = ``5``; ` `        ``int` `size_a = ``10``; ` `        ``int` `b[] = {``16``, ``17``, ``19``, ``20``, ``22``}; ` `        ``int` `m = ``5``; ` `        ``sortedMerge(a, b, n, m); ` `        ``printArray(a, size_a); ` `    ``} ` `} ` ` `  `// This code is contributed by vt_m. `

## Python3

 `# Python3 program to merge B  ` `# into A in sorted order. ` ` `  `NA ``=` `-``1` ` `  `# Merging b[] into a[] ` `def` `sortedMerge(a, b, n, m): ` ` `  `    ``i ``=` `n ``-` `1` `    ``j ``=` `m ``-` `1` `     `  `    ``lastIndex ``=` `n ``+` `m ``-` `1` `     `  `    ``# Merge a and b, starting from last ` `    ``# element in each ` `    ``while` `(j >``=` `0``) : ` `     `  `        ``# End of a is greater than end  ` `        ``# of b ` `        ``if` `(i >``=` `0` `and` `a[i] > b[j]): ` `             `  `            ``# Copy Element ` `            ``a[lastIndex] ``=` `a[i]  ` `            ``i ``-``=` `1` `        ``else``: ` `             `  `             ``# Copy Element ` `            ``a[lastIndex] ``=` `b[j] ` `            ``j ``-``=` `1` `         `  `        ``# Move indices ` `        ``lastIndex``-``=` `1`  ` `  `# Helper function to print  ` `# the array ` `def` `printArray(arr, n): ` ` `  `    ``print``(``"Array A after merging B in sorted order : "``) ` ` `  `    ``for` `i ``in` `range``(``0``, n): ` `        ``print``(arr[i], end ``=``" "``) ` ` `  `size_a ``=` `10` ` `  `a ``=` `[``10``, ``12``, ``13``, ``14``, ``18``, NA, NA, NA, NA, NA] ` `n ``=` `5` ` `  `b ``=` `[``16``, ``17``, ``19``, ``20``, ``22``] ` `m ``=` `5` ` `  `sortedMerge(a, b, n, m) ` `printArray(a, size_a) ` ` `  `# This code is contributed by ` `# Smitha Dinesh Semwal `

## C#

 `// C# program to merge B into A in  ` `// sorted order. ` `using` `System; ` ` `  `class` `GFG { ` `     `  `    ``static` `int` `NA =-1; ` `    ``static` `void` `sortedMerge(``int` `[]a, ``int` `[]b, ` `                                 ``int` `n, ``int` `m)  ` `    ``{ ` `        ``int` `i = n - 1; ` `        ``int` `j = m - 1; ` `         `  `        ``int` `lastIndex = n + m - 1; ` `         `  `        ``// Merge a and b, starting  ` `        ``// from last element in each  ` `        ``while` `(j >= 0)  ` `        ``{ ` `         `  `            ``/* End of a is greater than  ` `            ``end of b */` `            ``if` `(i >= 0 && a[i] > b[j])  ` `            ``{ ` `                 `  `                ``// Copy Element ` `                ``a[lastIndex] = a[i];  ` `                ``i--; ` `            ``}  ` `             `  `            ``else` `            ``{  ` `                 `  `                ``// Copy Element ` `                ``a[lastIndex] = b[j];  ` `                ``j--; ` `            ``} ` `             `  `            ``// Move indices ` `            ``lastIndex--;  ` `        ``} ` `    ``} ` `     `  `    ``// Helper function to print the array  ` `    ``static` `void` `printArray(``int` `[]arr, ``int` `n)  ` `    ``{ ` `         `  `        ``Console.WriteLine ( ``"Array A after "` `        ``+ ``"merging B in sorted order : "` `) ; ` `         `  `        ``for` `(``int` `i = 0; i < n; i++) ` `            ``Console.Write(arr[i] +``" "``); ` `    ``} ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `Main ()  ` `    ``{ ` `        ``int` `[]a = {10, 12, 13, 14, 18, NA,  ` `                           ``NA, NA, NA, NA}; ` `        ``int` `n = 5; ` `        ``int` `size_a = 10; ` `        ``int` `[]b = {16, 17, 19, 20, 22}; ` `        ``int` `m = 5; ` `         `  `        ``sortedMerge(a, b, n, m); ` `         `  `        ``printArray(a, size_a); ` `    ``} ` `} ` ` `  `// This code is contributed by vt_m. `

## PHP

 `= 0)  ` `    ``{ ` `     `  `        ``/* End of a is greater than end of b */` `        ``if` `(``\$i` `>= 0 && ``\$a``[``\$i``] > ``\$b``[``\$j``])  ` `        ``{ ` `            ``\$a``[``\$lastIndex``] = ``\$a``[``\$i``]; ``// Copy Element ` `            ``\$i``--; ` `        ``}  ` `        ``else`  `        ``{ ` `            ``\$a``[``\$lastIndex``] = ``\$b``[``\$j``]; ``// Copy Element ` `            ``\$j``--; ` `        ``} ` `        ``\$lastIndex``--; ``// Move indices ` `    ``} ` `    ``return` `\$a``; ` `} ` ` `  `/* Helper function to print the array */` `function` `printArray(``\$arr``, ``\$n``)  ` `{ ` `    ``echo` `"Array A after merging B in sorted"``; ` `        ``echo` `" order : \n"``; ` `    ``for` `(``\$i` `= 0; ``\$i` `< ``\$n``; ``\$i``++) ` `        ``echo` `\$arr``[``\$i``] . ``" "``; ` `} ` ` `  `// Driver Code ` `\$a` `= ``array``(10, 12, 13, 14, 18,  ` `          ``-1, -1, -1, -1, -1); ` `\$n` `= 5; ` `\$size_a` `= 10; ` ` `  `\$b` `= ``array``(16, 17, 19, 20, 22); ` `\$m` `= 5; ` `\$c` `= sortedMerge(``\$a``, ``\$b``, ``\$n``, ``\$m``); ` `printArray(``\$c``, ``\$size_a``); ` ` `  `// This code is contributed by Rajput-Ji. ` `?> `

Output:

```Array A after merging B in sorted order :
10 12 13 14 16 17 18 19 20 22
```

Time complexity is O(m+n).

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : Rajput-Ji

Article Tags :
Practice Tags :

2

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.