算法代码(1)——插入排序

#include

#include<stdlib.h>

#include<time.h>

using namespace std;

#define MaxLength 1000

template<class TypeName>

void InsertionSort(TypeName A[], int length)

{

    int i,j;

TypeName key;

for(j=1; j<length; j++)

{

key = A[j];

i = j-1;

while( i>=0 && A[i]>key)

{

A[i+1] = A[i];

i–;

}

A[i+1] = key;

}

}

void main()

{

    srand(time(NULL));

int A[MaxLength];

for(int i=0; i<15; i++)

{

 A[i] = rand()%100;

 cout<<A[i]<<endl;

}

    InsertionSort(A,15);

cout<<“After sort:”<<endl;

for(i=0; i<15; i++)

cout<<A[i]<<endl;

}

点赞