排序函数和查找函数
作者:互联网
//排序函数
void sort(char na[][20], int nu[], int n)
{
int i, j, t;
char temp[20];
for (i=0; i<n; i++)
for (j=i+1; j<n; nu[i]>nu[j] ? t=nu[j], nu[j]=nu[i], nu[i]=t, strcpy(temp, na[j]), strcpy(na[j], na[i]), strcpy(na[i], temp), j++ : j++);
for (i=0; i<n; printf("%d %s\n", nu[i], na[i]), i++);
}
//查找函数
void search(int numb, char na[][20], int nu[], int n)
{
int mid, low=0, high=n-1;
mid=(low+high)/2;
for (low=0, high=n-1, mid=(low+high)/2; low<=high; mid=(low+high)/2){
if (numb<nu[mid]) high=mid-1;
if (numb>nu[mid]) low=mid+1;
if (numb==nu[mid]){
printf(“Name: %s\n”, na[mid]);
break;
}
}
if (numb!=nu[mid]) printf(“No Match!\n”);
}
标签:函数,int,na,mid,查找,low,numb,排序,nu 来源: https://blog.csdn.net/qq_45415401/article/details/97812836