老邱排序分册讲解.docx

上传人:b****5 文档编号:8640339 上传时间:2023-02-01 格式:DOCX 页数:27 大小:17.33KB
下载 相关 举报
老邱排序分册讲解.docx_第1页
第1页 / 共27页
老邱排序分册讲解.docx_第2页
第2页 / 共27页
老邱排序分册讲解.docx_第3页
第3页 / 共27页
老邱排序分册讲解.docx_第4页
第4页 / 共27页
老邱排序分册讲解.docx_第5页
第5页 / 共27页
点击查看更多>>
下载资源
资源描述

老邱排序分册讲解.docx

《老邱排序分册讲解.docx》由会员分享,可在线阅读,更多相关《老邱排序分册讲解.docx(27页珍藏版)》请在冰豆网上搜索。

老邱排序分册讲解.docx

老邱排序分册讲解

实验十排序实验题

1.分别用直接插入排序、折半插入排序、希尔排序、冒泡排序、快速排序和简单选择排序算法对相同的待排序列进行排序,输出排序结果;

2.统计排序过程中“比较”操作的执行次数和记录“移动”的次数。

【存储结构】

#defineMAXSIZE20//顺序表的最大长度

typedefstruct

{intkey;//关键字项

InfoTypeotherinfo;//其他数据项

}DataType;

typedefstruct

{DataTyper[MAXSIZE+1];//r[0]闲置或用作哨兵单元

intlength;

}SqList;

//分别用直接插入排序、折半插入排序、希尔排序、冒泡排序、快速排序和简单选择排序算法对相同的待排序列进行排序,输出排序结果

//直接插入排序

#include

#include

#include

#defineMAXSIZE20

typedefintKeyType;

typedefcharInfoType;

typedefstruct

{

KeyTypekey;

InfoTypeotherType;

}RedType;

typedefstruct

{

RedTyper[MAXSIZE+1];

intlength;

}SqList;

voidInsertSort(SqList&L)

