/*
* 直接插入排序*/
#include <iostream>
using namespace std;
#define Size 6
typedef int DataType;
//声明
void PaintInsertSort(DataType const digit[]);
void InsertSort(DataType digit[]);
//Main
int main(void)
{
DataType digit[Size] = {64,5, 7, 89, 6, 24};
PaintInsertSort(digit);
InsertSort(digit);
PaintInsertSort(digit);
return 0;
}
void InsertSort(DataType digit[])
{
cout<<" After InsertSort..."<<endl;
DataType temp;
int i;
int j;
//注意 i 从 1 开始
for(i = 1; i < Size; i++)
{
temp = digit[i];
j = i;
while(j > 0 && temp < digit[j-1])
{
digit[j] = digit[j-1];
j--;
}
digit[j] = temp;
}
}
void PaintInsertSort(DataType const digit[])
{
int i;
for(i = 0; i < Size; i++)
cout<<'\t'<<digit[i];
cout<<endl<<endl;
}