Merge two sorted arrays Dry Run in C

Merge two sorted arrays is an interactive C dry run visualizer from the One D programs section. Study the source code, then use the execution controls to follow each step, variable update, highlighted line, and console output.

This page provides a browser-based dry run with source-code highlighting, auto-scroll, voice narration controls, and execution output for learning the program step by step.

Merge two sorted arrays Program Code

#include <stdio.h>
int main() {
    int arr1[] = {10, 20, 30, 40};
    int arr2[] = {15, 25, 35, 45, 55};
    int n1 = 4, n2 = 5, n3 = n1+n2;
    int merged[100];
    int i = 0, j = 0, k = 0;
    
    while(i < n1 && j < n2) {
        if(arr1[i] < arr2[j]) {
            merged[k++] = arr1[i++];
        } else {
            merged[k++] = arr2[j++];
        }
    }
    
    while(i < n1) {
        merged[k++] = arr1[i++];
    }
    
    while(j < n2) {
        merged[k++] = arr2[j++];
    }
    
    printf("Merged sorted array: ");
    for(i = 0; i < n3; i++) {
        printf("%d ", merged[i]);
    }
    return 0;
}

View the complete One D programs page.

Program Console Merge two sorted arrays Topic: C Vignaankosh.com
Execution Panel
Step 0/0
Console is empty.