当前位置:网站首页>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 !
边栏推荐
- 实战模拟│JWT 登录认证
- 《论文笔记》Multi-UAV Collaborative Monocular SLAM
- Face recognition 5- insight face padding code practice notes
- 图解网络:什么是网关负载均衡协议GLBP?
- 人脸识别5- insight-face-paddle-代码实战笔记
- URL和URI
- 人生无常,大肠包小肠, 这次真的可以回家看媳妇去了。。。
- He worked as a foreign lead and paid off all the housing loans in a year
- What is the difference between port mapping and port forwarding
- Is it safe to open and register new bonds? Is there any risk? Is it reliable?
猜你喜欢
他做国外LEAD,用了一年时间,把所有房贷都还清了
Detailed explanation of openharmony resource management
P3304 [SDOI2013]直径(树的直径)
【雅思阅读】王希伟阅读P3(Heading)
基于三维gis平台的消防系统运用
人脸识别5- insight-face-paddle-代码实战笔记
JS how to realize array to tree
Continuous modification of business scenario functions
uniapp微信小程序拿来即用的瀑布流布局demo2(方法二)(复制粘贴即可使用,无需做其他处理)
Significance of acrel EMS integrated energy efficiency platform in campus construction
随机推荐
【雅思阅读】王希伟阅读P4(matching1)
How to avoid arc generation—— Aafd fault arc detector solves the problem for you
Acrel-EMS综合能效平台在校园建设的意义
【报错】 “TypeError: Cannot read properties of undefined (reading ‘split‘)“
【雅思阅读】王希伟阅读P3(Heading)
Fast parsing intranet penetration helps enterprises quickly achieve collaborative office
JS convert pseudo array to array
微服务(Microservice)那点事儿
多回路仪表在基站“转改直”方面的应用
【雅思阅读】王希伟阅读P4(matching2段落信息配对题【困难】)
Verilog tutorial (11) initial block in Verilog
P4281 [AHOI2008]紧急集合 / 聚会(LCA)
Cross domain request
企业应用业务场景,功能添加和修改C#源码
2022.07.03(LC_6111_统计放置房子的方式数)
业务场景功能的继续修改
基本放大电路的学习
兩個數相互替換
P4408 [NOI2003] 逃学的小孩(树的直径)
"Xiaodeng" domain password policy enhancer in operation and maintenance