Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******************************************************************************
- Online C++ Compiler.
- Code, Compile, Run and Debug C++ program online.
- Write your code in this editor and press "Run" button to compile and execute it.
- *******************************************************************************/
- #include <iostream>
- #include <fstream>
- #include <cstdlib>
- #include <bits/stdc++.h>
- using namespace std;
- // Function to merge array in sorted order
- void sortedMerge(int a[], int b[], int res[],
- int n, int m)
- {
- // Concatenate two arrays
- int i = 0, j = 0, k = 0;
- while (i < n) {
- res[k] = a[i];
- i += 1;
- k += 1;
- }
- while (j < m) {
- res[k] = b[j];
- j += 1;
- k += 1;
- }
- // sorting the res array
- sort(res, res + n + m);
- }
- // Driver code
- int main()
- {
- int a[] = { 25, 10, 5, 15 };
- int b[] = { 20, 3, 2, 12 };
- int n = sizeof(a) / sizeof(a[0]);
- int m = sizeof(b) / sizeof(b[0]);
- // Final merge list
- int res[n + m];
- sortedMerge(a, b, res, n, m);
- cout << "Sorted merged list :";
- for (int i = 0; i < n + m; i++)
- cout << " " << res[i];
- cout << "";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement