Merge Sort

easy

Implement merge sort using divide-and-conquer

Merge Sort

Key Insight

Divide in half recursively, then merge sorted halves back together

Step 1Start: Unsorted Array
Split into halves
38
0
27
1
43
2
3
3
9
4
82
5
10
6

Split the array in half recursively until single elements.

1 / 6

Learn the Pattern

Practice the Code

Step-by-Step Walkthrough: Merge Sort

Divide in half recursively, then merge sorted halves back together

  1. Start: Unsorted Array

    Split the array in half recursively until single elements.

  2. Divide Left Half

    [38, 27, 43] splits into [38] and [27, 43], then [27] and [43].

  3. Merge [27] + [43]

    Compare front elements: 27 < 43. Result: [27, 43].

  4. Merge [38] + [27, 43]

    27 < 38 → take 27. Then 38 < 43 → take 38. Then 43. Result: [27, 38, 43].

  5. Merge Right Half Similarly

    [3, 9, 82, 10] → [3, 9] + [10, 82] → [3, 9, 10, 82].

  6. Final Merge

    Merge [27, 38, 43] + [3, 9, 10, 82]. Compare front elements repeatedly.