当前位置:网站首页>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
边栏推荐
- 8. Static file
- About some basic DP -- those things about coins (the basic introduction of DP)
- MLAPI系列 - 04 - 网络变量和网络序列化【网络同步】
- Query the number and size of records in each table in MySQL database
- Overturn your cognition? The nature of get and post requests
- Meet diverse needs: jetmade creates three one-stop development packages to help efficient development
- Unity screen coordinates ugui coordinates world coordinates conversion between three coordinate systems
- canal同步mysql数据变化到kafka(centos部署)
- Lombok原理和同时使⽤@Data和@Builder 的坑
- flink sql 能同时读多个topic吗。with里怎么写
猜你喜欢

Yyds dry inventory automatic lighting system based on CC2530 (ZigBee)

Solution of storage bar code management system in food industry

Case of Jiecode empowerment: professional training, technical support, and multiple measures to promote graduates to build smart campus completion system

ETCD数据库源码分析——etcdserver bootstrap初始化存储

Understanding of processes, threads, coroutines, synchronization, asynchrony, blocking, non blocking, concurrency, parallelism, and serialization

捷码赋能案例:专业培训、技术支撑,多措并举推动毕业生搭建智慧校园毕设系统

View workflow

题解:《单词覆盖还原》、《最长连号》、《小玉买文具》、《小玉家的电费》

CADD course learning (8) -- virtual screening of Compound Library
![[network] channel attention network and spatial attention network](/img/b5/5e746f0dd6badcf0714cae05fc6e82.jpg)
[network] channel attention network and spatial attention network
随机推荐
[network] channel attention network and spatial attention network
HotSpot VM
ue5 小知识点 开启lumen的设置
MLAPI系列 - 04 - 网络变量和网络序列化【网络同步】
8. Static file
ue5 小知识 FreezeRendering 查看视锥内渲染的物体
Stable Huawei micro certification, stable Huawei cloud database service practice
Easyrecovery reliable and toll free data recovery computer software
How to solve the problem of slow downloading from foreign NPM official servers—— Teach you two ways to switch to Taobao NPM image server
Delete subsequence < daily question >
SharedPreferences 源码分析
Yyds dry goods inventory OSI & tcp/ip
Complete list of common functions of turtle module
[Chongqing Guangdong education] engineering fluid mechanics reference materials of southwestjiaotonguniversity
CADD course learning (8) -- virtual screening of Compound Library
[try to hack] John hash cracking tool
2328. Number of incremental paths in the grid graph (memory search)
Data processing methods - smote series and adasyn
P2102 地砖铺设(dfs&贪心)
[Chongqing Guangdong education] Suzhou University English film and Television Appreciation reference materials