多种排序算法实现及其比较Word下载.docx
- 文档编号:19883424
- 上传时间:2023-01-11
- 格式:DOCX
- 页数:29
- 大小:185.92KB
多种排序算法实现及其比较Word下载.docx
《多种排序算法实现及其比较Word下载.docx》由会员分享,可在线阅读,更多相关《多种排序算法实现及其比较Word下载.docx(29页珍藏版)》请在冰豆网上搜索。
(15)typedefintKeyType;
(16)typedefintArrType[RADIX];
//定义宏参
(17)
(18)
(19)//固定类型结构定义
(20)typedefstruct
(21){
(22)KeyTypekey;
//关键字
(23)InfoTypeotherinfo;
//其他数据
(24)intnext;
(25)}RedType;
(26)
(27)
(28)//顺序表结构类型定义
(29)typedefstructSqList
(30){
(31)RedTyper[MAXSIZE+1];
//元素类型定义
(32)intkeynum;
////关键字数量
(33)intlength;
//顺序表长度
(34)}SqList;
(35)
(36)
(37)//======================函数声明=========================//
(38)voidCreatSort(SqList&
L);
//建立顺序表
(39)voidInsertSort(SqList&
//直接插入排序
(40)voidShellInsert(SqList&
L,intdk);
//希尔排序
(41)voidShellSort(SqList&
L,intdlta[],intt);
(42)intPartition(SqList&
L,intlow,inthigh);
(43)voidQSort(SqList&
//快速排序
(44)voidQuickSort(SqList&
(45)voidSelectSort(SqList&
//简单排序
(46)voidHeapAdjust(SqList&
L,ints,intm);
//堆排序
(47)voidHeaoSort(SqList&
(48)voidBubbleSort(SqList&
//冒泡排序
(49)voidDistribute(RedTyper[],inti,ArrTypef,ArrTypee);
//基数排序
(50)voidCollect(RedTyper[],inti,ArrTypef,ArrTypee);
(51)voidRadixSort(SqList&
(52)voidprint0(SqListL);
(53)voidprint2(SqListL);
//输出数据
(54)voidprint(SqListL);
(55)voidprint3(SqList&
(56)//======================函数声明=========================//
(57)
(58)
(59)//建立顺序表
(60)voidCreatSort(SqList&
L)
(61){//操作结果:
建立一个顺序表
(62)intm,v,i;
(63)printf("
请输入数据的长度:
"
);
(64)scanf("
%d"
&
m);
(65)L.length=m;
(66)printf("
请输入数据!
\n"
(67)for(i=1;
i<
=L.length;
i++)
(68){
(69)printf("
请输入第%d个数据:
"
i);
(70)scanf("
v);
(71)L.r[i].otherinfo=i;
(72)L.r[i].key=v;
(73)}
(74)printf("
初始线性表为:
(75)print0(L);
(76)printf("
(77)}
(78)
(79)
(80)//输出数据
(81)voidprint10(SqListL)
(82){//初始条件:
顺序表L已存在
(83)//操作结果:
(84)inti,k,t=3;
(85)for(k=0;
k<
t;
++k)
(86){
(87)printf("
第%d趟排序结果:
k+1);
(88)for(i=1;
(89){
(90)printf("
%d"
L.r[i].key);
(91)}
(92)printf("
(93)}
(94)}
(95)
(96)
(97)//输出数据
(98)voidprint(SqListL)
(99){//初始条件:
(100)//操作结果:
(101)inti;
(102)for(i=1;
(103)printf("
(%d,%d)"
L.r[i].key,L.r[i].otherinfo);
(104)printf("
(105)}
(106)
(107)
(108)
(109)//输出数据
(110)voidprint0(SqListL)
(111){//初始条件:
(112)//操作结果:
(113)inti;
(114)for(i=1;
(115){
(116)printf("
(117)}
(118)printf("
(119)}
(120)
(121)
(122)//插入排序
(123)voidInsertSort(SqList&
(124){//初始条件:
(125)//操作结果:
元素进行插入排序
(126)inti,j;
(127)for(i=2;
(128)L.r[0]=L.r[i];
(129)L.r[i]=L.r[i-1];
(130)for(j=i-2;
L.r[0].key<
L.r[j].key;
--j)
(131)L.r[j+1]=L.r[j];
(132)L.r[j+1]=L.r[0];
(133)}
(134)
(135)
(136)//输出插入排序
(137)voidprint1(SqList&
(138){//初始条件:
(139)//操作结果:
输出表中所有元素
(140)inti,k,t=3;
(141)for(k=0;
(142){
(143)InsertSort(L);
(144)printf("
(145)for(i=1;
(146){
(147)printf("
(148)}
(149)printf("
(150)}
(151)}
(152)
(153)
(154)
(155)//希尔排序
(156)voidShellInsert(SqList&
L,intdk)
(157){//初始条件:
(158)//操作结果:
元素进行希尔排序
(159)inti,j;
(160)for(i=dk+1;
++i)
(161)if(L.r[i].key,L.r[i-dk].key)
(162){
(163)L.r[0]=L.r[i];
(164)for(j=i-dk;
j>
0&
&
j-=dk)
(165)L.r[j+dk]=L.r[j];
(166)L.r[j+dk]=L.r[0];
(167)}
(168)}
(169)
(170)
(171)//输出希尔排序
(172)voidShellSort(SqList&
L,intdlta[],intt)
(173){//初始条件:
(174)//操作结果:
(175)inti,k;
(176)for(k=0;
(177){
(178)ShellInsert(L,dlta[k]);
(179)printf("
第%d趟排序结果(d=%d):
k+1,dlta[k]);
(180)for(i=1;
(181){
(182)printf("
(183)}
(184)printf("
(185)}
(186)}
(187)
(188)
(189)//冒泡排序
(190)voidBubbleSort(SqList&
(191){//初始条件:
(192)//操作结果:
元素进行冒泡排序
(193)inti,j,flag=1;
(194)for(i=1;
=L.length&
flag==1;
(195){
(196)flag=0;
(197)for(j=0;
j<
=L.length-i;
j++)
(198){
(199)if(L.r[j].key>
L.r[j+1].key)
(200){
(201)L.r[0].key=L.r[j].key;
(202)L.r[j].key=L.r[j+1].key;
(203)L.r[j+1].key=L.r[0].key;
(204)}
(205)}
(206)}
(207)}
(208)
(209)
(210)//输出冒泡排序
(211)voidprint3(SqList&
(212){//初始条件:
(213)//操作结果:
(214)inti,k;
(215)for(k=0;
L.length;
(216){
(217)BubbleSort(L);
(218)printf("
(219)for(i=1;
(220){
(221)printf("
(222)}
(223)printf("
(224)}
(225)}
(226)
(227)
(228)
(229)//快速排序
(230)intPartition(SqList&
L,intlow,inthigh)
(231){//初始条件:
(232)//操作结果:
进行快速排序
(233)KeyTypepivotkey;
(234)L.r[0]=L.r[low];
(235)pivotkey=L.r[low].key;
(236)while(low<
high)
(237){
(238)while(low<
high&
L.r[high].key>
=pivotkey)
(239)--high;
(240)L.r[low]=L.r[high];
(241)while(low<
L.r[low].key<
(242)++low;
(243)L.r[high]=L.r[low];
(244)}
(245)L.r[low]=L.r[0];
(246)returnlow;
(247)}
(248)
(249)
(250)//快速排序
(251)voidQSort(SqList&
(252){//初始条件:
(253)//操作结果:
运用递归进行快速排序
(254)intpivotloc;
(255)if(low<
(256){
(257)pivotloc=Partition(L,low,high);
(258)QSort(L,low,pivotloc-1);
(259)QSort(L,pivotloc+1,high);
(260)}
(261)}
(262)
(263)
(264)//快速排序
(265)voidQuickSort(SqList&
(266){//初始条件:
(267)//操作结果:
运用递归进行快速排序
(268)QSort(L,1,L.length);
(269)}
(270)
(271)
(272)
(273)//简单选择排序
(274)voidSelectSort(SqList&
(275){//初始条件:
(276)//操作结果:
元素进行简单选择排序
(277)inti,j,small;
(278)for(i=1;
(279){
(280)small=i;
(281)for(j=i+1;
(282){
(283)if(L.r[j].key<
L.r[small].key)
(284)small=j;
(285)}
(286)if(small!
=i)
(287){
(288)L.r[0]=L.r[i];
(289)L.r[i]=L.r[small];
(290)L.r[small]=L.r[0];
(291)}
(292)}
(293)}
(294)
(295)
(296)
(297)
(298)//堆排序
(299)voidHeapAdjust(SqList&
L,ints,intm)
(300){//初始条件:
(301)//操作结果:
元素进行堆排序
(302)RedTyperc;
(303)intj;
(304)rc=L.r[s];
(305)for(j=2*s;
=m;
j*=2)
(306){
(307)if(j<
m&
L.r[j].key<
(308)++j;
(309)if(!
(rc.key<
L.r[j].key))
(310)break;
(311)L.r[s]=L.r[j];
(312)s=j;
(313)}
(314)L.r[s]=rc;
(315)}
(316)
(317)
(318)//堆排序
(319)voidHeaoSort(SqList&
(320){//初始条件:
(321)//操作结果:
(322)RedTypet;
(323)inti;
(324)for(i=L.length/2;
i>
0;
--i)
(325)HeapAdjust(L,i,L.length);
(326)for(i=L.length;
1;
(327){
(328)t=L.r[2];
(329)L.r[1]=L.r[i];
(330)L.r[i]=t;
(331)HeapAdjust(L,1,i-1);
(332)}
(333)}
(334)
(335)
(336)
(337)
(338)//输出数据
(339)voidprint6(SqList&
(340){//初始条件:
(341)//操作结果:
(342)inti,k,t=3;
(343)for(k=0;
(344){
(345)HeaoSort(L);
(346)printf("
(347)for(i=1;
(348){
(349)printf("
(350)}
(351)printf("
(352)}
(353)}
(354)
(355)
(356)
(357)/*//归并排序
(358)voidMerge(RedTypea[],RedType&
b[],inti,intm,intn)
(359){
(360)intj,k;
(361)for(j=m+1,k=i;
=m&
=n;
(362){
(363)if(a[i].key<
a[j].key)
(364)b[k]=a[i++];
(365)else
(366)b[k]=a[j++];
(367)}
(368)if(i<
=m)
(369)while(k=n&
=m)
(370)b[k++]=a[i++];
(371)if(j<
=n)
(372)while(k=n&
=n)
(373)b[k++]=a[j++];
(374)}
(375)
(376)
(377)voidMSort(RedTypea[],RedType&
b[],ints,intt)
(378){
(379)intm;
(380)if(s==t)
(381)b[s]=a[s];
(382)else
(383){
(384)m=(s+t)/2;
(385)MSort(a,c,s,m);
(386)MSort(a,c,m+1,t);
(387)Merge(c,b,s,m,t);
(388)}
(389)}
(390)
(391)voidMergeSort(SqList&
(392){
(393)MSort(L.r,L.r,1,L.length);
(394)}
(395)
(396)*/
(397)
(398)
(399)
(400)
(401)
(402)
(403)//基数排序
(404)voidDistribute(RedTyper[],inti,ArrTypef,ArrTypee)
(405){//初始条件:
(406)//操作结果:
(407)intj,p;
(408)for(j=0;
RADIX;
++j)
(409)f[j]=0;
//各子表初始化为空表
(410)for(p=r[0].next;
p!
=0;
p=r[p].next)
(411){
(412)//j=r[p].key[i]-'
0'
;
//ord将记录中第i个关键字映射到[0..RADIX]
(413)if(!
f[j])
(414)f[j]=p;
(415)else
(416)r[e[j]].next=p;
(417)e[j]=p;
//将p所指的结点插入第j个子表中
(418)}
(419)}
(420)
(421)
(422)//基数排序
(423)voidCollect(RedTyper[],inti,ArrTypef,ArrTypee)
(424){//初始条件:
(425)//操作结果:
(426)intt,j;
(427)for(j=0;
!
f[j];
j=++j);
//找第一个非空子表,succ为求后继函数
(428)r[0].next=f[j];
(429)t=e[j];
//r[0].next指向第一个非空子表中第一个结点
(430)while(j<
RADIX-1)
(431){
(432)for(j=++j;
RADIX-1&
//找下一个非空子表
(433)if(f[j])
(434){
(435)r[t].next=f[j];
(436)t=e[j];
(437)}//链接两个非空子表
(438)}
(439)r[t].next=0;
//t指向最后一个非空子表中的最后一个结点
(440)}
(441)
(442)
(443)
(444)//基数排序
(445)voidRadixSort(SqList&
(446){//初始条件:
(447)//操作结果:
(448)inti;
(449)ArrTypef,e;
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 多种 排序 算法 实现 及其 比较