分享

选择,插入,交换,冒泡,希尔排序算法的效率比较

 收藏小管 2017-04-27
#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


对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.

 

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多