当前位置:网站首页>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
边栏推荐
- Security assurance is based on software life cycle -psp application
- 不用Swagger,那我用啥?
- 30天刷题计划(三)
- Cool operation preheating! Code to achieve small planet effect
- 《机器学习》(周志华) 第6章 支持向量 学习心得 笔记
- Deploy application delivery services in kubernetes (Part 1)
- 111. The sap ui5 fileuploader control realizes local file upload and encounters a cross domain access error when receiving the response from the server
- Strict mode -- let and const -- arrow function -- Deconstruction assignment -- string template symbol -- set and map -- generator function
- POJ3275 Ranking the Cows题解
- Holes in [apue] files
猜你喜欢

Countdown 2 days! 2022 China Computing Conference: Mobile cloud invites you to meet with computing network for innovative development
![[basic course of flight control development 7] crazy shell · open source formation UAV SPI (barometer data acquisition)](/img/ad/e0bc488c238a260768f7e7faec87d0.png)
[basic course of flight control development 7] crazy shell · open source formation UAV SPI (barometer data acquisition)
![[security] read rfc6749 and understand the authorization code mode under oauth2.0](/img/dc/e6d8626195b2e09a6c06050a9b552e.jpg)
[security] read rfc6749 and understand the authorization code mode under oauth2.0

The strongest distributed locking tool: redisson

安全保障基于软件全生命周期-PSP应用

30天刷题训练(一)

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

7. Dependency injection

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

What is the reason why the words behind word disappear when typing? How to solve it?
随机推荐
R语言使用dpois函数生成泊松分布密度数据、使用plot函数可视化泊松分布密度数据(Poisson distribution)
Istio四之故障注入和链路追踪
How to play a data mining game entry Edition
leetcode(442)数组中重复的数据
[Architecture] reading notes of three micro service books with high scores
Debezium series: major changes and new features of 2.0.0.beta1
R语言ggplot2可视化:可视化散点图并为散点图中的数据点添加文本标签、使用ggrepel包的geom_text_repel函数避免数据点标签互相重叠(自定义指定字体类型font family)
使用 Fail2ban 保护 Web 服务器免受 DDoS 攻击
记一次COOKIE的伪造登录
leetcode-深度优先与广度优先遍历
No swagger, what do I use?
长封闭期私募产品再现 业内人士看法各异
Postgresql14安装及主从配置
Understanding of stack and practical application scenarios
UVA1599理想路径题解
Long closed period private placement products reappearance industry insiders have different views
Intersectionobserver
SLAM论文合集
a标签_文件下载(download属性)
R语言检验样本比例:使用prop.test函数执行单样本比例检验计算总体中成功样本比例p值的置信区间(设置conf.level参数指定置信水平、置信区间的大小)