为什么堆排序这么慢

mengzesam 2018-10-18 10:26:01
下图是堆排序与其他排序比较(单位ms),其中shellIS是采用Incerpi-Sedgewick提出的增量递减序列,hybridQuick是当在快速与插入的混合排序,qsort是采用c库的排序


堆排序参考算法导论,maxHeapify作了点修改,用迭代取代递归,代码如下,请帮忙分析性能瓶颈在哪里

#define cmp(A, B) (A < B)
typedef long itemType;
void maxHeapify(itemType A[],int l,int r,int i){
/*
shiftDown
*/
if(i<l || i>r) return;
int root=i;
int largest=root;
int left=root-l+root+1;
int right=root-l+root+2;
if(right<=r && cmp(A[root],A[right]))
largest=right;
if(left<=r && cmp(A[largest],A[left]))
largest=left;
while(largest!=root){
itemType tmp=A[root];
A[root]=A[largest];
A[largest]=tmp;
root=largest;
left=root-l+root+1;
right=root-l+root+2;
if(right<=r && cmp(A[root],A[right]))
largest=right;
if(left<=r && cmp(A[largest],A[left]))
largest=left;
}
}
void buildMaxHeap(itemType A[],int l,int r){
for(int i=l-1+(r-l+1)/2;i>=l;i--)
maxHeapify(A,l,r,i);
}
void heapSort(itemType A[],int l,int r){
if(l==r) return;
buildMaxHeap(A,l,r);
int end=r;
while(end>l){
itemType tmp=A[end];
A[end]=A[l];
A[l]=tmp;
end--;
maxHeapify(A,l,end,l);
}
}
...全文
203 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
mengzesam 2018-10-18
  • 打赏
  • 举报
回复
代码中的
itemType代表long

typedef long itemType

33,008

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