代码是最好的文档【虽然很违心】
#include <stdio.h>
#define SIZE 10
//直接插入排序
void zhi_jie_cha_ru(int a[]) {
int temp;//标兵位
for(int i=1; i<SIZE; i++) {
temp = a[i];
int j;
for(j=i-1; temp<a[j]&&j>=0; j--) {
a[j+1]=a[j];
}
a[j+1]=temp;
}
for(int i=0; i<SIZE; i++) {
printf("%d ",a[i]);
}
}// 折半插入排序
void zhe_ban_cha_ru(int a[]) {
int temp;//标兵位
for(int i=1; i<SIZE; i++) {
int low =0,heigh=i-1;
temp=a[i];
while(low<=heigh) {
if(a[i]>a[(heigh+low)/2]) {
low = (heigh+low)/2+1;
} else {
heigh=(heigh+low)/2-1;
}
}
for(int j=i-1; j>heigh; j--)a[j+1]=a[j];
a[heigh+1]=temp;
}
for(int i=0; i<SIZE; i++) {
printf("%d ",a[i]);
}}
//2-路插入排序,构造静态循环链表
void lu_2_cha_ru(int a[]) {
int b[SIZE],temp;
int first=0,final=0;//标志位,first代表循环入口,final代表循环出口
b[0]=a[0];
for(int n = 1; n<SIZE; n++) {
temp=a[n];
int i=0;
if(a[n]>b[0]) {
for(i=final; temp<b[i]&&i>0; i--) {
b[i+1]=b[i];
}
b[i+1]=temp;
final ++;
} else {
if(first==0) {
first=SIZE-1;
b[first]=temp;
} else {
for(i=first; temp>b[i]&&i<SIZE; i++) {
b[i-1]=b[i];
}
b[i-1]=temp;
first --;
}
}
}
for(int i=0; i<SIZE; i++) {
printf("%d ",b[i]);
}
}int main() {
// int a[SIZE]= {100,56};
int a[SIZE]= {100,56,1,0,23,3,5,116,3,2};
// int a[SIZE]= {1,2,3,4,5,6,7,8,9,10};
// int a[SIZE]= {100,20,3,4};
// zhi_jie_cha_ru(a);
// printf("\n");
// zhe_ban_cha_ru(a);
lu_2_cha_ru(a);;
return 1;
}