数据结构_内部排序_插入排序小练习
采用了两种不同的排序方法,各有优劣。
#include<iostream>
#include<ctime>
using namespace std;
#define ListLen 10
#define radombase 100
void print(int a[])
{
for(int i=0;i<ListLen;i++)
cout<<a[i]<<" ";
cout<<endl;
}/*
void InsertSort(int a[])
{
int temp;
for(int i=1;i<ListLen;i++)
{
int j=0;
while(j<i&&a[j]<a[i])
j++;
if(j<i)
{
temp=a[i];
for(int k=i-1;k>=j;k--)
a[k+1]=a[k];
a[j]=temp;
}
}
}*/
void InsertSort(int a[])
{
int temp,j;
for(int i=1;i<ListLen;i++)
{
if(a[i]<a[i-1])
{
temp=a[i];
j=i-1;
while(j>=0&&temp<a[j])
{
a[j+1]=a[j];
a[j]=temp;
j--;
}
}
}
}
void GetRandomNum(int a[])
{
srand((unsigned)time(NULL));
for(int i=0;i<ListLen;i++)
a[i]=rand()%radombase+1;
}
int main()
{
int a[ListLen];
GetRandomNum(a);
print(a);
InsertSort(a);
print(a);
system("pause");
return 0;
}