当前位置:网站首页>Insert sort of sort
Insert sort of sort
2022-07-05 00:28:00 【Building Zzzz】
Catalog
One 、 About direct insertion sorting
Two 、 Specific code implementation
3、 ... and 、 Time complexity , Space complexity and stability
Preface
We often encounter some problems with arrays , Let's deal with various sequence problems , Often, when it comes to sorting , I feel like I can't do it , Mainly because it is too strange to sort ( The unknown is the most terrifying ), The so-called sorting is to make a series of numbers use various methods to order , Whether ascending or descending , So I sort out some sort based on comparison , Quick sort , Heap sort , Bubble sort , Shell Sort …… Today's protagonist is direct insertion sorting .
One 、 About direct insertion sorting
Direct insert sort is insert , It's like jumping in line , And if you want to insert, first you have to have something to insert , So you can take out one element in order , Then compare it with other elements , Just make it orderly .
Two 、 Specific code implementation
public class Insert {
public static void insertSort(int[] array){
for (int i = 1; i < array.length; i++) {
int j = i - 1;
int tmp = array[i];
for (; j >= 0; j--) {
if(array[j] > tmp){
array[j + 1] = array[j];
}else{
break;
}
}
array[j + 1] = tmp;//j Back to less than 0 The place of
}
}
}
3、 ... and 、 Time complexity , Space complexity and stability
The space complexity is O(1): No more use of extra space
The time complexity is O(N^2): In the worst case, change it every time, that is N*N The best case is when it is orderly O(N)( So the data More orderly , Faster )
stability : Stable ( If when judging array[j] > tmp When you add an equal sign, it becomes unstable , A stable sort can To become unstable, but an unstable sort cannot become stable )
summary
Insert sort is one of the few stable sorts in common sort , But the insertion sorting time is too complicated , Insert sort is not often used, but the more ordered the insert sort is, the faster it will be. Therefore, it can be used in the optimization of other sorts , And direct insertion sorting is also relatively simple , After learning, you can prepare for other sorting learning !
边栏推荐
- leetcode518,377
- ORB(Oriented FAST and Rotated BRIEF)
- [IELTS reading] Wang Xiwei reading P3 (heading)
- P4408 [NOI2003] 逃学的小孩(树的直径)
- Go step on the pit - no required module provides package: go mod file not found in current directory or any parent
- Using fast parsing intranet penetration to realize zero cost self built website
- [selenium automation] common notes
- Application of multi loop instrument in base station "switching to direct"
- 图解网络:什么是网关负载均衡协议GLBP?
- ORB(Oriented FAST and Rotated BRIEF)
猜你喜欢
uniapp微信小程序拿来即用的瀑布流布局demo2(方法二)(复制粘贴即可使用,无需做其他处理)
Réseau graphique: Qu'est - ce que le Protocole d'équilibrage de charge de passerelle glbp?
同事的接口文档我每次看着就头大,毛病多多。。。
Two numbers replace each other
What did I pay for it transfer to testing post from confusion to firmness?
Fast parsing intranet penetration helps enterprises quickly achieve collaborative office
JS how to realize array to tree
分布式BASE理论
[path planning] RRT adds dynamic model for trajectory planning
URL和URI
随机推荐
【C】(笔试题)指针与数组,指针
A new method for analyzing the trend chart of London Silver
If you open an account of Huatai Securities by stock speculation, is it safe to open an account online?
Data on the number of functional divisions of national wetland parks in Qinghai Province, data on the distribution of wetlands and marshes across the country, and natural reserves in provinces, cities
多回路仪表在基站“转改直”方面的应用
[paper reading] cavemix: a simple data augmentation method for brain vision segmentation
挖财学院开户安全的吗?开户怎么开?
"Xiaodeng" domain password policy enhancer in operation and maintenance
URL和URI
两个数相互替换
巩固表达式C# 案例简单变量运算
Verilog tutorial (11) initial block in Verilog
P4281 [AHOI2008]紧急集合 / 聚会(LCA)
Best practice case of enterprise digital transformation: introduction and reference of cloud based digital platform system security measures
Advanced template
Tester's algorithm interview question - find mode
Continuous modification of business scenario functions
Application of multi loop instrument in base station "switching to direct"
The company needs to be monitored. How do ZABBIX and Prometheus choose? That's the right choice!
P3304 [sdoi2013] diameter (diameter of tree)