您的位置:首页 > 编程语言 > C语言/C++

直接插入排序(C语言实现)

2012-07-12 00:23 260 查看
#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);
}

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