C/C++ code#include
#include
#include
void sort(int arr[], int n);
int main(int argc, char* argv[])
{
int n1,n2,x,i;
int A[20];
for(i=0;i<=9;i++)
scanf("%d",&A[i]);
sort(A, 10);
printf("排序后..\n");
for(i=0;i<=9;i++) {
printf("%d ",A[i]);
}
printf("\n");
printf("输入要插入的元素: ");
scanf("%d",&x);
n1=0;
n2=i-1;
//for(;n1!=n2;)
for(;n1<=n2;)
{
if(n2-n1+1==0) {
printf("%d",i);
}
else
{
if(x>=A[(n1+n2)/2])
n1=(n1+n2)/2+1;
else
n2=(n1+n2)/2-1;
}
}
//for(i=n1;i<=10;i++)
for(i = 9; i >= n1; i--)
{
A[i+1]=A[i];
//A[i]=x;
}
A[n1] = x;
printf("插入后: \n");
for(i=0;i<=10;i++)
printf("%d ",A[i]);
printf("\n");
return 0;
}
void sort(int arr[], int n)
{
int i, j, k;
int temp;
for(i = 0; i < n-1; i++) {
k = i;
for(j = i + 1; j < n; j++) {
if(arr[k] > arr[j]) {
k = j;
}
}
if(k != i) {
temp = arr[k];
arr[k] = arr[i];
arr[i] = temp;
}
}
}