当前位置:网站首页>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
边栏推荐
- 2/13 review Backpack + monotonic queue variant
- After learning classes and objects, I wrote a date class
- Can CDC pull the Oracle table in full
- [leetcode question brushing day 33] 1189 The maximum number of "balloons", 201. The number range is bitwise AND
- Microservice resource address
- In depth MySQL transactions, stored procedures and triggers
- 8. Static file
- CADD course learning (7) -- Simulation of target and small molecule interaction (flexible docking autodock)
- Solution of storage bar code management system in food industry
- flink sql 能同时读多个topic吗。with里怎么写
猜你喜欢
Use sentinel to interface locally
Visio draw fan
Execution order of scripts bound to game objects
Data processing methods - smote series and adasyn
Query the number and size of records in each table in MySQL database
How does computer nail adjust sound
Slow SQL fetching and analysis of MySQL database
Stable Huawei micro certification, stable Huawei cloud database service practice
Easyrecovery靠谱不收费的数据恢复电脑软件
题解:《单词覆盖还原》、《最长连号》、《小玉买文具》、《小玉家的电费》
随机推荐
Redis —— Redis In Action —— Redis 实战—— 实战篇一 —— 基于 Redis 的短信登录功能 —— Redis + Token 的共享 session 应用— 有代码
Basic use of MySQL (it is recommended to read and recite the content)
SharedPreferences source code analysis
word封面下划线
Use sentinel to interface locally
When debugging after pycharm remote server is connected, trying to add breakpoint to file that does not exist: /data appears_ sda/d:/segmentation
1291_ Add timestamp function in xshell log
Understanding of processes, threads, coroutines, synchronization, asynchrony, blocking, non blocking, concurrency, parallelism, and serialization
The most detailed and comprehensive update content and all functions of guitar pro 8.0
深入浅出node模板解析错误escape is not a function
CADD课程学习(8)-- 化合物库虚拟筛选(Virtual Screening)
BOM - location, history, pop-up box, timing
Mlapi series - 04 - network variables and network serialization [network synchronization]
VNCTF2022 WriteUp
也算是学习中的小总结
Canal synchronizes MySQL data changes to Kafka (CentOS deployment)
P2022 有趣的数(二分&数位dp)
Stable Huawei micro certification, stable Huawei cloud database service practice
E. Best Pair
[network] channel attention network and spatial attention network