Detailed explanation of common sorting examples in PHP

小云云
Release: 2023-03-21 13:58:01
Original
1221 people have browsed it

This article mainly shares with you detailed explanations of commonly used sorting examples in PHP. It is mainly shared with you in the form of code. I hope it can help you.

$arr=array(1,43,54,62,21,66,32,78,36,76,39);
Copy after login

1. Bubble sort

function bubbleSort ($arr)
{
    $len = count($arr);
    //该层循环控制 需要冒泡的轮数
    for ($i=1; $i<$len; $i++) {
    //该层循环用来控制每轮 冒出一个数 需要比较的次数
        for ($k=0; $k<$len-$i; $k++) {
            if($arr[$k] > $arr[$k+1]) {
            $tmp = $arr[$k+1]; // 声明一个临时变量
            $arr[$k+1] = $arr[$k];
            $arr[$k] = $tmp;
            }
        }
    }
    return $arr;
}
Copy after login

2. Quick sort

  1. Pick out an element from the sequence, called "pivot" ),

  2. Reorder the sequence, all elements smaller than the baseline value are placed in front of the baseline, and all elements larger than the baseline value are placed behind the baseline (the same number can be placed anywhere side). After this partition exits, the base is in the middle of the sequence. This is called a partition operation.

  3. Recursively sort the subarray of elements smaller than the base value and the subarray of elements greater than the base value.
    $arr=array(1,43,54,62,21,66,32,78,36,76,39);
    function quick_sort($arr)
    {
    //Judgement Whether the parameter is an array
    if(!is_array($arr)) return false;
    //Recursive exit: the array length is 1, return the array directly
    $length = count($arr);
    if($length<=1) return $arr;
    //If there are multiple array elements, define two empty arrays
    $left = $right = array();
    //Use for Loop through and use the first element as the comparison object
    for($i=1; $i<$length; $i++)
    {
    //Judge the size of the current element
    if ($arr[$i]<$arr[0]){
                                                                                                                               | Merge
    return array_merge($left,array($arr[0]),$right);
    }



    3.Select sort


    First find the smallest element in the unsorted sequence and store it at the beginning of the sorted sequence. Then, continue to find the smallest element from the remaining unsorted elements and put it at the end of the sorted sequence. And so on until all elements are sorted.

  4. //实现思路 双重循环完成,外层控制轮数,当前的最小值。内层 控制的比较次数
    $arr=array(1,43,54,62,21,66,32,78,36,76,39);
    function select_sort($arr) {
        //$i 当前最小值的位置, 需要参与比较的元素
        for($i=0, $len=count($arr); $i<$len-1; $i++) {
            //先假设最小的值的位置
            $p = $i;
            //$j 当前都需要和哪些元素比较,$i 后边的。
                for($j=$i+1; $j<$len; $j++) {
                //$arr[$p] 是 当前已知的最小值
                    if($arr[$p] > $arr[$j]) {
                    //比较,发现更小的,记录下最小值的位置;并且在下次比较时,应该采用已知的最小值进行比较。
                    $p = $j;
                    }
                }
            //已经确定了当前的最小值的位置,保存到$p中。
            //如果发现 最小值的位置与当前假设的位置$i不同,则位置互换即可
            if($p != $i) {
                $tmp = $arr[$p];
                $arr[$p] = $arr[$i];
                $arr[$i] = $tmp;
            }
        }
        //返回最终结果
        return $arr;
    }
    Copy after login
4. Insertion sort

It works by constructing an ordered sequence, for unsorted data, from backward in the sorted sequence Scan forward to find the corresponding position and insert it.


Starting from the first element, the element can be considered to have been sorted

Take out the next element and scan from back to front in the sorted element sequence

  1. If the element (sorted) is larger than the new element, move the element Go to the next position

  2. Repeat step 3 until you find a position where the sorted element is less than or equal to the new element

  3. Insert the new element into

  4. Repeat steps in this position

  5. $arr=array(1,43,54,62,21,66,32,78,36,76,39);
    function insert_sort($arr)
    {
        $len=count($arr);
        for($i=1; $i<$len; $i++) {
            //获得当前需要比较的元素值。
            $tmp = $arr[$i];
            //内层循环控制 比较 并 插入
                for($j=$i-1; $j>=0; $j--) {
                //$arr[$i];//需要插入的元素; $arr[$j];//需要比较的元素
                    if($tmp < $arr[$j]) {
                        //发现插入的元素要小,交换位置
                        //将后边的元素与前面的元素互换
                        $arr[$j+1] = $arr[$j];
                        //将前面的数设置为 当前需要交换的数
                        $arr[$j] = $tmp;
                    } else {
                        //如果碰到不需要移动的元素
                        //由于是已经排序好是数组,则前面的就不需要再次比较了。
                        break;
                    }
                }
            }
        //将这个元素 插入到已经排序好的序列内。
        //返回
        return $arr;
    }
    Copy after login

    5. Dictionary sorting (that is, the number of times the same value appears in the array)

    PHP has built-in Functionarray_count_values, of course, you can also loop it yourself.
  6. Related recommendations:


    Example analysis of basic commonly used sorting algorithms in JavaScript

    Explanation of dynamic diagrams of commonly used sorting algorithms

    Nine commonly used sorting algorithms in JavaScript_javascript skills

    The above is the detailed content of Detailed explanation of common sorting examples in 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!