Merge two sorted arrays into a third array using pointers

Merge two sorted arrays into a third array using pointers

Simplified through Visualization...

How we will really do that..

Let us declare the required variables first

Let us build the logic which we will use to get the above output on screen

Corrected Approach
Place them in order

So finally we have got the desired output with this logic

But....

We wanted to do it with pointers let us change our approach

.


--


What did we visualized in above iterations

when

*ptr1 < *ptr2        

then only top arr1 element get into merged array ,ptr1 increase by 1 and size of arr1 decrease by 1 element

 *ptrMerged = *ptr1;
            ptr1++;
            size1--;        

otherwise if

*ptr1 > *ptr2
  *ptrMerged = *ptr2;
            ptr2++;
            size2--;
        

After this pointer pointing to merged array gets incremented

All this process goes on till the time all the elements in array arr1 or arr2 get get exhausted so this process can go upto till the time elements in arr1 and arr2 are greater than 0.

 int *ptr1 = arr1;
    int *ptr2 = arr2;
    int *ptrMerged = merged;

    while (size1 > 0 && size2 > 0) {
        if (*ptr1 < *ptr2) {
            *ptrMerged = *ptr1;
            ptr1++;
            size1--;
        } else {
            *ptrMerged = *ptr2;
            ptr2++;
            size2--;
        }
        ptrMerged++;
    }
        

Where will the extra left elements in arr2 go?

We will store them exactly same in same order in merged array...


   while (size2 > 0) {
        *ptrMerged = *ptr2;
        ptr2++;
        ptrMerged++;
        size2--;
    }        

What if we had elements in Arr1 than

    while (size1 > 0) {
        *ptrMerged = *ptr1;
        ptr1++;
        ptrMerged++;
        size1--;
    }        

Therefore our merging function will be

void mergeSortedArrays(int arr1[], int size1, int arr2[], int size2, int merged[]) {
    int *ptr1 = arr1;
    int *ptr2 = arr2;
    int *ptrMerged = merged;

    while (size1 > 0 && size2 > 0) {
        if (*ptr1 < *ptr2) {
            *ptrMerged = *ptr1;
            ptr1++;
            size1--;
        } else {
            *ptrMerged = *ptr2;
            ptr2++;
            size2--;
        }
        ptrMerged++;
    }

    // If elements are left in arr1, copy them to merged
    while (size1 > 0) {
        *ptrMerged = *ptr1;
        ptr1++;
        ptrMerged++;
        size1--;
    }

    // If elements are left in arr2, copy them to merged
    while (size2 > 0) {
        *ptrMerged = *ptr2;
        ptr2++;
        ptrMerged++;
        size2--;
    }
}        

Let us now setup a function in main and print the values of 3rd sorted array

int main() {
    int arr1[] = {1, 3, 5};
    int arr2[] = {2, 4, 6};
    int size1 = sizeof(arr1) / sizeof(arr1[0]);
    int size2 = sizeof(arr2) / sizeof(arr2[0]);
    int merged[size1 + size2];

    mergeSortedArrays(arr1, size1, arr2, size2, merged);

    printf("Merged Array: ");
    for (int i = 0; i < size1 + size2; i++) {
        printf("%d ", merged[i]);
    }

    return 0;
}        
Balemarthy Vamsi Krishna

Career Coach | Get noticed at work | LinkedIn, GenAI4Career| Salary hike | Establish thought leadership | Interview & appraisal | Resume writing | Build LinkedIn profile | Networking | Job Search

1 年

I appreciate your time taking to create all this effort of creating images and content

要查看或添加评论,请登录

Anoushka Tripathi的更多文章

社区洞察

其他会员也浏览了