求一个函数在有序数组中插数

来源:百度知道 编辑:UC知道 时间:2024/06/25 06:12:50
在一个已经赋好10个值的有序的从小到大排列的11个位置的数组中插入一个输入的数,要求插入过后原数组依然以升序排列,求能完成此项功能的函数

#include<stdio.h>
#define N 10
void main()
{
int i,j;
int a[N+1]={0},b;
printf("please input %d numbers from small to big:",N);
for(i=0;i<N;i++)scanf("%d",&a[i]);
printf("please input the number insert:");
scanf("%d",&b);
for(i=0,j=N;i<N;i++)
if(b<a[i])
{
j=i;
break;
}
for(i=N;i>j;i--)
a[i]=a[i-1];
a[j]=b;
printf("the result is:");
for(i=0;i<N+1;i++)printf("%d ",a[i]);
printf("\n");
}

/*运行结果:
please input 10 numbers from small to big:1 3 5 7 9 11 13 15 17 19
please input the number insert:8
the result is:1 3 5 7 8 9 11 13 15 17 19
*/

void Insert(int *array,int insertnum, int position=9 )
{
if (position==-1)
array[0]=insertnum;
else if (insertnum>=array[position])
array[position+1]=insertnum;
else