当前位置:网站首页>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;
}
边栏推荐
- Introduction to the characteristics of balancer decentralized exchange market capitalization robot
- Junda technology is applicable to "kestar" intelligent precision air conditioning network monitoring
- torch. Finfo function
- torch. nn. Linear() function
- Typescript definition type: type 'timeout' cannot be assigned to type 'number';
- Nexus3搭建本地仓库
- torch. unique()
- Data visualization - Calendar chart
- InRelease: 由于没有公钥,无法验证下列签名: NO_PUBKEY EB3E94ADBE1229CF
- JS中如何实现重载
猜你喜欢

(11) Image frequency domain filtering with OpenCV

#141 Linked List Cycle

Composer version degradation
![Li Mu [practical machine learning] 1.4 data annotation](/img/e4/2593b1dec04476a9cc3b4af94dc189.jpg)
Li Mu [practical machine learning] 1.4 data annotation

Lintcode:127. Topology sorting

#113 Path Sum II

What's a good gift for the goddess Festival? Gift recommendation for the goddess Festival on March 8

Vs2017 environmental issues

结构体知识点all in

Before job hopping, Jin San made up the interview questions. Jin San successfully landed at Tencent and got a 30K test offer
随机推荐
Market trend report, technical innovation and market forecast of hydraulic chain hoist in China
pytorch transforms. Use of lambda
What did new do
初步了解认识正则表达式(Regex)
(11) Image frequency domain filtering with OpenCV
Draw according to weight
New product release Junda intelligent integrated environmental monitoring terminal
How can CTCM in the inspection lot system status of SAP QM be eliminated?
MySQL field truncation principle and source code analysis
做自媒体视频,友好的新媒体运营必备app分享
Solve the cvxpy error the solver GLPK_ MI is not installed
新品发布丨竣达智能综合环境监测终端
重排数列练习题
torch. unique()
Nexus3 build local warehouse
Data visualization - Calendar chart
Listener in JSP
Can tonghuashun open an account? Is it safe to open an account in tonghuashun
The required books for software testers (with e-books) recommended by senior Ali have benefited me a lot
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?