PHP Program for Merge Sort with Example and Explanation
mergeSort() function that splits the array into halves recursively and merges them in sorted order using a merge() helper function.Examples
How to Think About It
Algorithm
Code
<?php function merge(array $left, array $right): array { $result = []; while (count($left) > 0 && count($right) > 0) { if ($left[0] <= $right[0]) { $result[] = array_shift($left); } else { $result[] = array_shift($right); } } return array_merge($result, $left, $right); } function mergeSort(array $arr): array { if (count($arr) <= 1) { return $arr; } $mid = intdiv(count($arr), 2); $left = array_slice($arr, 0, $mid); $right = array_slice($arr, $mid); $left = mergeSort($left); $right = mergeSort($right); return merge($left, $right); } $input = [5, 3, 8, 4, 2]; $output = mergeSort($input); print_r($output);
Dry Run
Let's trace the array [5, 3, 8, 4, 2] through the merge sort code.
Split array
Split [5, 3, 8, 4, 2] into [5, 3] and [8, 4, 2]
Sort left half
Split [5, 3] into [5] and [3], then merge to [3, 5]
Sort right half
Split [8, 4, 2] into [8] and [4, 2], sort [4, 2] into [2, 4], then merge to [2, 4, 8]
Merge halves
Merge [3, 5] and [2, 4, 8] into [2, 3, 4, 5, 8]
| Operation | Array State |
|---|---|
| Initial | [5, 3, 8, 4, 2] |
| Split | [5, 3] and [8, 4, 2] |
| Sort left half | [3, 5] |
| Sort right half | [2, 4, 8] |
| Merge halves | [2, 3, 4, 5, 8] |
Why This Works
Step 1: Splitting the array
The code uses array_slice to split the array into two halves until each sub-array has one or zero elements, which are naturally sorted.
Step 2: Recursive sorting
The mergeSort function calls itself on each half, breaking down the problem into smaller sorting tasks.
Step 3: Merging sorted arrays
The merge function combines two sorted arrays by comparing their first elements and building a new sorted array.
Alternative Approaches
<?php function iterativeMergeSort(array $arr): array { $width = 1; $n = count($arr); while ($width < $n) { for ($i = 0; $i < $n; $i += 2 * $width) { $left = array_slice($arr, $i, $width); $right = array_slice($arr, $i + $width, $width); $merged = merge($left, $right); array_splice($arr, $i, count($merged), $merged); } $width *= 2; } return $arr; } function merge(array $left, array $right): array { $result = []; while (count($left) > 0 && count($right) > 0) { if ($left[0] <= $right[0]) { $result[] = array_shift($left); } else { $result[] = array_shift($right); } } return array_merge($result, $left, $right); } $input = [5, 3, 8, 4, 2]; $output = iterativeMergeSort($input); print_r($output);
<?php $input = [5, 3, 8, 4, 2]; sort($input); print_r($input);
Complexity: O(n log n) time, O(n) space
Time Complexity
Merge sort divides the array into halves recursively (log n splits) and merges them back (n operations per merge), resulting in O(n log n) time.
Space Complexity
Merge sort requires extra space to hold temporary arrays during merging, so it uses O(n) additional memory.
Which Approach is Fastest?
Built-in PHP sort functions are fastest for practical use, but merge sort is useful for learning and stable sorting.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Recursive Merge Sort | O(n log n) | O(n) | Learning algorithm and stable sorting |
| Iterative Merge Sort | O(n log n) | O(n) | Avoiding recursion, complex implementation |
| PHP built-in sort() | O(n log n) | O(1) | Fastest and simplest for real applications |
array_slice to split arrays and array_merge to combine them when implementing merge sort in PHP.