#include
#include
void Sort(int 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(int a[],int n,int num) {
int mid,start = 0;
int end = n - 1;
if(a[start] == num) return start;
if(a[end] == num) return end;
while(start < end) {
mid = (start + end)/2;
if(a[mid] == num) return mid;
else if(a[mid] > num) end = mid - 1;
else start = mid + 1;
}
return -1;
}
int main() {
int a[] = {3,6,9,4,8,7,21,12,23,24,25,2,92,10,11,19,5};
int pos,i,num = 5;
int n = sizeof(a)/sizeof(a[0]);
pos = BinFind(a,n,num);
printf("排序前:\n");
for(i = 0; i < n; ++i) printf("%d ",a[i]);
printf("\n");
if(pos >= 0) printf("s[%d] = %d\n",pos,num);
else printf("没找到数值%d,对不起。\n",num);
Sort(a,n);
printf("排序后:\n");
for(i = 0; i < n; ++i) printf("%d ",a[i]);
printf("\n");
pos = BinFind(a,n,8);
if(pos >= 0) printf("a[%d] = %d\n",pos,8);
else printf("没找到数值%d,对不起。\n",8);
return 0;
}