What is selection sort?

Basic idea: a permutation is divided into ordered and disordered areas, ordered areas on the left, disordered areas on the right. First, find the smallest (largest) element in the unordered area and store it at the beginning of the ordered area. Then, continue to find the smallest (largest) element in the remaining unordered area and put it at the end of the ordered area. And so on until the unordered region has no elements to arrange

Intuitive expression: to look through an array, find the small one in the left of the array, and then continue to look in the rest of the elements, until the sorting is complete

Algorithm description

Suppose an array has two regions

Five, eight, two, three, one

The ordered region is empty, and the unordered region is 5, 8, 2, 3, 1

-----------------------------------

First: record the first value of the array (5), see if there is anything smaller, and if there is, record (1). Until the first iteration of the array is complete. The smallest value (1) is then swapped with the first value of the unordered region (5)

Results: 1, 8, 2, 3, 5

The ordered region is 1, and the unordered region is 8, 2, 3, 5

-----------------------------------

Then the second traversal is carried out, and the minimum value (2) is continued to be found in the unordered area. After the traversal is completed, the minimum value (2) is exchanged with the first value (8) in the unordered area

Results: 1, 2, 8, 3, 5

The ordered region is 1, 2, and the unordered region is 8, 3, 5

-----------------------------------

. And so on until the unordered region is empty

<img src=”https://noxussj.top:3000/21/1.gif”></img>

Reference is worth collecting ten classic sorting algorithm

additional

  • This article is published through multiple platforms of “We Media” and will not be maintained after publication. If you have any objection to the content, you can discuss it in the GitHub below
  • The ongoing maintenance / 500 + face questions before update/notes 】 https://github.com/noxussj/In…
  • [3D city modeling using three. JS (Zhuhai City)] https://3d.noxussj.top/