C语言中在100到999中随机选择100个数,用插入排序从小到大排列

如题所述

第1个回答  2013-11-19
下面是完整程序。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void InsertSort(int *a, int N)
{
int itemp = 0;
int i,j, t;
for (i = 1;i < N;i++)
{
itemp = i;
for (j = i-1; a[itemp] < a[j] && j >= 0;j--,itemp--)
{
t = a[itemp]; a[itemp]=a[j];a[j]=t;
}
}
}

main(){
int a[100];
int i,N;
N=100;
srand(time(NULL));
for (i=0;i<N;i++) { a[i] = rand()%901+99; if (a[i] < 100) a[i]++;}
InsertSort(a, N);
for (i=0;i<N;i++) printf("%d ",a[i]);
return 0;
}
相似回答