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

PHP sorting algorithm selection sort

藏色散人
藏色散人forward
2019-12-09 14:32:472873browse

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;

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;//元素下标交换
}

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;
    }

● 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));

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!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete