1、算法实验一1、实验要求(1)排序 N 个元素,元素是随机生成的长为 116 的字符串,n 的取值为:24、2 6、2 8、2 10、2 12、2 14、2 16、2 18、2 20。算法包括:直接插入排序、冒泡排序、堆排序、归并排序、快排序。(2)排序 N 个元素,元素是随机生成的 110 4正整数,n 的取值为:24、2 6、2 8、2 10、2 12、2 14、2 16、2 18、2 20。算法包括:直接插入排序、快排序、基数排序、计数排序。2、源代码及说明源代码请参看 done 文件夹,说明:全局变量 n 为排序数目。主函数/后内容为输出函数3、数据分析数据规模排序方式 4 6 8 10
2、 12 14 16 18 20insert 0 0 0.001 0.004 0.069 1.118 20.797 694.395 13392.53radix 0 0 0 0.001 0.004 0.016 0.111 0.437 1.676counting 0 0 0 0 0 0 0.016 0.063 0.203quick 0 0 0 0 0 0 0.031 0.188 1.404insert-string 0 0 0 0.046 0.842 12.776 203.845 3554.806bubble-string 0 0 0.015 0.14 2.324 38.586merge-strin
3、g 0 0 0 0 0.016 0.109 0.483 2.262heap-string 0 0 0 0 0.015 0.125 0.561 2.605 12.152quick-string 0 0 0 0 0.015 0.078 0.39 3.073 29.39A)平方时间排序:冒泡排序:05010 15020-50510152025303540 bule-string Alomtic1 Fit of Shet1 bule-stringbule-string scaleModel Alometric1Equationy =a*xbRedced Chi-Sqr1.08534E-5Adj. R-
4、uar 1ValueStandr Erbule-stringa1.0961E-1.09452-9l-trib 2.7.01mdeo demo demo demoemo demo dmo demo demodemo demo demo demo demodemo demo demo demo demodemo demo demo demo demodemo demo demo demo demodemo demo demo demo demo插入排序:0 100200300010203040 insert-string Alomtic1 Fit of Shet1 insert-stringins
5、ert-stringscaleModel AlometriEquatiny =a*xbRedced Chi-Sqr0.196Adj. R-ua1ValueStandr Erinsert-stringa2.39645E4.694-10irt-trib.0170.57demo deo demo demo demoemo demo demo demo demodmo demo demo demo demodemo demo demo demo demodemo demo demo demo demodemo demo demo demo demodemo demo demo demo demo020
6、0400600800100120005010150insertscaleModel AlometriEquationy =a*xbRedced Chi-Sqr32.7623Adj. R-ua1ValueStandr Erinsert a1.83681.525-irt b2.90.94三个图结合起来,可以看到曲线拟合相关系数颇高0 200100 1200501015010512051305140510 insert it-string buleti Alomtric1 Fit of Shet1 insert leti it f t it-string lotric1 Fit of het1 bu
7、letiinsertscaleModel Alometric1Equationy =a*xbRedced Chi-Sqr 32.76230.1961.08534E-5Adj. R-uare 1 1 1ValueStandr Erorinsert a 1.8368E-91.525-10irt b 2.130.94insert-stringa .39645E-84.694E-10irt-tri b 2.01670.57bule-stringa 1.9E-1.942E-9l-tri b 2.0670.10demo dmo dmo demo demodemo demo dmo dmo demodemo
8、 demo demo demo demodemo demo demo demdodemo demo demo demo demodemo demo demo demo demodemo demo demo demo demo放大 212 前的数据,发现冒泡排序增长比插入排序要快,调用操作数较多。而对字符排序操作数则比对数字排序操作数要多。0 5010 15020-5005010insert it-string buleti Alomtric1 Fit of Shet1 insert leti it f t it-string lotric1 Fit of het1 buletiinsertsc
9、aleModel Alometric1Equationy =a*xbRedced Chi-Sqr 32.76230.1961.08534E-5Adj. R-uare 1 1 1ValueStandr Erorinsert a 1.8368E-91.525-10irt b 2.130.94insert-stringa .39645E-84.694E-10irt-tri b 2.01670.57bule-stringa 1.9E-1.942E-9l-tri b 2.0670.10demo demo dmo demo dmodemo demo dmo dmo dmodemo demo demo de
10、mo demodemo demo demo demo demodemo demo demo demo demodemo demo demo demo demodemo demo demo demo demoB)线性时间排序计数排序:0 500 1000.0.10.2 countig Liear Fit of Shet1 countigcountigscaleEquationy =a +b*xWeight No WeightinRsidual Sm of quares1.5473E-4Adj. R-Squar0.9517ValueStandr ErcountigIntercpt 9.73E-40
11、.174ti Slo1.528-4.8E-9dmdmo demo demo demoo demo demo demo demodemo demo demo demo demodemo demo demo demo demodemo demo demo demo demodemo demo demo demo demdemo demo demo demo demo基数排序:020040060080010012000.0.51.01.52.0 radix Liner Fit of Shet1 radixradixscaleEquationy =a +b*WeightNo WeighRsidual
12、Sum of qares4.5036E-4Adj. R-Sq0.979ValueStandr radixIntercpt6.05790.296ri Slo1.186.4Edo dmo demo demo demoemo demo demo demo demodemo demo demo demo demodemo demo demo demo demodemo demo demo demo demodemo demo demo demo demodemo demo demo demo demo两个图结合:两种排序的数据都严格按线性增长,可以看出基数排序的操作数较多0 500 100012 ra
13、dix countig Liear Fit of Shet1 radix in it f t countigradixscaleEquationy =a +b*xWeight No WeightingRsidual Sum of Sqres4.5036E-41.5473E-4Adj. R-quare0.9790.9517ValueStandr Erorradix Intercpt 6.0579E-40.296ri Slo 1.186-8.24E-countigIntercpt 9.73E-40.174ti Slo 1.528-74.8E-9demo demdemo demo demod dem
14、o demo demo demoemo demo emdemo demodemo demo demo demo demodemo demo demo demo demodemo demo demo demo demodemo demo demo demo demoC) nlgn 时间排序-10010020304050607080901010-202468102146182024628302quickscalequick merg-string hapti quick-string从图中可以看出各算法的增长速度,在同为字符串的三个排序中,归并排序和堆排序效率要比快排要高。D)总览利用多点平滑函数
15、拟合各点,再放大进行微观的分析,可以看到平方时间算法、nlgn 时间算法和线性时间算法的鲜明对比。 (由于是多点平滑,有些线并没有通过点)0 50 10 1500.0.10.20.3insertscaleinsert radix countig qik insert-string bulti merg-string hapti quick-stringdemo demo demo demo demodemo demo demo demo demodemo demo demo demo dodemo demo demo demo demodemo demo demo demo demodemo demo demo demo demodemo demo demo demo demo4、心得与总结本次试验加深我了对各种排序算法的工作原理、实现方式、所用的时间和空间等认识。复习了时间函数、动态分配贮存以及各种数据结构。其中最大的认识是在编程实现时,要清楚算法中各种参数的上界和下界的特殊含义,否则算法不可能正常工作,或者指针会越界出错(比如出现在 partition 中的 p 必须跑到 r-1,否则不能正常工作) ,在以后的学习中要注意对这些细节的琢磨。