输入一组数据,再输入正整数。按照升序的规律把这个数据重新排序,如果输入一个负数,就结束
#include<stdio.h>
int main()
{
int arr[100] = { 1,4,6,9,10 };
int m = 0;
int i = 0;
int j = 0;
int n = 5;
printf("请输入需要插入的数字:\n");
while (scanf("%d", &m) == 1)
{
if (m > 0)
{
arr[n] = m;
n++;
}
else
break;
}
printf("打印出未排序的所有的数字:\n");
for (i = 0; i < n; i++)
{
printf("%d ",arr[i]);
}
printf("\n");
int tmp = 0;
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
if (arr[i] > arr[j])
{
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
}
printf("打印出排序的所有数字:\n");
for (i = 0; i <n; i++)
{
printf("%d ",arr[i]);
}
printf("\n");
return 0;
}
输出结果: