当前位置:网站首页>Master several common sorting - Select Sorting
Master several common sorting - Select Sorting
2022-07-28 13:59:00 【Maic】
Selection sortIt's a simple sort , The time complexity is O(n^2), stayunsortedFind the smallest number in the array of , Then put it inThe starting position, Continue to find the smallest element from the remaining unsorted data , Put it at the end of the sorted , And so on , Until all elements are sorted .
Let's take a look at the code for selecting sorting
function selectSort(arr) {
const len = arr.length;
var minIndex, temp;
for (let i=0;i<len;i++) {
minIndex = i; // Suppose the current circular index is the smallest element
for (let j=i+1;j<len;j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j; // Look for the smallest value
}
}
// In exchange for minIndex And i The value of the element
temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
return arr;
}
selectSort([6,12,80,91,8,0]);
Let's draw a diagram to restore all the processes of sorting , As follows
From each cycle, we can know the selection sorting , In fact, first confirm the index of the starting position , Suppose the first is the minimum position , Find a value smaller than the first position from the remaining elements , If the remaining elements are smaller than it , Then confirm that the current index is the minimum index value , And exchange the positions of the two elements .
Then start with the second element , Suppose the second element is the minimum , Then find the smallest element from the remaining elements , If the remaining elements are smaller than it, exchange positions , without , It is normal not to exchange positions , Until the loop reaches the last element .
Be more concise , Choosing to sort is
1、 Suppose the first element is the minimum
2、 Select from the remaining elements and compare the element size with the first element , Confirm the minimum index value , And then switch places
3、 Cycle from the remaining positions , Assume that the remaining position is the minimum , Then select from the remaining elements to compare , Then confirm whether to exchange positions
4、 Cycle until the last index
summary
1、 Selection sort The time complexity is O(n^2)
2、 Suppose the first element is the smallest element , Compare with it in the remaining unsorted elements , If it's smaller , Just confirm the minimum position index , Swap places with it
3、 Among all the remaining unsorted elements , Suppose the first element is the minimum , Then compare with the remaining elements in turn , Confirm the current minimum index of the element , Swap places , In turn, cycle , Until the end of the last cycle
边栏推荐
- R language ggplot2 visualization: use the ggviolin function of ggpubr package to visualize violin diagrams, set the palette parameter, and customize the border colors of violin diagrams at different l
- UVA1599理想路径题解
- Several efficient APIs commonly used in inventory operation URL
- R语言可视化散点图、使用ggrepel包的geom_text_repel函数避免数据点之间的标签互相重叠(使用参数xlim和ylim将标签添加到可视化图像的特定区域、指定标签线段并添加箭头)
- 拒绝服务 DDoS 攻击
- The strongest distributed locking tool: redisson
- Record a fake login of cookie
- Qt5 development from introduction to mastery -- the first overview
- 算法---不同路径(Kotlin)
- 要想组建敏捷团队,这些方法不可少
猜你喜欢

Cool operation preheating! Code to achieve small planet effect

安全保障基于软件全生命周期-Istio的授权机制

7. Dependency injection

Qt5 development from introduction to mastery -- the first overview

基于NoneBot2的qq机器人配置记录

【安全】 阅读 RFC6749 及理解 Oauth2.0 下的授权码模式

酷炫操作预热!代码实现小星球特效

SAP UI5 FileUploader 控件实现本地文件上传,接收服务器端的响应时遇到跨域访问错误的试读版

《机器学习》(周志华) 第6章 支持向量 学习心得 笔记

Deploy application delivery services in kubernetes (Part 1)
随机推荐
了解虚拟列表背后原理,轻松实现虚拟列表
Understanding of stack and practical application scenarios
R语言可视化散点图、使用ggrepel包的geom_text_repel函数避免数据点之间的标签互相重叠(使用参数xlim和ylim将标签添加到可视化图像的特定区域、指定标签线段并添加箭头)
[security] read rfc6749 and understand the authorization code mode under oauth2.0
数据库系统原理与应用教程(062)—— MySQL 练习题:操作题 32-38(六)
对“Image Denoising Using an Improved Generative Adversarial Network with Wasserstein Distance“的理解
Product Manager: job responsibility table
【LVGL事件(Events)】事件代码
R language uses LM function to build linear regression model and subset function to specify subset of data set to build regression model (use floor function and length function to select the former pa
P1797 heavy transportation problem solution
Record a fake login of cookie
掌握闭包,夯实基本功
regular expression
数据库系统原理与应用教程(061)—— MySQL 练习题:操作题 21-31(五)
Countdown 2 days! 2022 China Computing Conference: Mobile cloud invites you to meet with computing network for innovative development
111. The sap ui5 fileuploader control realizes local file upload and encounters a cross domain access error when receiving the response from the server
正则表达式
ES6 what amazing writing methods have you used
Socket class understanding and learning about TCP character stream programming
掌握常见的几种排序-选择排序