当前位置:网站首页>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 !
边栏推荐
- "Xiaodeng" domain password policy enhancer in operation and maintenance
- abc 258 G - Triangle(bitset)
- The company needs to be monitored. How do ZABBIX and Prometheus choose? That's the right choice!
- 【雅思阅读】王希伟阅读P3(Heading)
- JS convert pseudo array to array
- Fast analysis -- easy to use intranet security software
- Skills in analyzing the trend chart of London Silver
- 【报错】 “TypeError: Cannot read properties of undefined (reading ‘split‘)“
- Significance of acrel EMS integrated energy efficiency platform in campus construction
- JS how to realize array to tree
猜你喜欢

Consolidated expression C case simple variable operation

【雅思阅读】王希伟阅读P4(matching2段落信息配对题【困难】)

企业公司项目开发好一部分基础功能,重要的事保存到线上第一a

skimage: imread & imsave & imshow

兩個數相互替換
![[IELTS reading] Wang Xiwei reads P4 (matching2 paragraph information matching question [difficult])](/img/83/63296108b47eda37c19b9ff9deb5ec.png)
[IELTS reading] Wang Xiwei reads P4 (matching2 paragraph information matching question [difficult])
![[IELTS reading] Wang Xiwei reading P4 (matching1)](/img/91/1b3f85410035f65acb0c205185f698.png)
[IELTS reading] Wang Xiwei reading P4 (matching1)
![[论文阅读] TUN-Det: A Novel Network for Thyroid Ultrasound Nodule Detection](/img/25/e2366cabf00e55664d16455a6049e0.png)
[论文阅读] TUN-Det: A Novel Network for Thyroid Ultrasound Nodule Detection

ORB(Oriented FAST and Rotated BRIEF)

【C】(笔试题)指针与数组,指针
随机推荐
leetcode494,474
Life is changeable, and the large intestine covers the small intestine. This time, I can really go home to see my daughter-in-law...
城市轨道交通站应急照明疏散指示系统设计
Design of emergency lighting evacuation indication system for urban rail transit station
Robot reinforcement learning synergies between pushing and grassing with self supervised DRL (2018)
[IELTS reading] Wang Xiwei reading P3 (heading)
ORB(Oriented FAST and Rotated BRIEF)
Consolidated expression C case simple variable operation
[monitoring] ZABBIX
GDB common commands
2022.07.03(LC_6108_解密消息)
2022.07.03 (LC 6108 decryption message)
[selenium automation] common notes
Best practice case of enterprise digital transformation: introduction and reference of cloud based digital platform system security measures
【C】(笔试题)指针与数组,指针
OpenHarmony资源管理详解
XML的解析
Paper notes multi UAV collaborative monolithic slam
"Xiaodeng" domain password policy enhancer in operation and maintenance
Five papers recommended for the new development of convolutional neural network in deep learning