1
0
mirror of https://github.com/142vip/408CSFamily.git synced 2026-02-03 02:23:38 +08:00
Files
408CSFamily/code/ds/StraightInsertSort.cpp
喜欢芝士的妹妹 6d81d1706a feat: 修改代码
2023-08-30 22:12:32 +08:00

20 lines
498 B
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 直接插入排序【伪代码】
void straightInsertSort(ElemType A[], int n){
int i,j;
// 依次将前面的第2到第n个元素插入到前面的有序序列
for(i=2;i<=n;i++){
if(A[i].key< A[i-1].key){
// 哨兵元素
A[0]=A[i];
// 循环向后挪动
for(j=i-1;A[0].key<A[j].key;--j){
A[j+1]=A[j]
}
// 哨兵元素插入注意这里为j+1因为--j等循环完先递减再使用比预想靠后
A[j+1]=A[0]
}
}
}