ACC==1升序,ACC==-1降序
#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct{ int NO; int Age; char Name[50]; }Student; typedef struct{ int StudentCount; Student *data; }Sqlist; void Binsersort(Sqlist &L,int ACC) { int i,j,k,low,high,mid; if(ACC==1) //升序 { for(i=1;i<L.StudentCount;i++) { k=L.data[i].NO; j=i-1; low=0; high=i-1; while(low<=high) { mid=(low+high)/2; if(L.data[mid].NO>k) high=mid-1; else low=mid+1; } for(j=i-1;j>=low;j--) L.data[j+1].NO=L.data[j].NO; L.data[low].NO=k; } } if(ACC==-1) //降序 { for(i=1;i<L.StudentCount;i++) { k=L.data[i].NO; j=i-1; low=0; high=i-1; while(low<=high) { mid=(low+high)/2; if(L.data[mid].NO<k) high=mid-1; else low=mid+1; } for(j=i-1;j>=low;j--) L.data[j+1].NO=L.data[j].NO; L.data[low].NO=k; } } } int main() { Sqlist L; L.StudentCount=4; L.data = (Student *)malloc(L.StudentCount * sizeof(Student)); L.data[0].Age=20; strcpy(L.data[0].Name,"zhangsan"); L.data[0].NO=1115; L.data[1].Age=20; strcpy(L.data[1].Name,"lisi"); L.data[1].NO=1112; L.data[2].Age=20; strcpy(L.data[2].Name,"wanger"); L.data[2].NO=1113; L.data[3].Age=20; strcpy(L.data[3].Name,"mazi"); L.data[3].NO=1114; Binsersort(L,-1); for (int i = 0; i < L.StudentCount; i++) { printf("NO: %d, Age: %d, Name: %s\n", L.data[i].NO, L.data[i].Age, L.data[i].Name); } return 0; }
标签:折半,Name,NO,int,插入排序,low,data,Age From: https://www.cnblogs.com/simpleset/p/17871561.html