bsc

Comprehensive codebase and cou...
Log | Files | Refs | README | LICENSE

IP-11.c (1115B)


      1 /*
      2  * Author  : Amit Dutta <amitdutta4255@gmail.com>
      3  * Date    : 03 Jan 2026
      4  * Repo    : https://github.com/notamitgamer/bsc
      5  * License : MIT License (See the LICENSE file for details)
      6  */
      7 
      8 /* Write a program to merge two sorted integer arrays to form a single sorted array. */
      9 
     10 #include <stdio.h>
     11 #include <stdlib.h>
     12 
     13 void merge(int[], int, int[], int);
     14 
     15 int main()
     16 {
     17     int a[] = {10, 30, 50, 70, 90};
     18     int b[] = {20, 40, 60, 80, 100};
     19     int n1 = sizeof(a) / sizeof(a[0]);
     20     int n2 = sizeof(b) / sizeof(b[0]);
     21     merge(a, n1, b, n2);
     22     return 0;
     23 }
     24 
     25 void merge(int a[], int n1, int b[], int n2)
     26 {
     27     int n = n1 + n2;
     28     int *c = (int *)malloc(n * sizeof(int));
     29     int i, j, k;
     30     i = j = k = 0;
     31     while (i < n1 && j < n2)
     32     {
     33         if (a[i] < b[j])
     34         {
     35             c[k++] = a[i++];
     36         }
     37         else
     38         {
     39             c[k++] = b[j++];
     40         }
     41     }
     42     while (i < n1)
     43     {
     44         c[k++] = a[i++];
     45     }
     46     while (j < n2)
     47     {
     48         c[k++] = b[j++];
     49     }
     50     printf("Merged Array:");
     51     for (i = 0; i < n; i++)
     52     {
     53         printf("  %d", c[i]);
     54     }
     55     free(c);
     56 }
© notamitgamer • Site Built: 2026-07-21 13:58:23 UTC • git-mirror commit: 1037f62 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror