直接插入排序(修改自严蔚敏的数据结构)
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;
}
#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;
}
相关文章推荐
- 10-1-直接插入排序-内部排序-第10章-《数据结构》课本源码-严蔚敏吴伟民版
- C++代码,数据结构-内部排序-插入排序-直接插入排序
- 【C#数据结构】直接插入排序
- 数据结构——排序——直接插入排序和折半插入排序算法
- 数据结构 - 直接插入排序(Straight Insertion Sort) 详解 及 代码(C++)
- python数据结构之直接插入排序
- 数据结构 查找及排序算法、直接插入排序及顺序查找(c语言实现)
- 数据结构—单链表—直接插入排序
- 数据结构 - 直接插入排序法
- c++实现数据结构中的各种排序方法:直接插入、选择,归并、冒泡、快速、堆排序、shell排序
- 【数据结构】直接插入排序
- 数据结构--直接插入排序实现
- 算法与数据结构(四)--插入排序(直接插入排序)
- 直接插入排序 - 数据结构和算法90
- 数据结构之直接插入排序
- python数据结构之直接插入排序
- 数据结构之直接直接插入排序
- 浅谈数据结构之直接插入排序的理解
- 数据结构学习10——直接插入排序与希尔排序
- 数据结构直接插入排序——归并排序