当前位置:网站首页>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 !
边栏推荐
- P3304 [sdoi2013] diameter (diameter of tree)
- Specification for fs4061a boost 8.4v charging IC chip and fs4061b boost 12.6V charging IC chip datasheet
- Identifiers and keywords
- Get to know ROS for the first time
- Kibana index, mapping, document operation
- Parsing of XML
- Oracle case: SMON rollback exception causes instance crash
- The waterfall flow layout demo2 (method 2) used by the uniapp wechat applet (copy and paste can be used without other processing)
- 初识ROS
- 图解网络:什么是网关负载均衡协议GLBP?
猜你喜欢
随机推荐
Verilog tutorial (11) initial block in Verilog
巩固表达式C# 案例简单变量运算
【雅思阅读】王希伟阅读P4(matching2段落信息配对题【困难】)
Detailed explanation of openharmony resource management
The company needs to be monitored. How do ZABBIX and Prometheus choose? That's the right choice!
跨域请求
GDB常用命令
Continuous modification of business scenario functions
积分商城游戏设置的基本要点
微服务(Microservice)那点事儿
Every time I look at the interface documents of my colleagues, I get confused and have a lot of problems...
2022.07.03 (LC 6109 number of people who know secrets)
兩個數相互替換
Get to know ROS for the first time
Netcore3.1 JSON web token Middleware
2022.07.03 (lc_6111_counts the number of ways to place houses)
初识ROS
人脸识别5- insight-face-paddle-代码实战笔记
(script) one click deployment of any version of redis - the way to build a dream
Go pit - no required module provides Package: go. Mod file not found in current directory or any parent


![[IELTS reading] Wang Xiwei reading P3 (heading)](/img/19/40564f2afc18fe3e34f218b7b44681.png)






