打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
选择,插入,交换,冒泡,希尔排序算法的效率比较
#include#includeusing namespace std;templatevoid bubbleSort(T arr[],int n){ int temp,i,j; bool flag=false; for(i=0;iarr[j]) { temp=arr[j]; arr[j]=arr[j-1]; arr[j-1]=temp; flag=true; } if(!flag) break; }}void xuanzeSort(int arr[],int n){ int smallIndex; int pass,j; int temp; for(pass=0;passvoid swapSort(T arr[],int n){ int i,j,temp; for(i=0;ivoid shellSort(T arr[],int n){ int i,j,gap=n; T temp; do { gap=gap/3+1;//间隔 for(i=gap;i=0&&temp1);//间隔}templatevoid insertionSort(T arr[],int n){ int i,j; T target; for(i=1;i0&&target<3;j++) {="" switch(j)="" {="" case="" 0:=""><><><><><><><'timing the="" selection=""><'selection sort="" takes=""><><'><'timing the="" bubble=""><'bubble sort="" takes=""><><'><'timing the="" insert=""><'insert sort="" takes=""><><'><'timing the="" swap=""><'swap sort="" takes=""><><'><'timing the="" shell=""><'shell sort="" takes=""><><'>


对10000个数据排序的时间效率结果,最好是希尔排序,最差是交换排序,最好最坏情况的时间效率相差不大,从这里可以看出为什么算法宁愿找个最差情况比较好的算法也不要平均情况比较好的算法.

随机数
Timing the Selection Sort
Selection Sort takes 130 seconds.
Timing the Bubble Sort
Bubble Sort takes 130 seconds.
Timing the Insert Sort
Insert Sort takes 60 seconds.
Timing the Swap Sort
Swap Sort takes 280 seconds.
Timing the Shell Sort
Shell Sort takes 10 seconds.

正序数
Timing the Selection Sort
Selection Sort takes 120 seconds.
Timing the Bubble Sort
Bubble Sort takes 120 seconds.
Timing the Insert Sort
Insert Sort takes 60 seconds.
Timing the Swap Sort
Swap Sort takes 290 seconds.
Timing the Shell Sort
Shell Sort takes 10 seconds.

倒序数
Timing the Selection Sort
Selection Sort takes 120 seconds.
Timing the Bubble Sort
Bubble Sort takes 120 seconds.
Timing the Insert Sort
Insert Sort takes 60 seconds.
Timing the Swap Sort
Swap Sort takes 290 seconds.
Timing the Shell Sort
Shell Sort takes 0 seconds.

 

 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
(一).2
冒泡排序(c 实现)
C语言冒泡排序算法及代码
排序算法
冒泡排序、插入排序与选择排序
Java中的经典算法之冒泡排序(Bubble Sort)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服