Extra 5% OFF Use Code: OL05
Free Shipping over ₹999

Merge two sorted array

In this example, we merge two sorted array

#include <iostream>
#include <bits/stdc++.h>
using namespace std;


void mergeArray(int arr1[], int arr2[], int size_arr1,int size_arr2, int arr3[]){
    int i=0, j=0, k=0;
    while(i < size_arr1){
        arr3[k++] = arr1[i++];
    }
    while(j < size_arr2){
        arr3[k++] = arr2[j++];
    }

    sort(arr3,arr3+size_arr1+size_arr2);
}
int main(){
    int arr1[] = {1,2,5,6,8};
    int size_arr1 = sizeof(arr1) / sizeof(int);
    int arr2[] = {9,7,3,4};
    int size_arr2 = sizeof(arr2) / sizeof(int);

    int arr3[size_arr1+size_arr2];

    mergeArray(arr1,arr2,size_arr1,size_arr2,arr3);

    cout<<"Array after merge"<<endl;
    for(int i=0; i < size_arr1+size_arr2; i++){
        cout<<arr3[i]<<" ";
    }
    return 0;
}

Output:

Array after merge:
1 2 3 4 5 6 7 8 9

Explanation:

Step 1: Including Libraries

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
  • #include <iostream>: Includes the input-output stream, which is required for cout.
  • #include <bits/stdc++.h>: Includes all standard libraries (often used in competitive programming for convenience). In this case, it’s primarily used to access the sort function.

Step 2: Function to Merge Two Arrays

void margeArray(int arr1[], int arr2[], int size_arr1, int size_arr2, int arr3[]) {
    int i = 0, j = 0, k = 0;
    
    // Copy elements from arr1 to arr3
    while(i < size_arr1) {
        arr3[k++] = arr1[i++];
    }
    
    // Copy elements from arr2 to arr3
    while(j < size_arr2) {
        arr3[k++] = arr2[j++];
    }

    // Sort the merged array arr3
    sort(arr3, arr3 + size_arr1 + size_arr2);
}

Parameters:

  • arr1[]: First array to merge.
  • arr2[]: Second array to merge.
  • size_arr1: Size of the first array.
  • size_arr2: Size of the second array.
  • arr3[]: The array that will store the merged result.

Copying Elements from arr1 to arr3:

while(i < size_arr1) {
    arr3[k++] = arr1[i++];
}
  • This loop copies each element from arr1 into arr3. After each copy, both i (index for arr1) and k (index for arr3) are incremented.

Copying Elements from arr2 to arr3:

while(j < size_arr2) {
    arr3[k++] = arr2[j++];
}
  • This loop copies each element from arr2 into arr3 after the elements from arr1 have been copied.

Sorting the Merged Array:

sort(arr3, arr3 + size_arr1 + size_arr2);
  • The sort function sorts the entire arr3 from arr3[0] to arr3[size_arr1 + size_arr2] in ascending order.

Step 3: Main Function

int main() {
    int arr1[] = {1, 2, 5, 6, 8}; // First array
    int size_arr1 = sizeof(arr1) / sizeof(int); // Size of arr1

    int arr2[] = {9, 7, 3, 4}; // Second array
    int size_arr2 = sizeof(arr2) / sizeof(int); // Size of arr2

    int arr3[size_arr1 + size_arr2]; // Merged array with size = size_arr1 + size_arr2

    // Merging arr1 and arr2 into arr3 and sorting
    margeArray(arr1, arr2, size_arr1, size_arr2, arr3);

    // Printing the merged and sorted array
    cout << "Array after merge:" << endl;
    for(int i = 0; i < size_arr1 + size_arr2; i++) {
        cout << arr3[i] << " ";
    }

    return 0;
}

Array Declaration:

  • arr1[] = {1, 2, 5, 6, 8}: The first array.
  • arr2[] = {9, 7, 3, 4}: The second array.

Array Sizes:

int size_arr1 = sizeof(arr1) / sizeof(int);
int size_arr2 = sizeof(arr2) / sizeof(int);
  • These lines calculate the size of each array by dividing the total memory size of the array by the size of an integer.

Merged Array:

int arr3[size_arr1 + size_arr2];
  • This array arr3 is declared with the combined size of arr1 and arr2, so it can hold all the merged elements.

Merging and Sorting:

margeArray(arr1, arr2, size_arr1, size_arr2, arr3);
  • The mergeArray function is called to merge arr1 and arr2 into arr3, and it automatically sorts arr3 after merging.

Printing the Merged Array:

for(int i = 0; i < size_arr1 + size_arr2; i++) {
    cout << arr3[i] << " ";
}
  • This loop prints the elements of the merged and sorted array arr3 .

    Leave a Reply

    Your email address will not be published.

    Need Help?