当前位置:网站首页>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
边栏推荐
- TS literacy method - Basic chapter
- Operator3-设计一个operator
- 解决跨越的几种方案
- After finishing, help autumn move, I wish you call it an offer harvester
- 【安全】 阅读 RFC6749 及理解 Oauth2.0 下的授权码模式
- 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
- 30天刷题计划(二)
- Poj1860 currency exchange solution
- Understanding of stack and practical application scenarios
- 要想组建敏捷团队,这些方法不可少
猜你喜欢

对“Image Denoising Using an Improved Generative Adversarial Network with Wasserstein Distance“的理解

30 day question brushing training (I)

strcmp、strstr、memcpy、memmove的实现

Implementation of StrCmp, strstr, memcpy, memmove

产品经理:岗位职责表

Denial of service DDoS Attacks

7. Dependency injection

30 day question brushing plan (IV)

最强分布式锁工具:Redisson

The strongest distributed locking tool: redisson
随机推荐
R language test sample proportion: use prop The test function performs the single sample proportion test to calculate the confidence interval of the p value of the successful sample proportion in the
Denial of service DDoS Attacks
Poj3268 shortest path solution
SAP ui5 fileuploader control realizes local file upload, and trial version of cross domain access error encountered when receiving server-side response
30 day question brushing training (I)
To build agile teams, these methods are indispensable
SQL daily practice (Niuke new question bank) - day 4: advanced operators
最强分布式锁工具:Redisson
安全保障基于软件全生命周期-NetworkPolicy应用
Do you really know esmodule
在 Kubernetes 中部署应用交付服务(第 1 部分)
a标签_文件下载(download属性)
30 day question brushing plan (III)
Operator3 - design an operator
The domestic API management tool eolink is very easy to use, creating an efficient research and development tool
第六章 支持向量机
Remember to use pdfbox once to parse PDF and obtain the key data of PDF
How to play a data mining game entry Edition
性能超群!牛津&上海AI Lab&港大&商汤&清华强强联手,提出用于引用图像分割的语言感知视觉Transformer!代码已开源...
POJ3268最短路径题解