How to write quick sort algorithm using PHP

王林
Release: 2023-07-07 22:18:01
Original
1432 people have browsed it

How to use PHP to write a quick sort algorithm

The quick sort algorithm is a commonly used sorting algorithm that shows good efficiency when processing large amounts of data. This article will introduce how to write a quick sort algorithm using PHP and provide code examples.

The basic principle of the quick sort algorithm is to recursively split the array into two smaller sub-arrays, then sort the two sub-arrays, and finally merge the two ordered sub-arrays into an ordered one array.

The following is a code example of the quick sort algorithm written in PHP:

function quickSort($arr) {
    // 如果数组长度小于等于1,则直接返回数组
    $length = count($arr);
    if ($length <= 1) {
        return $arr;
    }
    
    // 选择一个基准元素(可以是任意一个元素),将数组分成两部分
    $pivot = $arr[0];
    $left = $right = array();
    
    // 将比基准元素小的放在左子数组,比基准元素大的放在右子数组
    for ($i = 1; $i < $length; $i++) {
        if ($arr[$i] < $pivot) {
            $left[] = $arr[$i];
        } else {
            $right[] = $arr[$i];
        }
    }
    
    // 递归地对左右子数组进行快速排序,并将结果合并
    return array_merge(quickSort($left), array($pivot), quickSort($right));
}

// 调用快速排序函数
$array = array(7, 2, 5, 3, 9, 6);
$sortedArray = quickSort($array);

// 输出排序结果
echo "排序结果:" . implode(", ", $sortedArray);
Copy after login

In the above code, we first define a function named quickSort. This function accepts an array as a parameter and returns an ordered array after quick sorting.

First, determine whether the array length is less than or equal to 1 inside the function. If so, return the array directly. This is the end condition of the recursive algorithm.

Then, we select the first element of the array as the base element and define two empty arrays $left and $right to store elements smaller than the base element. and large elements.

Next, use a loop to traverse the array, put elements smaller than the base element into the $left array, and put elements larger than the base element into the $right array.

Finally, we use recursive calls to the quickSort function to sort the left and right subarrays, and merge the results using the array_merge function, plus the base element. The final return is the ordered array after quick sorting.

Finally, we define an array $array and call the quickSort function to sort it. Finally, use the implode function to output the sorted results in comma-separated form.

Using the above code example, we can quickly sort the array easily.

To summarize, the quick sort algorithm is an efficient sorting algorithm that shows good performance when processing large amounts of data. Through the above PHP code examples, you can easily write your own quick sort algorithm and apply it to actual projects. Understanding and mastering the quick sort algorithm is of great significance to improving the efficiency of the program. I hope this article will be helpful to you in learning and using the quick sort algorithm.

The above is the detailed content of How to write quick sort algorithm using PHP. 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!