Home > Backend Development > PHP Tutorial > PHP sorting algorithm selection sort

PHP sorting algorithm selection sort

藏色散人
Release: 2023-04-07 22:56:01
forward
2889 people have browsed it

select sorting

● Selection sorting is also internal sorting

● Sorting idea:

First select a number at random, It is to select an element in the array to be sorted and compare it with other elements of the array. Then compare and swap positions to get the minimum or maximum value, and then select a number in the remaining array to compare with the remaining elements of the array, and finally get the second minimum or maximum element. And so on

● Schematic diagram:

The selection sort has a total array size - 1 round of sorting; each round of sorting is another cycle; first assume that the current array is the minimum number, Then compare it with the following elements in sequence. If it is found that there is a smaller number than the current number, re-determine the minimum number and get the subscript. When the end of the array is traversed, the minimum number and subscript of this round are obtained, and exchange

1. Suppose there is an array to be sorted [3, 1, 15, 5, 20]

2. Randomly select an element, assuming that the first one is the smallest element, Compare 3 with the remaining elements of the array. After the first round of sorting, we get the smallest element 1

<?php
$arr = [3, 1, 15, 5, 20];
$count = count($arr);
//假设最小的元素就是第一个元素
$minIndex = 0;
$min = $arr[0];
for ($j = $minIndex + 1; $j < $count; $j++) {
    if ($min > $arr[$j]) { //假定的最小值大于后面的值,重置最小值
        $min = $arr[$j];
        $minIndex = $j;
    }
}
$arr[$minIndex] = $arr[0];
$arr[0] = $min;
Copy after login

3. Select a hypothetical minimum value again and compare it with the following elements to get the second minimum value

<?php
$arr = [1, 3, 15, 5, 20];
$count = count($arr);
//假设最小的元素就是第二个元素
$minIndex = 1;//假设的最小元素的下表
$min = $arr[1];//假定最小元素的值
for ($j = $minIndex + 1; $j < $count; $j++) {
    if ($min > $arr[$j]) { //假定的最小值大于后面的值,重置最小值
        $min = $arr[$j];
        $minIndex = $j;
    }
}
if ($minIndex != 1) {
    $arr[$minIndex] = $arr[1];//假定的最小元素不是最小元素,那么把后面的最小元素和假定的最小元素做交换
    $arr[1] = $min;//元素下标交换
}
Copy after login

4. By analogy, you can use a double for loop to get the selection sorting algorithm as follows:

  public static function sortSelect(array $arr) :array
    {
        if (!is_array($arr)) {
            return [&#39;message&#39; => &#39;$arr不是一个数组&#39;];
        }
        $count = count($arr);
        if ($count <= 1) {
            return $arr;
        }
        for ($i = 0; $i < $count; $i++) {
            $minIndex = $i;
            $min = $arr[$i];
            for ($j = $i + 1; $j < $count; $j++) {
                if ($min > $arr[$j]) {//选择的假定最小元素大于后面的元素
                    $min = $arr[$j];//把后面的最小元素赋值给假定的最小元素
                    $minIndex = $j;//把后面最小元素的坐标赋值给假定的最小元素
                }
            }
            if ($minIndex != $i) {//如果在这个位置,一开始的假定最小元素的坐标被替换了,说明假定最小元素不是最小元素,那么发生交换
                $arr[$minIndex] = $arr[$i];//交换最小元素,把最小元素和假定元素做交换
                $arr[$i] = $min;
            }
        }
        return $arr;
    }
Copy after login

● The complete code is as follows:

<?php
class SelectSort
{
    public static function select(array $arr):array
    {
        $count = count($arr);
        //假设最小的元素就是第二个元素
        $minIndex = 0;//假设的最小元素的下表
        $min = $arr[0];//假定最小元素的值
        for ($j = $minIndex + 1; $j < $count; $j++) {
            if ($min > $arr[$j]) { //假定的最小值大于后面的值,重置最小值
                $min = $arr[$j];
                $minIndex = $j;
            }
        }
        if ($minIndex != 0) {
            $arr[$minIndex] = $arr[0];//假定的最小元素不是最小元素,那么把后面的最小元素和假定的最小元素做交换
            $arr[0] = $min;//元素下标交换
        }
        var_dump($arr);
        $minIndex = 1;//假设的最小元素的下表
        $min = $arr[1];//假定最小元素的值
        for ($j = $minIndex + 1; $j < $count; $j++) {
            if ($min > $arr[$j]) { //假定的最小值大于后面的值,重置最小值
                $min = $arr[$j];
                $minIndex = $j;
            }
        }
        if ($minIndex != 1) {
            $arr[$minIndex] = $arr[1];//假定的最小元素不是最小元素,那么把后面的最小元素和假定的最小元素做交换
            $arr[1] = $min;//元素下标交换
        }
        var_dump($arr);
        $minIndex = 2;//假设的最小元素的下表
        $min = $arr[2];//假定最小元素的值
        for ($j = $minIndex + 1; $j < $count; $j++) {
            if ($min > $arr[$j]) { //假定的最小值大于后面的值,重置最小值
                $min = $arr[$j];
                $minIndex = $j;
            }
        }
        if ($minIndex != 2) {
            $arr[$minIndex] = $arr[2];//假定的最小元素不是最小元素,那么把后面的最小元素和假定的最小元素做交换
            $arr[2] = $min;//元素下标交换
        }
        var_dump($arr);
        return $arr;
    }
    public static function sortSelect(array $arr) :array
    {
        if (!is_array($arr)) {
            return [&#39;message&#39; => &#39;$arr不是一个数组&#39;];
        }
        $count = count($arr);
        if ($count <= 1) {
            return $arr;
        }
        for ($i = 0; $i < $count - 1; $i++) {
            $minIndex = $i;
            $min = $arr[$i];
            for ($j = $i + 1; $j < $count; $j++) {
                if ($min > $arr[$j]) {//选择的假定最小元素大于后面的元素
                    $min = $arr[$j];//把后面的最小元素赋值给假定的最小元素
                    $minIndex = $j;//把后面最小元素的坐标赋值给假定的最小元素
                }
            }
            if ($minIndex != $i) {//如果在这个位置,一开始的假定最小元素的坐标被替换了,说明假定最小元素不是最小元素,那么发生交换
                $arr[$minIndex] = $arr[$i];//交换最小元素,把最小元素和假定元素做交换
                $arr[$i] = $min;
            }
        }
        return $arr;
    }
}
$arr = [3, 1, 15, 5, 20];
var_dump(SelectSort::sortSelect($arr));
Copy after login

The above is the detailed content of PHP sorting algorithm selection sort. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:learnku.com
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