![](https://video.ask-data.xyz/img.php?b=https://iknow-pic.cdn.bcebos.com/63d0f703918fa0ec0a8c73f42d9759ee3c6ddbfb?x-bce-process=image%2Fresize%2Cm_lfit%2Cw_600%2Ch_800%2Climit_1%2Fquality%2Cq_85%2Fformat%2Cf_auto)
#include <stdio.h>
#define N 10
typedef int datatype;
void Sort(datatype a[],int n) {
int i,j,k;
int num;
for(i = 0; i < n - 1; ++i) {
k = i;
for(j = i + 1; j < n; ++j)
if(a[k] < a[j]) k = j;
if(k != i) {
num = a[k];
a[k] = a[i];
a[i] = num;
}
}
}
int BinFind(datatype a[],int n,datatype num) {
int mid,start = 0;
int end = n - 1;
while(start <= end) {
mid = (start + end)/2;
if(a[mid] == num) return mid;
else if(a[mid] > num) start = mid + 1;
else end = mid - 1;
}
return -1;
}
int main() {
datatype a[N],num;
int pos,i,n;
printf("输入%d整数(空格隔开):",N);
for(n = 0; n < N; ++n) scanf("%d",&a[n]);
printf("排序前:\n");
for(i = 0; i < n; ++i) printf("%d ",a[i]);
printf("\n");
Sort(a,n);
printf("排序后:\n");
for(i = 0; i < n; ++i) printf("%d ",a[i]);
printf("\n");
printf("输入你要查找的数:");
scanf("%d",&num);
pos = BinFind(a,n,num);
if(pos >= 0) printf("a[%d] = %d\n",pos,num);
else printf("没找到数值%d,对不起。\n",num);
return 0;
}