当前位置:网站首页>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
边栏推荐
- Platformio create libopencm3 + FreeRTOS project
- 【HBZ分享】云数据库如何定位慢查询
- 电脑钉钉怎么调整声音
- Solution of storage bar code management system in food industry
- Meet diverse needs: jetmade creates three one-stop development packages to help efficient development
- web工程导入了mysql驱动jar包却无法加载到驱动的问题
- One question per day (Mathematics)
- Hashlimit rate control
- English Vocabulary - life scene memory method
- 729. My schedule I (set or dynamic open point segment tree)
猜你喜欢
R note prophet
[Zhao Yuqiang] deploy kubernetes cluster with binary package
Redis —— Redis In Action —— Redis 实战—— 实战篇一 —— 基于 Redis 的短信登录功能 —— Redis + Token 的共享 session 应用— 有代码
捷码赋能案例:专业培训、技术支撑,多措并举推动毕业生搭建智慧校园毕设系统
How do programmers teach their bosses to do things in one sentence? "I'm off duty first. You have to work harder."
Fedora/rehl installation semanage
Cross domain and jsonp details
How to solve the problem of slow downloading from foreign NPM official servers—— Teach you two ways to switch to Taobao NPM image server
MLAPI系列 - 04 - 网络变量和网络序列化【网络同步】
Query the number and size of records in each table in MySQL database
随机推荐
Case of Jiecode empowerment: professional training, technical support, and multiple measures to promote graduates to build smart campus completion system
Tengine kernel parameters
Fedora/REHL 安装 semanage
深入浅出node模板解析错误escape is not a function
我想问一下 按照现在mysql-cdc的设计,全量阶段,如果某一个chunk的binlog回填阶段,
牛顿插值法
Lambda expression learning
Meet diverse needs: jetmade creates three one-stop development packages to help efficient development
Sorting out the latest Android interview points in 2022 to help you easily win the offer - attached is the summary of Android intermediate and advanced interview questions in 2022
Can Flink SQL read multiple topics at the same time. How to write in with
729. My schedule I (set or dynamic open point segment tree)
题解:《单词覆盖还原》、《最长连号》、《小玉买文具》、《小玉家的电费》
Basic explanation of turtle module - draw curve
MIT CMS. 300 session 8 – immersion / immersion
Solution of storage bar code management system in food industry
How to solve the problem of slow downloading from foreign NPM official servers—— Teach you two ways to switch to Taobao NPM image server
Selection of slow motion function
web工程导入了mysql驱动jar包却无法加载到驱动的问题
Mixed development of QML and QWidget (preliminary exploration)
2327. 知道秘密的人数(递推)