您的位置:首页 > 理论基础 > 数据结构算法

直接插入排序(修改自严蔚敏的数据结构)

2010-09-12 21:54 295 查看
#include <stdio.h>

#include <stdlib.h>

#define MAXSIZE 20

typedef int KeyType;

typedef struct{

KeyType key;

//InfoType otherinfo;

}RedType;

typedef struct{

RedType r[MAXSIZE+1];

int length;

}SqList;

void InsertSort( SqList *L, int da ){

int i,j;

L->length=L->length+1;

L->r[L->length].key=da;

for( i=2; i<=L->length; ++i )

if ( L->r[i].key < L->r[i-1].key ){

L->r[0] = L->r[i];

for ( j=i-1; L->r[0].key < L->r[j].key ; --j )

L->r[j+1] = L->r[j];

L->r[j+1] = L->r[0];

}

}

int main(int argc, char *argv[])

{

SqList sl;

int i;

sl.length=0;

InsertSort( &sl, 38);

InsertSort( &sl, 49);

InsertSort( &sl, 65);

InsertSort( &sl, 97);

InsertSort( &sl, 78);



for ( i=1; i<=sl.length; i++ ){

if (i>1)

printf(",");

printf("%d",sl.r[i].key);

}



system("PAUSE");

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: