Insert Sort

#include #include #define MAXSIZE 20; typedef int KeyType; typedef struct { KeyType key; } RecordType; typedef struct { RecordType * r; int length; }SqList; void InitSqList(SqList &L); void AppendElement(SqList &L, KeyType key); void Display(SqList L); void InsertSort(SqList &L); void InitSqList(SqList &L) { int size = MAXSIZE + 1; L.r = (RecordType *)malloc(size * sizeof(RecordType)); L.length = 0; } void AppendElement(SqList &L, KeyType key) { int size = (int) MAXSIZE; if (L.length > size) { printf("%s", "list is full, can not append"); return; } L.length++; L.r[L.length].key = key; } void Display(SqList L) { register RecordType * p = &L.r[1]; while(p <= &L.r[L.length]) { printf("%d/t", p->key); p++; } } void InsertSort(SqList &L) { for (int i = 2; i <= L.length;i++) { if(L.r[i].key < L.r[i - 1].key) { L.r[0] = L.r[i]; L.r[i] = L.r[i - 1]; for (int j = i - 2; L.r[j].key > L.r[0].key; j--) { L.r[j + 1] = L.r[j]; } L.r[j + 1] = L.r[0]; } } } void main() { SqList L; InitSqList(L); AppendElement(L, 16); AppendElement(L, 13); AppendElement(L, 20); AppendElement(L, 18); AppendElement(L, 43); AppendElement(L, 21); AppendElement(L, 20); AppendElement(L, 91); AppendElement(L, 29); Display(L); printf("after insert sort"); InsertSort(L); Display(L); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值