当前位置:网站首页>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 !
边栏推荐
猜你喜欢

Two numbers replace each other

leetcode518,377

基本放大电路的学习

What did I pay for it transfer to testing post from confusion to firmness?

如何有效对直流列头柜进行监测

他做国外LEAD,用了一年时间,把所有房贷都还清了

Oracle case: SMON rollback exception causes instance crash

XML的解析

城市轨道交通站应急照明疏散指示系统设计

Application of multi loop instrument in base station "switching to direct"
随机推荐
分布式BASE理论
打新债开户注册安全吗?有没有风险的?靠谱吗?
人脸识别5- insight-face-paddle-代码实战笔记
Hisilicon 3559 universal platform construction: YUV422 pit stepping record
[paper reading] cavemix: a simple data augmentation method for brain vision segmentation
Some basic functions of enterprise projects are developed, and important things are saved to online first a
PermissionError: [Errno 13] Permission denied: ‘data. csv‘
Upload avatar on uniapp
JS convert pseudo array to array
【C】(笔试题)指针与数组,指针
Leetcode70 (Advanced), 322
Distributed base theory
lambda表达式
公司要上监控,Zabbix 和 Prometheus 怎么选?这么选准没错!
2022.07.03(LC_6109_知道秘密的人数)
[论文阅读] TUN-Det: A Novel Network for Thyroid Ultrasound Nodule Detection
C语言中sizeof操作符的坑
巩固表达式C# 案例简单变量运算
Illustrated network: what is gateway load balancing protocol GLBP?
IELTS examination process, what to pay attention to and how to review?