首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >这个算法有什么问题吗?

这个算法有什么问题吗?
EN

Stack Overflow用户
提问于 2010-07-14 23:27:17
回答 1查看 132关注 0票数 1

我对堆排序的这个算法有一个问题

代码语言:javascript
复制
Heap_Sort(A)
  Build_Heap(A)
  for i<--n down to 2
    swap (A[1],A[n])
    n<--n-1
    MaxHeapify(A,1)

我认为我们应该这样写,而不是这个算法:

代码语言:javascript
复制
Heap_Sort(A)
  Build_Heap(A)
  for i<-- n down to 1
    Delete_Max(A)
EN

回答 1

Stack Overflow用户

发布于 2010-07-14 23:47:19

如果你做的是就地排序...

代码语言:javascript
复制
Heap_Sort(A)
    B = Build_Heap(A)    # Assuming maxheap

    for i -> A.length to 0:
        Remove top value from B (which is basically just A, but treated differently),
        assuming the heap is modified to maintain partial ordering during this part
        (as well as decreasing the noted size of the heap by 1) and add the removed
        value to position A[i].

基本上是你的算法。但是,如果您没有就地进行排序,则可以简单地使用minheap并将所有值弹出到一个新的数组中。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3247721

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档