脑图
相关代码
#include <stdio.h>
#define MAXLEN 10
void InsertSort(int A[]){
int i,j,temp;
for (int i = 1; i < MAXLEN; ++i) {
if(A[i]<A[i-1]){
temp = A[i];
for(j=i-1;j>=0&&A[j]>temp;--j)
A[j+1]=A[j];
A[j+1]=temp;
}
}
}
void InsertSortSB(int A[],int n){
int i,j;
for (int i = 2; i < n; ++i) {
if(A[i]<A[i-1]){
A[0] = A[i];
for (j=i-1;A[0]<A[j];--j)
A[j+1]= A[j];
A[j+1] = A[0];
}
}
}
int main() {
int arr1[MAXLEN]={9,8,7,6,5,4,3,2,1,0};
int arr2[MAXLEN]={0,4,5,6,7,2,1,3,8,9};
InsertSort(arr1);
InsertSortSB(arr2,MAXLEN);
printf("直插法排序\n");
for (int i = 0; i < MAXLEN; ++i) {
printf("%d",arr1[i]);
}
printf("\n带哨兵的直插法排序\n");
for (int i = 1; i < MAXLEN; ++i) {
printf("%d",arr2[i]);
}
return 0;
}
|