#include <iostream>
using namespace std;
#define MAXSIZE 5
typedef int KeyType;
typedef struct
{
KeyType Key[MAXSIZE+1];
int length;
}SqList;
void InsertSort(SqList &L) //直接插入排序
{
for(int i = 2; i<= L.length; i++) //需将L.r[i]插入有序子表
{
if(L.Key[i] < L.Key[i-1])
{
L.Key[0] = L.Key[i]; //复制为哨兵
int j;
for( j = i-1; L.Key[0]<L.Key[j];--j)
L.Key[j+1] = L.Key[j]; //记录后移
L.Key[j+1] = L.Key[0]; //插入到正确的位置
}
}
}
void main()
{
SqList L;
L.length = MAXSIZE;
for(int i = 1 ; i <= L.length; i++)
{
cin >> L.Key[i];
}
for(int i = 1; i <= L.length ; i++)
{
cout << L.Key[i] <<" ";
}
cout << endl;
InsertSort(L);
for(int i = 1; i <= L.length ; i++)
{
cout << L.Key[i] <<" ";
}
cout << endl;
system("pause");
}
直接插入排序
最新推荐文章于 2019-07-17 13:20:00 发布