在codeblocks编译器下运行
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;
//非升序排序
void InsertSort(int a[],int n)
{
int i,j;
int key;
for(j=2; j<=n; j++)
{
key = a[j];
i = j-1;
while(i>0 && a[i]<key)
{
a[i+1] = a[i];
i--;
}
a[i+1] = key;
}
}
int main()
{
int a[99];
a[0] = -1;
srand(time(0)); //需添加#include<stdlib.h>,否则会报错
for(int i=1; i<99; i++)
{
a[i] = rand()%999; //随机生成数组并排序
}
InsertSort(a,98);
for(int i=1; i<99; i++)
{
cout << a[i] << ' ';
}
return 0;
}