# 写一个函数,对任意一维数组进行降序排序

<p>在主函数中随机生成一有n个元素的一维数组(元素的取值范围载10-90之间),调用排序函数对该数组进行排序,并输出排序结果。</p>

以下程序实现了这一功能，请你填补空白处的内容：

```cpp
#include <stdio.h>
#include <random>
#include <iostream>
#include <string>
#include <time.h>
using namespace std;
void InsertSort(int a[], int n)
{
    for (int i = 1; i < n; i++)
    {
        if (a[i] < a[i - 1])
        {
            int j = i - 1;
            int x = a[i];
			________________;
        }
    }
}
int main()
{
    cout << "请输入产生多个随机数：";
    int ranNum[100], num = 0;
    srand((unsigned)time(NULL));
    cin >> num;
    cout << "随机数组：";
    for (int i = 0; i < num; i++)
    {
        ranNum[i] = 10 + (rand() % 80);
        cout << " " << ranNum[i];
    }
    cout << endl;
    InsertSort(ranNum, num);
    cout << "排序后整数序列：";
    for (int j = num - 1; j >= 0; j--)
    {
        cout << ranNum[j] << " ";
    }
    cout << endl;
    system("pause");
    return 0;
}
```

## template

```cpp
#include <stdio.h>
#include <random>
#include <iostream>
#include <string>
#include <time.h>
using namespace std;
void InsertSort(int a[], int n)
{
    for (int i = 1; i < n; i++)
    {
        if (a[i] < a[i - 1])
        {
            int j = i - 1;
            int x = a[i];
			a[i] = a[i - 1];
			while (x < a[j])
			{
				a[j + 1] = a[j];
				j--;
			}
			a[j + 1] = x;
        }
    }
}
int main()
{
    cout << "请输入产生多个随机数：";
    int ranNum[100], num = 0;
    srand((unsigned)time(NULL));
    cin >> num;
    cout << "随机数组：";
    for (int i = 0; i < num; i++)
    {
        ranNum[i] = 10 + (rand() % 80);
        cout << " " << ranNum[i];
    }
    cout << endl;
    InsertSort(ranNum, num);
    cout << "排序后整数序列：";
    for (int j = num - 1; j >= 0; j--)
    {
        cout << ranNum[j] << " ";
    }
    cout << endl;
    system("pause");
    return 0;
}
```

## 答案

```cpp
a[i] = a[i - 1];
while (x < a[j])
{
	a[j + 1] = a[j];
	j--;
}
a[j + 1] = x;
```

## 选项

### A

```cpp
a[i] = a[i - 1];
while (x < a[j])
{
	a[j + 1] = a[j];
	j++;
}
a[j + 1] = x;
```

### B

```cpp
a[i] = a[i - 1];
while (x < a[j])
{
	a[j - 1] = a[j];
	j--;
}
a[j - 1] = x;
```

### C

```cpp
a[i] = a[i - 1];
while (x < a[j])
{
	a[j - 1] = a[j];
	j++;
}
a[j + 1] = x;
```