当前位置:网站首页>Insert sort
Insert sort
2022-06-12 21:05:00 【Bald and weak】
Insertion sort :( Small ones go forward )
thought :
Disordered sequence : 4 2 3 5 7 1 9 2 0
i j //i、j Corresponding to the number pointing above ( If you don't know where the arrow symbol is, you don't draw it )
temp=2(j) 4>2,4 Move backward j++
2 4 3 5 7 1 9 2 0 // The red Is sorted
i j //i It was orderly
temp=3 3<4,4 Assign values to the 3 The location of i--
2 4 4 5 7 1 9 2 0
i j
2<3, Don't swap 3->i+1 The location of j++,i->j-1
2 3 4 5 7 1 9 2 0
i j
temp=5 (i Value )4<5,i There is no need to compare the front j++,i->j-1;
2 3 4 5 7 1 9 2 0
i j
temp=7; 5<7, No more exchanges
2 3 4 5 7 1 9 2 0
i j
temp=1; 7>1; 7->j+1 Location i--;
.... 2>1; 5->i+1 Location
i-1=-1<0;// Stop conditions
temp->i+1 j++;i->j-1
1 2 3 4 5 7 9 2 0
i j
.....
0 1 2 2 3 4 5 7 9
Code implementation :
// Insertion sort
int InsertSort(int arr,int length) {
for (int j = 1; j < length; j++)
{
int i = j - 1;
int temp = arr[j];
for (;arr[i] >temp && i>=0;i--) //arr[i] >arr[j] error , Every time j Everything has changed
{
arr[i+1] = arr[i];
}//i When I quit -1 Or the current arr[i]>arr[j]
//if(arr[i] <= arr[j] || i = 0)
arr[i+1] = temp;
}
}
int main() {
int arr[] = { 4,2,3,5,7,1,9,2,0 };
InsertSort(arr, sizeof(arr) / sizeof(arr[0]));
return 0;
}
边栏推荐
- torch. nn. Linear() function
- Typescript definition type: type 'timeout' cannot be assigned to type 'number';
- Let Google browser fofa plug-in come alive
- nn. PReLU(planes)
- 机器学习资料汇总
- JS中如何实现重载
- In the spring recruitment of 2022, the test engineer will have a full set of interview strategies to thoroughly understand all the technical stacks (all dry goods)
- Cv2.lut() (populates the output array with values from the lookup table)
- 重排数列练习题
- 同花顺能开户吗,同花顺在APP上可以直接开通券商安全吗 ,买股票怎么网上开户
猜你喜欢
Solution of multi machine room dynamic loop status network touch screen monitoring
Algorinote_ 2_ Main theorem and Akra bazzi theorem
Is it really possible to find a testing job with a monthly income of more than 10000 without a degree and self-study software testing?
Teamwork collaboration application experience sharing | community essay solicitation
Teambition 协作应用心得分享|社区征文
The year of the outbreak of financial innovation! All dtinsight products of kangaroo cloud data stack have passed the special test of Xinchuang of ICT Institute
Algorinote_2_主定理与 Akra-Bazzi 定理
中小型机房动力环境综合监控解决方案
Nexus3 build local warehouse
String Basics
随机推荐
test
Data visualization - broken line area chart
Social metauniverse: start from redefining yourself
Gather function in pytorch_
同花顺能开户吗,同花顺在APP上可以直接开通券商安全吗 ,买股票怎么网上开户
leetcode:207. 课程表
Junda technology is applicable to "kestar" intelligent precision air conditioning network monitoring
最简单ALV模板
MySQL + PostgreSQL batch insert update insertorupdate
Understanding of functions
JS深浅拷贝
What's a good gift for the goddess Festival? Gift recommendation for the goddess Festival on March 8
China hydraulic cylinder linear position sensor market trend report, technical dynamic innovation and market forecast
The required books for software testers (with e-books) recommended by senior Ali have benefited me a lot
好数对的求解
Bluetooth for Delphi xe7
JSON file handles object Tags
At the same time, do the test. Others have been paid 20W a year. Why are you still working hard to reach 10K a month?
Minio client (MC command) implements data migration
Large and small end conversion