数据结构简单选择直接插入快速排序冒泡排序希尔排序堆排序算法比较平台Word下载.docx
- 文档编号:19751690
- 上传时间:2023-01-09
- 格式:DOCX
- 页数:14
- 大小:142.20KB
数据结构简单选择直接插入快速排序冒泡排序希尔排序堆排序算法比较平台Word下载.docx
《数据结构简单选择直接插入快速排序冒泡排序希尔排序堆排序算法比较平台Word下载.docx》由会员分享,可在线阅读,更多相关《数据结构简单选择直接插入快速排序冒泡排序希尔排序堆排序算法比较平台Word下载.docx(14页珍藏版)》请在冰豆网上搜索。
intj=i;
k=i+1
inti=1;
k++
L.r[i]L.r[j]
i++
简单选择排序
开始
i=2
i<
=L.length
L.r[i].key<
L.r[i-1].key
L.r[0]=L.r[i];
L.r[i]=L.r[i-1];
j=i-2;
L.r[0].key<
L.r[j].key
L.r[j+1]=L.r[j];
--j;
L.r[j+1]=L.r[0]
++i
结束
直接插入排序
希尔排序
快速排序
四、源程序代码
#defineN10
intcompare[6]={0,0,0,0,0,0},change[6]={0,0,0,0,0,0};
voidinput(ints[])
{
inttest[N];
srand((unsigned)time(NULL));
for(inti=0;
N;
i++)
{
test[i]=rand()%100;
for(intj=0;
j<
i;
j++)
while(test[j]==test[i])
test[i]=rand()%N;
j=0;
}
for(i=0;
=N-1;
s[i]=test[i];
}
voids&
a,int&
b)
inttmp;
tmp=a;
a=b;
b=tmp;
voidinsertsort(ints[])
inti,j;
inta[N+1];
for(i=1;
=N;
a[i]=s[i-1];
}
for(i=2;
a[0]=a[i];
for(j=i;
j>
0&
&
a[0]<
a[j-1]&
(++compare[0]);
j--)
{
a[j]=a[j-1];
change[0]++;
}
a[j]=a[0];
change[0]++;
voidbubble_sort(ints[],intn)
inti,j,temp,a[N];
n;
a[i]=s[i];
n-1;
for(j=0;
n-i-1;
compare[1]++;
if(a[j]>
a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
change[1]++;
}
intpartition(inta[],intlow,inthigh)
intt,key;
t=a[low];
key=a[low];
while(low<
high)
while(low<
high&
a[high]>
=key)
high--;
++compare[2];
if(low<
a[low]=a[high];
low++;
change[2]++;
a[low]<
a[high]=a[low];
a[low]=t;
returnlow;
voidquicksort(inta[],intlow,inthigh)
intkey;
if(low<
key=partition(a,low,high);
quicksort(a,low,key-1);
quicksort(a,key+1,high);
voidselectsort(ints[],intn)
inti,j,k,a[N];
intt;
j=i;
for(k=i+1;
=n-1;
k++)
if(a[k]<
a[j]&
(++compare[3]))
j=k;
if(j!
=i)
t=a[i];
a[i]=a[j];
a[j]=t;
change[3]++;
voidshellinsertsort(ints[],intn)
inti,k,a[N];
k=int(n/2);
for(intgap=n/2;
gap>
0;
gap/=2)
{
for(inti=gap;
i<
n;
i++)
{
inttmp=a[i];
intj=i;
for(;
j>
0&
tmp<
a[j-gap];
j-=gap)
a[j]=a[j-gap];
compare[4]++;
a[j]=tmp;
change[4]++;
}
voidheap_adjust(intarray[],inti,intlen)
intrc=array[i];
for(intj=2*i;
j<
len;
j*=2)
if(j<
len&
array[j]<
array[j+1])j++;
compare[5]++;
if(rc>
=array[j])break;
array[i]=array[j];
i=j;
array[i]=rc;
voidheap_sort(inta[],intlen)
inti;
for(i=(len-1)/2;
i>
=0;
i--)
heap_adjust(a,i,len);
for(i=(len-1);
swap(a[0],a[i]);
change[5]++;
//弹出最大值,重新对i-1个元素建堆
heap_adjust(a,0,i-1);
voidCMyDlg:
:
OnButton1()
//TODO:
Addyourcontrolnotificationhandlercodehere
UpdateData(TRUE);
ints[10],a[10];
input(s);
CStringstr[100];
100;
str[i]=a[i];
i<
i++)
str[i].Format("
%i,"
a[i]);
//把整型数组添加到字符串
m_shuju1+=str[i];
insertsort(s);
m_zhijie1=compare[0];
m_zhijie2=change[0];
quicksort(a,0,N-1);
m_kuaisu1=compare[2];
m_kuaisu2=change[2];
selectsort(s,N);
m_jiandan1=compare[3];
m_jiandan2=change[3];
shellinsertsort(s,N);
m_xier1=compare[4];
m_xier2=change[4];
heap_sort(a,N);
m_dui1=compare[5];
m_dui2=change[5];
bubble_sort(s,N);
m_maopao1=compare[1];
m_maopao2=change[1];
CStringstr2[100];
str2[i]=s[i];
str2[i].Format("
m_shuju2+=str2[i];
UpdateData(FALSE);
五、调试过程
对于算法的设计,除了希尔排序和堆排序之外,都比较简单,要注意每种排序的起始位置和哨兵的位置,便可以正确的排出。
而希尔排序,尽管是直接插入排序的变形,但应该从中间位置开始,从后至前选择,但是在程序上不好编出。
而堆排序,更是难以控制,只好借鉴参考。
六、结果分析
由随机数产生的10个数,排序后的结果如上图所示,可以发现,快速排序和简单选择排序比较次数和交换次数均较少,适合使用。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构 简单 选择 直接 插入 快速 排序 冒泡 希尔 算法 比较 平台