The road to learning sorting algorithms - selection sort
Selection sort is a simple and intuitive sorting algorithm. Its basic idea is to select a maximum (or minimum) element in an unsorted sequence and put it at the end (note: this is the end of the unsorted sequence, which can be considered as the starting position of the ordered sequence).
Let's take a look at this selection process through a simple illustration.
First, record and select the first element as the default maximum value, v stores the value, and p stores the position.
v = 10, p = 0
Then start from the first position and search backward for an element greater than 10, and replace the values of v and p after finding them.
v = 12, p = 2
v = 30, p = 3
Until the last element, 30 is the largest element in the unsorted sequence.
Then 30 and the last element are swapped, and 30 is no longer involved in the comparison in the next selection.
The second choice, the initial values of v and p are 0 and 10
v = 10,p = 0
After searching, we finally determined that v = 15 and p = 3
Then swap 15 and 5 (because 30 is no longer in the sort order)
15 and 30 are no longer included in the sorting.
Then follow the above process to select and exchange until all elements are in order
The above is the whole process of selection sorting. The steps of sorting in words are
1) First, find the smallest (largest) element in the unsorted sequence and store it at the beginning of the sorted sequence
. 2) Then continue to find the smallest (largest) element from the remaining unsorted elements and place it at the end of the sorted sequence.
3) Repeat the second step until all elements are sorted.
Let's look at the code implementation of selection sort
/**
* 交换函数
*/
function swap(&$arr,$a,$b){
$t = $arr[$a];
$arr[$a] = $arr[$b];
$arr[$b] = $t;
}
function SelectSort(&$arr){
$end = count($arr)-1;
do{
$p = 0;
for($i=0;$i<=$end;$i++){
if($arr[$i]>$arr[$p]){
$p = $i;
}
}
swap($arr,$p,$end);
}while(--$end>0);
}
$arr = array(10,6,8,23,4,1,17,56,32,50,11,9);
SelectSort($arr);
print_r($arr);
We can see from the code above that selection sort is very intuitive. Its implementation idea is very simple and not complicated.
But there is one thing we need to note. Although selection sort is simple, its efficiency is lower than other sorting algorithms. Its time complexity is O(n²). So in the application, we must choose our sorting algorithm according to the actual situation.
I hope this article is helpful to you.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Check if a Post exists in PHP
Publish Date:2025/04/13 Views:170 Category:PHP
-
PHP $_POST is a super global variable that can contain key-value pairs of HTML form data submitted through the post method. We will learn different ways to check $_POST if a and contains some data in this article. These methods will use iss
PHP with Ajax
Publish Date:2025/04/13 Views:139 Category:PHP
-
We will use PHP and ajax by printing a simple sum of two numbers 2 and . Also, print a php array in JSON. 3 object We will also use PHP with ajax by getting the HTML formatted output from the number division in PHP. Printing simple addition
Store Div Id in PHP variable and pass it to JavaScript
Publish Date:2025/04/13 Views:51 Category:PHP
-
This article shows you how to div id store a in a PHP variable and pass it to JavaScript code. We will answer the following questions. What is div id ? How to div id store in a PHP variable? How to pass variables to JavaScript code? Let’s
Returns the article tag with ID from the action page
Publish Date:2025/04/13 Views:80 Category:PHP
-
Let's say you're in a login form and you enter the wrong information; in this case, you probably want to go back to the login page. PHP has a built-in function header() to redirect a page to a specific page. But what if the login page is at
Switching PHP versions on Ubuntu
Publish Date:2025/04/13 Views:78 Category:PHP
-
Different tasks may require running multiple versions of PHP. You may need to switch PHP versions by running two sites on the same server or testing older versions of code using outdated methods. We can switch PHP versions on Ubuntu using t
Resizing images in PHP
Publish Date:2025/04/13 Views:155 Category:PHP
-
In this tutorial article, we will discuss about resizing images in PHP. Load the image before resizing Before we can resize an image, we must first load it as an image resource in our script. This is file_get_contents() different from using
PHP upload image
Publish Date:2025/04/13 Views:61 Category:PHP
-
We can upload images in PHP using simple file upload operation, but first, php.ini file upload should be enabled from Files. This tutorial demonstrates how to upload images in PHP. php.ini Enable file upload from file in PHP to upload image
Creating a signature from Hash_hmac() and Sha256 in PHP
Publish Date:2025/04/13 Views:107 Category:PHP
-
PHP has one of the best encryption functions for data security. Hash_hmac() The encrypt function is one of the most famous encryptors. We'll show you how to use hash_hmac and sha256 encryptors to create 安全签名 one that you can store i
Updating PHP 7.x to 7.4 on CentOS
Publish Date:2025/04/13 Views:131 Category:PHP
-
This article shows the steps to update the PHP version from 7.x version to 7.4 in CentOS. How to Update PHP from 7.X to 7.4 in CentOS Update operating system packages. yum update -y Check your PHP version in CentOS. php -v Prints a list of