各种排序算法,数据结构中的排序算法

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

1.直接插入排序

//InsertSort.cpp

//This function is to sort SqList

# include

# include

# define MAXSIZE 20

# define MAX_LENGTH 100

typedef int RedType;

typedef struct //define structure SqList

{ RedType r[MAXSIZE+1];

int length;

}SqList;

void InsertSort(SqList &L) //InsertSort() sub-function

{ int i,j;

for(i=2;i<=L.length;++i)

if(L.r[i]

{ L.r[0]=L.r[i];

for(j=i-1;L.r[0]

L.r[j+1]=L.r[j];

L.r[j+1]=L.r[0];

}

}

void main() //main() function

{ int i;

SqList L;

cout<

cout<

cout<>L.length;

for(i=1;i<=L.length;++i)

{ cout<<"Please input the "<

cin>>L.r[i];

}

cout<

for(i=1;i<=L.length;i++)

cout<

InsertSort(L); //call InsertSort()

cout<

for(i=1;i<=L.length;i++)

cout<

cout<

getch();

} //main() end

2.希尔排序

//Shellinert.cpp

//This function is Shell sort

# include

# include

# define MAXSIZE 20

# define OK 1

# define ERROR 0

typedef int RedType;

typedef struct //structure SqList

{ RedType r[MAXSIZE+1];

int length;

}SqList;

void Shellinsert(SqList&L,int dk) //Shellinsert() sub-function { int i,j;

for(i=dk+1;i<=L.length;++i)

if(L.r[i]

{ L.r[0]=L.r[i];

for(j=i-dk;j>0&&(L.r[0]

L.r[j+dk]=L.r[j];

L.r[j+dk]=L.r[0];

}

}

void main() //main() function

{ int i,dk=5;

SqList L={{0,49,38,65,97,76,13,27,49,55,4},10};

cout<

cout<

cout<

for(i=1;i<=L.length;i++)

cout<

Shellinsert(L,dk); //call Shellinsert()

cout<

for(i=1;i<=L.length;i++)

cout<

cout<

getch();

} //main() end

3.冒泡排序

//BubbleSort.cpp

//This function is to sort SqList

# include

# include

# define MAXSIZE 20

# define MAX_LENGTH 100

typedef int RedType;

typedef struct //define structure SqList

{ RedType r[MAXSIZE+1];

int length;

}SqList;

相关文档
最新文档