线性表(a1,a2,a3,...,an)中的元素递增有序且按顺序存储于计算机内。要求设计一个算法,用最少的时间在表中查找数值为x的元素,若找到,则将其与后继元素位置相交换,若找不到,则将其插入表中并使表中元素仍递增有序。
使用二分查找
cpp
void SearchExchangeInsert(int A[],int x){
int low=0,high=n-1,mid;
while(low<=high){
mid=(low+high)/2;
if(A[mid]==x)break;
else if(A[mid]<x) low=mid+1;
else high=mid-1;
}
//交换操作
if(A[mid]==x&&mid!=n-1){
t=A[mid];
A[mid]=A[mid+1];
A[mid+1]=t;
}
if(low>high){
for(i=n-1;i>high;i--) A[i+1]=A[i];
A[i+1]=x;
}
}




