0
0
DSA Cprogramming

Merge Sort as Divide and Conquer in DSA C

Choose your learning style9 modes available
Mental Model
Split the list into smaller parts, sort each part, then join them back in order.
Analogy: Imagine sorting a big pile of cards by splitting them into smaller piles, sorting each small pile, then carefully merging the piles back together in order.
Original array:
[38, 27, 43, 3, 9, 82, 10]

Split into halves:
[38, 27, 43]   [3, 9, 82, 10]

Split again:
[38] [27, 43]   [3, 9] [82, 10]

Split until single elements:
[38] [27] [43]   [3] [9] [82] [10]
Dry Run Walkthrough
Input: array: [38, 27, 43, 3, 9, 82, 10]
Goal: Sort the array in ascending order using merge sort
Step 1: Split array into two halves: left=[38,27,43], right=[3,9,82,10]
[38, 27, 43]   [3, 9, 82, 10]
Why: Divide the problem into smaller parts to sort separately
Step 2: Split left half into [38] and [27, 43]
[38]   [27, 43]   [3, 9, 82, 10]
Why: Keep dividing until subarrays have one element
Step 3: Split [27, 43] into [27] and [43]
[38]   [27]   [43]   [3, 9, 82, 10]
Why: Single elements are sorted by definition
Step 4: Merge [27] and [43] into sorted [27, 43]
[38]   [27, 43]   [3, 9, 82, 10]
Why: Combine sorted parts to build bigger sorted arrays
Step 5: Merge [38] and [27, 43] into sorted [27, 38, 43]
[27, 38, 43]   [3, 9, 82, 10]
Why: Merge sorted left half completely
Step 6: Split right half into [3, 9] and [82, 10]
[27, 38, 43]   [3, 9]   [82, 10]
Why: Divide right half to smaller parts
Step 7: Split [3, 9] into [3] and [9]
[27, 38, 43]   [3]   [9]   [82, 10]
Why: Single elements are sorted
Step 8: Merge [3] and [9] into [3, 9]
[27, 38, 43]   [3, 9]   [82, 10]
Why: Merge sorted small parts
Step 9: Split [82, 10] into [82] and [10]
[27, 38, 43]   [3, 9]   [82]   [10]
Why: Divide until single elements
Step 10: Merge [82] and [10] into [10, 82]
[27, 38, 43]   [3, 9]   [10, 82]
Why: Merge sorted small parts
Step 11: Merge [3, 9] and [10, 82] into [3, 9, 10, 82]
[27, 38, 43]   [3, 9, 10, 82]
Why: Merge sorted right half completely
Step 12: Merge [27, 38, 43] and [3, 9, 10, 82] into fully sorted [3, 9, 10, 27, 38, 43, 82]
[3, 9, 10, 27, 38, 43, 82]
Why: Final merge to get fully sorted array
Result:
[3, 9, 10, 27, 38, 43, 82]
Annotated Code
DSA C
#include <stdio.h>
#include <stdlib.h>

void merge(int arr[], int left, int mid, int right) {
    int n1 = mid - left + 1;
    int n2 = right - mid;

    int* L = (int*)malloc(n1 * sizeof(int));
    int* R = (int*)malloc(n2 * sizeof(int));

    for (int i = 0; i < n1; i++)
        L[i] = arr[left + i];
    for (int j = 0; j < n2; j++)
        R[j] = arr[mid + 1 + j];

    int i = 0, j = 0, k = left;
    while (i < n1 && j < n2) {
        if (L[i] <= R[j]) {
            arr[k] = L[i];
            i++;
        } else {
            arr[k] = R[j];
            j++;
        }
        k++;
    }

    while (i < n1) {
        arr[k] = L[i];
        i++;
        k++;
    }

    while (j < n2) {
        arr[k] = R[j];
        j++;
        k++;
    }

    free(L);
    free(R);
}

void mergeSort(int arr[], int left, int right) {
    if (left < right) {
        int mid = left + (right - left) / 2;
        mergeSort(arr, left, mid); // sort left half
        mergeSort(arr, mid + 1, right); // sort right half
        merge(arr, left, mid, right); // merge sorted halves
    }
}

void printArray(int arr[], int size) {
    for (int i = 0; i < size; i++) {
        printf("%d", arr[i]);
        if (i < size - 1) printf(", ");
    }
    printf("\n");
}

int main() {
    int arr[] = {38, 27, 43, 3, 9, 82, 10};
    int size = sizeof(arr) / sizeof(arr[0]);

    printf("Original array:\n");
    printArray(arr, size);

    mergeSort(arr, 0, size - 1);

    printf("Sorted array:\n");
    printArray(arr, size);

    return 0;
}
if (left < right) {
check if subarray has more than one element to continue dividing
int mid = left + (right - left) / 2;
find middle index to split array
mergeSort(arr, left, mid); // sort left half
recursively sort left half
mergeSort(arr, mid + 1, right); // sort right half
recursively sort right half
merge(arr, left, mid, right); // merge sorted halves
combine two sorted halves into one sorted array
while (i < n1 && j < n2) {
merge elements by comparing left and right subarrays
while (i < n1) {
copy remaining elements from left subarray if any
while (j < n2) {
copy remaining elements from right subarray if any
OutputSuccess
Original array: 38, 27, 43, 3, 9, 82, 10 Sorted array: 3, 9, 10, 27, 38, 43, 82
Complexity Analysis
Time: O(n log n) because the array is repeatedly split in half (log n splits) and merging takes O(n) at each level
Space: O(n) because temporary arrays are used to merge subarrays
vs Alternative: Compared to simple sorting like bubble sort O(n^2), merge sort is much faster for large arrays due to divide and conquer
Edge Cases
empty array
function returns immediately without changes
DSA C
if (left < right) {
array with one element
function returns immediately as single element is already sorted
DSA C
if (left < right) {
array with duplicate elements
duplicates are handled correctly and appear in sorted order
DSA C
if (L[i] <= R[j]) {
When to Use This Pattern
When you see a problem asking to sort or organize data efficiently by breaking it into smaller parts, reach for merge sort because it uses divide and conquer to sort in O(n log n) time.
Common Mistakes
Mistake: Not checking the base case properly causing infinite recursion
Fix: Add condition if (left < right) to stop recursion when subarray has one or zero elements
Mistake: Merging without copying remaining elements from left or right subarray
Fix: Add loops to copy leftover elements after main merge loop
Mistake: Calculating mid as (left + right) / 2 without handling large indices
Fix: Calculate mid as left + (right - left) / 2 to avoid overflow
Summary
Merge sort splits the array into smaller parts, sorts them, and merges back to get a sorted array.
Use merge sort when you want a reliable, efficient sorting method especially for large data.
The key insight is dividing the problem into smaller sorted pieces and merging them carefully.