向数组中插入一个数,保持数组内数字从小到大排列:
#include <stdio.h>
int main(){
int arr[100] = {2,5,8,15,40};
int num = 0 , i = 0 , j=0 , count = 5;
while(scanf("%d",&num)){
for (i = 0; i < count; ++i)
{
if (num < arr[i])
{
for (j = count ; j > i ; --j)
{
arr[j] = arr[j-1];
}
break;
}
}
arr[i] = num;
++count;
for(i = 0; i < count; ++i)
{
printf("%d ", arr[i]);
}
}
return 0;
}