#定義N ^ 5/*數組的大小*/
void input(int * a,int n)/* input函數*/
{
int * p = a;
while(p & lt;a+n)
scanf(“% d“,p++);
}
void output(int * a,int n)/*輸出函數*/
{
int * p = a;
while(p & lt;a+n)
printf(“% d“,* p++);
printf(" \ n ");
}
void isort(int * s,int n)/*排序函數*/
{
int i,j;
int *p,temp
for(I = 0;我& ltn-1;i++)
{
p = s+I;
for(j = I;j & ltn;j++)
if(* p & gt;s【j】)
p = s+j;
temp = * p;
* p = s【I】;
s【I】= temp;
}
}
int binsh(int * a,int c,int low,int high)/*遞歸二分搜索法方法。如果元素存在於中,則返回元素的第壹個位置,否則返回-1*/
{
int mid=(高+低)/2;
if(low & gt;高)
返回-1;
if(c & gt;a【中】
返回binsh(a,c,mid+1,high);
else if(c & lt;a【中】
return binsh(a,c,低,中1);
其他
返回mid
}
int main()
{
int a【N】,N;
input(a,N);/*輸入*/
isort(a,N);/*排序*/
輸出(a,N);/*輸出*/
scanf(“% d“,& ampn);/*要查找的元素*/
printf(“% d \ N“,binsh(a,N,0,N-1);/*打印位置(如果存在)或-1*/
返回0;
}
如果有用,請及時采納,謝謝。