当前位置:网站首页>Selection sort
Selection sort
2022-07-06 04:36:00 【A distant youth】
Selection sort (Selection-sort) It is a simple and intuitive sorting algorithm . How it works : First, find the minimum in the unsorted sequence ( Big ) Elements , To the beginning of the collating sequence , then , Continue to find the smallest from the remaining unsorted elements ( Big ) Elements , And then at the end of the sorted sequence . And so on , Until all the elements are sorted .
2.1 Algorithm description
n The direct selection and sorting of records can go through n-1 The direct selection and sorting of passes get ordered results . The specific algorithm is described as follows :
- The initial state : The disorder area is R[1..n], The ordered area is empty ;
- The first i Trip to sort (i=1,2,3…n-1) At the beginning of the , At present, the ordered region and the disordered region are R[1..i-1] and R(i..n). The sequence is from the current unordered area - Select the record with the smallest keyword R[k], Connect it with the disordered area 1 A record R In exchange for , send R[1..i] and R[i+1..n) The number of records increases 1 The number of new ordered areas and records decreased 1 A new disorder area ;
- n-1 The end of the trip , The array is ordered .
2.2 Dynamic diagram demonstration

2.3 Code implementation
function selectionSort(arr) {
var len = arr.length;
var minIndex, temp;
for (var i = 0; i < len - 1; i++) {
minIndex = i;
for (var j = i + 1; j < len; j++) {
if (arr[j] < arr[minIndex]) { // Look for the smallest number
minIndex = j; // Save the decimal index
}
}
temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
return arr;
}
One of the most stable sorting algorithms , Because whatever data goes in is O(n2) Time complexity of , So when you use it , The smaller the data, the better . The only benefit may be that it doesn't take up extra memory space . Theoretically speaking , Selection sorting may also be the most common sorting method that ordinary people think of .2.4 Algorithm analysis
边栏推荐
- R note prophet
- canal同步mysql数据变化到kafka(centos部署)
- Sentinel sliding window traffic statistics
- Platformio create libopencm3 + FreeRTOS project
- web工程导入了mysql驱动jar包却无法加载到驱动的问题
- 查询mysql数据库中各表记录数大小
- [Yu Yue education] reference materials of complex variable function and integral transformation of Northwestern Polytechnic University
- Script lifecycle
- 2328. 网格图中递增路径的数目(记忆化搜索)
- tengine 内核参数
猜你喜欢

Certbot failed to update certificate solution
![[05-1, 05-02, 05-03] network protocol](/img/25/2e9ccc3f31a1fd46c9ab643d48064b.jpg)
[05-1, 05-02, 05-03] network protocol

CertBot 更新证书失败解决

Patent | subject classification method based on graph convolution neural network fusion of multiple human brain maps

How do programmers teach their bosses to do things in one sentence? "I'm off duty first. You have to work harder."

1291_ Add timestamp function in xshell log

The value of two date types is subtracted and converted to seconds

Lombok principle and the pit of ⽤ @data and @builder at the same time

Digital children < daily question> (Digital DP)

Deep learning framework installation (tensorflow & pytorch & paddlepaddle)
随机推荐
Deep learning framework installation (tensorflow & pytorch & paddlepaddle)
Implementation of knowledge consolidation source code 2: TCP server receives and processes half packets and sticky packets
E. Best Pair
729. My schedule I (set or dynamic open point segment tree)
HotSpot VM
8. Static file
Cross domain and jsonp details
拉格朗日插值法
捷码赋能案例:专业培训、技术支撑,多措并举推动毕业生搭建智慧校园毕设系统
Ue5 small knowledge freezerendering view rendered objects in the cone
[Yu Yue education] reference materials of complex variable function and integral transformation of Northwestern Polytechnic University
深入浅出node模板解析错误escape is not a function
Fedora/REHL 安装 semanage
Recommendation | recommendation of 9 psychotherapy books
About some basic DP -- those things about coins (the basic introduction of DP)
Explain in simple terms node template parsing error escape is not a function
Mysql database storage engine
web工程导入了mysql驱动jar包却无法加载到驱动的问题
Visio draws Tai Chi
ue5 小知识 FreezeRendering 查看视锥内渲染的物体