当前位置:网站首页>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 !
边栏推荐
- Continuous modification of business scenario functions
- leetcode494,474
- Réseau graphique: Qu'est - ce que le Protocole d'équilibrage de charge de passerelle glbp?
- 业务场景功能的继续修改
- Summer challenge brings you to play harmoniyos multi terminal piano performance
- ORB(Oriented FAST and Rotated BRIEF)
- 2022.07.03 (LC 6108 decryption message)
- 2022.07.03(LC_6109_知道秘密的人数)
- 企业应用业务场景,功能添加和修改C#源码
- Application of multi loop instrument in base station "switching to direct"
猜你喜欢
Microservice
Fast parsing intranet penetration helps enterprises quickly achieve collaborative office
Detailed explanation of openharmony resource management
OpenHarmony资源管理详解
js如何实现数组转树
Illustrated network: what is gateway load balancing protocol GLBP?
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
初识ROS
[IELTS reading] Wang Xiwei reading P3 (heading)
abc 258 G - Triangle(bitset)
随机推荐
Nine Qi single chip microcomputer ny8b062d single key control four LED States
What did I pay for it transfer to testing post from confusion to firmness?
积分商城游戏设置的基本要点
NPM install error forced installation
Ap8022 switching power supply small household appliances ACDC chip offline switching power supply IC
2022.07.03 (lc_6111_counts the number of ways to place houses)
abc 258 G - Triangle(bitset)
[paper reading] Tun det: a novel network for meridian ultra sound nodule detection
Multilingual Wikipedia website source code development part II
[error reporting] "typeerror: cannot read properties of undefined (reading 'split')“
2022.07.03(LC_6111_统计放置房子的方式数)
Tester's algorithm interview question - find mode
Face recognition 5- insight face padding code practice notes
22-07-02周总结
GDB常用命令
Every time I look at the interface documents of my colleagues, I get confused and have a lot of problems...
[paper reading] cavemix: a simple data augmentation method for brain vision segmentation
TS快速入门-函数
Date time type and format in MySQL
Go pit - no required module provides Package: go. Mod file not found in current directory or any parent