JIYIK CN >

Current Location:Home > Learning > ALGORITHM >

The road to learning sorting algorithms - selection sort

Author:JIYIK Last Updated:2025/03/19 Views:

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.

Article URL:

Related Articles

The road to learning sorting algorithms - heap sort

Publish Date:2025/03/19 Views:145 Category:ALGORITHM

Like other sorting algorithms, let's first look at the definition of heap sort. Heapsort : refers to a sorting algorithm designed using the heap data structure. A heap is a structure that approximates a complete binary tree and satisfies th

The road to learning sorting algorithms - bubble sort

Publish Date:2025/03/19 Views:80 Category:ALGORITHM

Bubble sort is also a simple and intuitive sorting algorithm. The idea is that it repeatedly visits the sequence to be sorted, compares two elements at a time, and swaps them if they are in the wrong order. The work of visiting the sequence

Core code of commonly used sorting algorithms

Publish Date:2025/03/19 Views:122 Category:ALGORITHM

In the article "Common Sorting Algorithms", we briefly introduced the ideas and implementation steps of various sorting algorithms. In this article, I will share the core codes of these sorting algorithms with you. The complete code of all

Learning the Sorting Algorithm - Radix Sorting (LSD)

Publish Date:2025/03/19 Views:177 Category:ALGORITHM

Radix Sort : It is a non-comparative integer sorting algorithm. The basic principle of radix sorting is to group integers according to the number of digits in the integer. In the grouping process, the missing digits are filled with 0. Accor

The road to learning sorting algorithms - Radix Sort (MSD)

Publish Date:2025/03/19 Views:140 Category:ALGORITHM

In the article "Radix Sort (LSD)" , we explained the concept and efficiency analysis of radix sort. We will not repeat it in this article. You can refer to that article to get a general understanding of the idea of ​​radix sort. Next, w

PHP finds the common prefix of multiple strings [Case]

Publish Date:2025/03/19 Views:94 Category:ALGORITHM

This article shares with you the application of a small algorithm - finding the common prefix of a string array: array ( 'abcdefg' , 'abcdfio' , 'abcdqle' ) In the above array string, the common prefix is ​​'abcd'. The first thing that

First contact with CGI

Publish Date:2025/03/18 Views:51 Category:NETWORK

Since I am a PHP programmer, I often have to build a PHP operating environment. The popular nginx+php environment is very popular, and the mode it adopts is the FastCGI method, so I spent some time to learn about FastCGI. CGI (Common Gatewa

PHP cluster session sharing

Publish Date:2025/03/18 Views:124 Category:NETWORK

The concept of cluster is not complicated. It is actually multiple computers working together for the same goal. In Web applications, multiple servers provide services for a site. The first step to build a PHP cluster is to set up load bala

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial