Open In App

PHP Program for Merge Sort

Last Updated : 19 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Merge sort is a popular sorting algorithm that employs a divide-and-conquer approach to sort an array in PHP. It divides the array into smaller subarrays, sorts each subarray, and then merges the sorted subarrays back together to form the final sorted array.

How Does Merge Sort Work?

Merge sort is a recursive algorithm that continuously splits the array in half until it cannot be further divided (i.e., the array has only one element left, which is considered sorted). Then, the sorted subarrays are merged into one sorted array. This process is repeated until the entire array is sorted.

Approach

  • The merge function is responsible for merging two sorted subarrays into one sorted array.
  • In this approach, The mergeSort() function recursively divides the array into smaller subarrays, sorts each subarray, and then calls the merge function to merge the sorted subarrays.
  • The printArray() function is used to print the array elements.
  • In the driver code, we define an array $arr, print the original array, call the mergeSort() function to sort the array, and then print the sorted array.

Example: Below is the PHP code for implementing merge sort.

PHP
<?php

// Function to merge two subarrays
function merge(&$arr, $left, $middle, $right) {
    
    $leftArrayLength = $middle - $left + 1;
    $rightArrayLength = $right - $middle;

    // Create temporary arrays
    $leftArray = array_slice($arr, $left, $leftArrayLength);
    $rightArray = array_slice($arr, $middle + 1, $rightArrayLength);

    // Merge the temporary arrays
    // back into the main array
    $i = 0;
    $j = 0;
    $k = $left;
    
    while ($i < $leftArrayLength 
        && $j < $rightArrayLength) {
        if ($leftArray[$i] <= $rightArray[$j]) {
            $arr[$k] = $leftArray[$i];
            $i++;
        } else {
            $arr[$k] = $rightArray[$j];
            $j++;
        }
        $k++;
    }

    // Copy any remaining elements
    // of the left array
    while ($i < $leftArrayLength) {
        $arr[$k] = $leftArray[$i];
        $i++;
        $k++;
    }

    // Copy any remaining elements
    // of the right array
    while ($j < $rightArrayLength) {
        $arr[$k] = $rightArray[$j];
        $j++;
        $k++;
    }
}

// Function to perform merge sort
function mergeSort(&$arr, $left, $right) {
    if ($left < $right) {
        
        // Find the middle point
        $middle = $left + (int)(($right - $left) / 2);

        // Sort the first and second halves
        mergeSort($arr, $left, $middle);
        mergeSort($arr, $middle + 1, $right);

        // Merge the sorted halves
        merge($arr, $left, $middle, $right);
    }
}

// Function to print an array
function printArray($arr) {
    foreach ($arr as $value) {
        echo $value . " ";
    }
    echo "\n";
}

// Driver code
$arr = [38, 27, 43, 10, 12, 11, 13, 5, 6, 7];

echo "Given array is: ";
printArray($arr);

mergeSort($arr, 0, count($arr) - 1);

echo "Sorted array is:";
printArray($arr);

?>

Output
Given array is: 38 27 43 10 12 11 13 5 6 7 
Sorted array is:5 6 7 10 11 12 13 27 38 43 

Time Complexity: O(N log N), where N is the number of elements in the array. Merge sort always divides the array into two halves and takes linear time to merge two halves.

Space Complexity: O(N), as merge sort requires additional space to store the merged subarrays during the sorting process.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads