把一个数插入到已经排好序的数组中去:
--------------------------------------------------------------------------------
#include<stdio.h>
#include<stdlib.h>
int sort(const void *a, const void *b)
{
return *(int *)a - *(int *)b;
}
int main()
{
int n, i, num, a[100];
scanf("%d",&n);
for (i = 0; i < n; i++)
scanf("%d",&a[i]);
qsort(a, n, sizeof(int), sort);
scanf("%d",&num);
int l = 0, r = n - 1, mid;
while (l <= r)
{
mid = (l + r) / 2;
if (num > a[mid])
l = mid + 1;
else
r = mid - 1;
}
for (i = n - 1; i >= l; i--)
a[i + 1] = a[i];
a[l] = num;
for(i = 0; i <= n; i++)
printf("%d ",a[i]);
return 0;
}
--------------------------------------------------------------------------------