{

inti,j;

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

if(L.r[i].key

{

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

L.r[i]=L.r[i-1];

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

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

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

}

}

voidInput(SqList&L)

{

cout<<"inputn:

"<

cin>>L.length;

for(inti=1;i

cin>>L.r[i].key;

}

voidPrintList(SqList&L)

{

for(inti=0;i

cout<

cout<

}

voidmain()

{

SqListL;

L.length=0;

Input(L);

PrintList(L);

cout<<"InsertSort:

"<

InsertSort(L);

PrintList(L);

}

/*

inputn:

9

13

23

11

67

98

43

76

90

56

132311679843769056

InsertSort:

111323435667769098

Pressanykeytocontinue

*/

//分别用直接插入排序、折半插入排序、希尔排序、冒泡排序、快速排序和简单选择排序算法对相同的待排序列进行排序,输出排序结果

//折半插入排序

#include

#include

#include

#defineMAXSIZE20

typedefintKeyType;

typedefcharInfoType;

typedefstruct

{

KeyTypekey;

InfoTypeotherType;

}RedType;

typedefstruct

{

RedTyper[MAXSIZE+1];

intlength;

}SqList;

voidInsertSort(SqList&L)

{

inti,j;

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

if(L.r[i].key

{

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

L.r[i]=L.r[i-1];

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

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

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

}

}

voidInput(SqList&L)

{

cout<<"inputn:

"<

cin>>L.length;

for(inti=1;i

cin>>L.r[i].key;

}

voidPrintList(SqList&L)

{

for(inti=0;i

cout<

cout<

}

voidBinarySort(SqList&L)

{

inti,j;

intlow,high,mid;

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

{

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

low=1;

high=i-1;

while(low<=high)

{

mid=(low+high)/2;

if(L.r[0].key

high=mid-1;

else

low=mid+1;

}

for(j=i-1;j>=high+1;--j)

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

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

}

}

voidmain()

{

SqListL;

L.length=0;

Input(L);

PrintList(L);

cout<<"BinarySort:

"<

BinarySort(L);

PrintList(L);

}

/*

inputn:

9

13

23

11

67

98

43

76

90

56

132311679843769056

BinarySort:

111323435667769098

Pressanykeytocontinue

*/

//分别用直接插入排序、折半插入排序、希尔排序、冒泡排序、快速排序和简单选择排序算法对相同的待排序列进行排序,输出排序结果

//希尔排序

#include

#include

#include

#defineMAXSIZE20

typedefintKeyType;

typedefcharInfoType;

typedefstruct

{

KeyTypekey;

InfoTypeotherType;

}RedType;

typedefstruct

{

RedTyper[MAXSIZE+1];

intlength;

}SqList;

voidInsertSort(SqList&L)

{

inti,j;

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

if(L.r[i].key

{

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

L.r[i]=L.r[i-1];

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

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

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

}

}

voidShellInsert(SqList&L,intdk)

{

inti,j;

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

if(L.r[i].key

{

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

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

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

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

}

}

voidShellSort(SqList&L,intdlta[],intt)

{

for(intk=0;k

{

ShellInsert(L,dlta[k]);

}

}

voidInput(SqList&L)

{

cout<<"inputn:

"<

cin>>L.length;

for(inti=1;i

cin>>L.r[i].key;

}

voidPrintList(SqList&L)

{

for(inti=0;i

cout<

cout<

}

voidmain()

{

SqListL;

L.length=0;

Input(L);

PrintList(L);

intdlta[3]={5,3,1};

intt=3;

cout<<"ShellSort:

"<

ShellSort(L,dlta,t);

PrintList(L);

}

/*

inputn:

9

13

23

11

67

98

43

76

90

56

132311679843769056

ShellSort:

111323435667769098

Pressanykeytocontinue

*/

//分别用直接插入排序、折半插入排序、希尔排序、冒泡排序、快速排序和简单选择排序算法对相同的待排序列进行排序,输出排序结果

//冒泡排序

#include

#include

#include

#defineMAXSIZE20

typedefintKeyType;

typedefcharInfoType;

typedefstruct

{

KeyTypekey;

InfoTypeotherType;

}RedType;

typedefstruct

{

RedTyper[MAXSIZE+1];

intlength;

}SqList;

voidInsertSort(SqList&L)

{

inti,j;

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

if(L.r[i].key

{

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

L.r[i]=L.r[i-1];

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

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

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

}

}

voidSwap(SqList&L,inta,intb)

{

KeyTypetemp;

temp=L.r[a].key;

L.r[a].key=L.r[b].key;

L.r[b].key=temp;

}

voidBubbleSort(SqList&L)

{

inti,j;

intflag;

for(i=1;i

{

flag=1;

for(j=L.length;j>=i+1;j--)

if(L.r[j-1].key>L.r[j].key)

{

Swap(L,j-1,j);

flag=0;

}

}

if(flag)return;

}

voidInput(SqList&L)

{

cout<<"inputn:

"<

cin>>L.length;

for(inti=1;i

cin>>L.r[i].key;

}

voidPrintList(SqList&L)

{

for(inti=0;i

cout<

cout<

}

voidmain()

{

SqListL;

L.length=0;

Input(L);

PrintList(L);

cout<<"BubbleSort:

"<

BubbleSort(L);

PrintList(L);

}

/*

inputn:

9

13

23

11

67

98

43

76

90

56

132311679843769056

BubbleSort:

111323435667769098

Pressanykeytocontinue

*/

//分别用直接插入排序、折半插入排序、希尔排序、冒泡排序、快速排序和简单选择排序算法对相同的待排序列进行排序,输出排序结果

//快速排序

#include

#include

#include

#defineMAXSIZE20

typedefintKeyType;

typedefcharInfoType;

typedefstruct

{

KeyTypekey;

InfoTypeotherType;

}RedType;

typedefstruct

{

RedTyper[MAXSIZE+1];

intlength;

}SqList;

voidInsertSort(SqList&L)

{

inti,j;

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

if(L.r[i].key

{

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

L.r[i]=L.r[i-1];

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

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

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

}

}

voidInput(SqList&L)

{

cout<<"inputn:

"<

cin>>L.length;

for(inti=1;i

cin>>L.r[i].key;

}

voidPrintList(SqList&L)

{

for(inti=0;i

cout<

cout<

}

intPartition(SqList&L,intlow,inthigh)

{

L.r[0]=L.r[low];

intpivokey=L.r[low].key;

while(low

{

while(low=pivokey)

--high;

L.r[low]=L.r[high];

while(low

++low;

L.r[high]=L.r[low];

}

L.r[low]=L.r[0];

returnlow;

}

voidQsort(SqList&L,intlow,inthigh)

{

intpivoloc;

if(low

{

pivoloc=Partition(L,low,high);

Qsort(L,low,pivoloc);

Qsort(L,pivoloc+1,high);

}

}

voidQuickSort(SqList&L)

{

Qsort(L,1,L.length);

}

voidmain()

{

SqListL;

L.length=0;

Input(L);

PrintList(L);

cout<<"QuickSort:

"<

QuickSort(L);

PrintList(L);

}

/*

inputn:

9

13

23

11

67

98

43

76

90

56

132311679843769056

QuickSort:

111323435667769098

Pressanykeytocontinue

*/

//分别用直接插入排序、折半插入排序、希尔排序、冒泡排序、快速排序和简单选择排序算法对相同的待排序列进行排序,输出排序结果

//简单选择

#include

#include

#include

#defineMAXSIZE20

typedefintKeyType;

typedefcharInfoType;

typedefstruct

{

KeyTypekey;

InfoTypeotherType;

}RedType;

typedefstruct

{

RedTyper[MAXSIZE+1];

intlength;

}SqList;

voidInsertSort(SqList&L)

{

inti,j;

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

if(L.r[i].key

{

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

L.r[i]=L.r[i-1];

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

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

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

}

}

voidInput(SqList&L)

{

cout<<"inputn:

"<

cin>>L.length;

for(inti=1;i

cin>>L.r[i].key;

}

voidPrintList(SqList&L)

{

for(inti=0;i

cout<

cout<

}

voidSwap(SqList&L,inta,intb)

{

KeyTypetemp;

temp=L.r[a].key;

L.r[a].key=L.r[b].key;

L.r[b].key=temp;

}

intSelectMinKey(SqList&L,inti)

{

KeyTypetemp=L.r[i].key;

inta=i;

for(intt=i;t

if(L.r[t].key

{

temp=L.r[t].key;

a=t;

}

returna;

}

voidSelectSort(SqList&L)

{

inti,j;

for(i=1;i

{

j=SelectMinKey(L,i);

if(i!

=j)

Swap(L,i,j);

}

}

voidmain()

{

SqListL;

L.length=0;

Input(L);

PrintList(L);

cout<<"SelectSort:

"<

SelectSort(L);

PrintList(L);

}

/*

inputn:

9

13

23

11

67

98

43

76

90

56

132311679843769056

SelectSort:

111323435667769098

Pressanykeytocontinue

*/

//分别用直接插入排序、折半插入排序、希尔排序、冒泡排序、快速排序和简单选择排序算法对相同的待排序列进行排序,输出排序结果

//希尔排序

#include

#include

#include

#defineMAXSIZE20

typedefintKeyType;

typedefcharInfoType;

typedefstruct

{

KeyTypekey;

InfoTypeotherType;

}RedType;

typedefstruct

{

RedTyper[MAXSIZE+1];

intlength;

}SqList;

voidInput(SqList&L)

{

cout<<"inputn:

"<

cin>>L.length;

for(inti=1;i

cin>>L.r[i].key;

}

voidPrintList(SqList&L)

{

for(inti=0;i

cout<

cout<

}

voidInsertSort(SqListL)

{

inti,j;

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

if(L.r[i].key

{

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

L.r[i]=L.r[i-1];

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

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

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

}

PrintList(L);

}

////////////

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 总结汇报 > 其它

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1