7-6 simplified insertion sort (15 points)
This problem requires programming, a given integer into the originally ordered sequence of integers, so that the results are still ordered sequence.
Input format:
Enter the first line gives the first non-negative integer N (<10); a second line gives the N integers from small to large in good order; and a third row gives the integer X.
Output format:
the output will remain X inserted ascending ordered sequence of integers in a row, behind each number has a space.
Sample input:
. 5
. 1 2. 4. 5. 7
. 3
Output Sample:
123457
#include<iostream>
using namespace std;
void input(int a[],int n);
int main()
{
int n,i,j,x,a[11];
cin>>n;
input(a,n); //输入n个数到数组a中
cin>>x; //输入插入的数
i=0;
while(a[i]<x && i<n)
i++; //右移
for (j=n-1;j>=i;j--)
{
a[j+1]=a[j]; //左移
}
a[i]=x; //判断插入的数在哪位
for(i=0;i<n+1;i++)
cout<<a[i]<<" ";
return 0;
}
void input(int a[],int n)
{
for(int i=0;i<n;i++)
cin>>a[i];
}