search
HomeDaily ProgrammingPHP KnowledgeDetailed analysis of selection sort algorithm

Selection sort

Selection sort is one of the most stable sorting algorithms. When using it, the smaller the data size, the better. Theoretically speaking, selection sort may also be the most common method that most people think of when sorting.

Selection sort is a simple and intuitive sorting algorithm. How it works is:

Traverse an array, and in the process, find the maximum value and its position in the array. Then "swap the position" of the unit with the maximum value with the last unit of the array. After doing this, the maximum value in the array must be placed at the last position.

Continue to traverse the remaining data in the above process and do the same thing. At this time, the maximum value of the remaining part can also be placed at the last position of the remaining part - it is the penultimate position as a whole. s position.

So on and so forth. . . . . .

Illustration:

##Original array 18221215239First trip18221215923##Second tripThe third tripThe fourth trip##The fifth trip91215182223# #The code is as follows:




18 9 12 15 22 23
15 9 12 18 22 23
12 9 15 18 22 23

<?php
        $arr1 = array(18,22,12,15,23,9);
        $n = count($arr1);
        for ($i=0; $i < $n-1; $i++) { 
            //找最大值
            $max = $arr1[0];
            $max_key = 0;
            for ($k=0; $k < $n - $i; $k++) { 
                if ($arr1[$k] > $max) {
                    $max = $arr1[$k];
                    $max_key = $k;
                }
            }
            //交换
            $temp = $arr1[$max_key];
            $arr1[$max_key] = $arr1[$n-1-$i];
            $arr1[$n-1-$i] = $temp;
        }
Summary of the rules:

1. To find the largest one from beginning to end value (and subscript), and the number of times to exchange is $n-1, $n is the length of the array 2. What each time needs to do is: a) find the maximum value, right) and Exchange the maximum value with the last item of this trip;

3. The number of data to find the maximum value in each trip is 1 less than the previous trip, of which the first trip has $n indivual.


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

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!