PHP array quick sort vs. merge sort

WBOY
Release: 2024-04-26 12:45:02
Original
1098 people have browsed it

Quick sort is a recursive algorithm that divides the array into smaller elements and larger elements and sorts them recursively, while merge sort recursively divides the array into smaller arrays, sorts each small array, and then merges it. Return the original array. The codes implemented by PHP are: Quick sort: Divide the array into elements smaller than and larger than the baseline value, and then sort each part recursively. Merge sort: Recursively divide an array into smaller arrays, sort each smaller array, and then merge the sorted smaller arrays back into the original array.

PHP 数组快排 vs. 归并排序

PHP array quick sort vs. merge sort

#What are quick sort and merge sort?

Quick sort and merge sort are both common algorithms used to sort arrays.

  • Quick sort: Divide the array into two parts, one containing smaller elements and the other containing larger elements, and then sort each part recursively.
  • Merge sort: Recursively divide the array into smaller arrays, sort each smaller array, and then merge the sorted smaller arrays back into the original array.

Code implementation

The following are quick sort and merge sort functions implemented in PHP:

Quick sort:

function quickSort($arr) {
    if (count($arr) <= 1) {
        return $arr;
    }
    $pivot = $arr[0];
    $left = [];
    $right = [];
    for ($i = 1; $i < count($arr); $i++) {
        if ($arr[$i] < $pivot) {
            $left[] = $arr[$i];
        } else {
            $right[] = $arr[$i];
        }
    }
    return array_merge(quickSort($left), [$pivot], quickSort($right));
}
Copy after login

Merge sort:

function mergeSort($arr) {
    $length = count($arr);
    if ($length <= 1) {
        return $arr;
    }
    $mid = floor($length / 2);
    $left = array_slice($arr, 0, $mid);
    $right = array_slice($arr, $mid);
    return merge(mergeSort($left), mergeSort($right));
}

function merge($left, $right) {
    $result = [];
    $lIndex = $rIndex = 0;
    while ($lIndex < count($left) && $rIndex < count($right)) {
        if ($left[$lIndex] < $right[$rIndex]) {
            $result[] = $left[$lIndex++];
        } else {
            $result[] = $right[$rIndex++];
        }
    }
    while ($lIndex < count($left)) {
        $result[] = $left[$lIndex++];
    }
    while ($rIndex < count($right)) {
        $result[] = $right[$rIndex++];
    }
    return $result;
}
Copy after login

Practical case

Consider an unordered integer array[5 , 2, 8, 3, 1, 9, 4, 7, 6].

Use quick sort:

$sortedArray = quickSort([5, 2, 8, 3, 1, 9, 4, 7, 6]);
print_r($sortedArray);
Copy after login

Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9]
Copy after login
Copy after login

Use merge sort :

$sortedArray = mergeSort([5, 2, 8, 3, 1, 9, 4, 7, 6]);
print_r($sortedArray);
Copy after login

Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9]
Copy after login
Copy after login

The above is the detailed content of PHP array quick sort vs. merge sort. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!