当前位置:网站首页>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 !
边栏推荐
- Huawei employs data management experts with an annual salary of 2million! The 100 billion market behind it deserves attention
- How to avoid arc generation—— Aafd fault arc detector solves the problem for you
- Face recognition 5- insight face padding code practice notes
- Enterprise application business scenarios, function addition and modification of C source code
- Five papers recommended for the new development of convolutional neural network in deep learning
- URLs and URIs
- (script) one click deployment of any version of redis - the way to build a dream
- Operator explanation
- Actual combat simulation │ JWT login authentication
- Significance of acrel EMS integrated energy efficiency platform in campus construction
猜你喜欢

【雅思阅读】王希伟阅读P3(Heading)

基本放大电路的学习

abc 258 G - Triangle(bitset)

Consolidated expression C case simple variable operation

圖解網絡:什麼是網關負載均衡協議GLBP?

Fast analysis -- easy to use intranet security software

Operator explanation

Deux nombres se remplacent

Some basic functions of enterprise projects are developed, and important things are saved to online first a

2022.07.03(LC_6111_统计放置房子的方式数)
随机推荐
The waterfall flow layout demo2 (method 2) used by the uniapp wechat applet (copy and paste can be used without other processing)
How to use fast parsing to make IOT cloud platform
Netcore3.1 JSON web token Middleware
URLs and URIs
Summer challenge brings you to play harmoniyos multi terminal piano performance
如何避免电弧产生?—— AAFD故障电弧探测器为您解决
挖财学院开户安全的吗?开户怎么开?
图解网络:什么是网关负载均衡协议GLBP?
If you open an account of Huatai Securities by stock speculation, is it safe to open an account online?
多回路仪表在基站“转改直”方面的应用
AcWing164. 可达性统计(拓扑排序+bitset)
ORB(Oriented FAST and Rotated BRIEF)
【雅思阅读】王希伟阅读P3(Heading)
[论文阅读] CarveMix: A Simple Data Augmentation Method for Brain Lesion Segmentation
lambda表达式
Nine Qi single chip microcomputer ny8b062d single key control four LED States
2022.07.03(LC_6108_解密消息)
Detailed explanation of openharmony resource management
PyTorch: In-place Operation
How to avoid arc generation—— Aafd fault arc detector solves the problem for you