选择与合并排序问题

发布于 2025-01-17 23:41:54 字数 301 浏览 4 评论 0原文

假设我们的交换(a,b)(非常昂贵)的函数的实现效率低下,并且我们可以选择四种排序算法可供选择,即:

  1. 合并
  2. 选择插入插入插入
  3. 插入插入堆
  4. 堆积,

这是以下之一四个会表现最好?

现在,根据我的说法,因为掉期是一项昂贵的操作,我们需要最大程度地减少掉期数量。因此,从技术上讲,答案应合并排序,因为它在其一般实施中没有执行交换。但是答案键表明它必须是选择选择?我不明白为什么,请帮忙。

选择排序首先找到正确的索引,然后掉期经济的互换,但合并排序的表现还不错?

Suppose we have inefficient implementation of swap(a,b)(very costly) function and we are given the choice of four sorting algorithms to choose from, namely:

  1. merge-sort
  2. selection-sort
  3. insertion-sort
  4. heap-sort

Which one of the following four will perform the best ?

Now according to me as the swap is a costly operation we need to minimise the number of swaps. so technically the answer should be merge sort as it performs no swapping in its general implementation. But the answer-key suggests that it must be selection-sort instead ? I fail to understand why, please help.

Selection sort first finds the correct index and then swaps which is economical but doesn't merge sort perform better still ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文