算法导论中的插入排序

发布于 2024-12-29 03:57:41 字数 815 浏览 2 评论 0原文

在算法简介第二版中,我找到了插入排序伪代码,

INSERTION-SORT(A)
1   for j <- 2 to length[A]
2       do key <- A[j]
3          //Insert A[j] into the sorted sequence A[1 □ j - 1].
4          i <- j - 1
5          while i > 0 and A[i] > key
6           do A[i+1] <- A[i]
7              i <- i -1
8          A[i + 1] <- key

但我无法理解交换在这里是如何工作的。

我认为它需要像这样的交换操作,

INSERTION-SORT(A)
1   for j <- 2 to length[A]
2       do key <- A[j]
3          //Insert A[j] into the sorted sequence A[1 □ j - 1].
4          i <- j - 1
5          while i > 0 and A[i] > key
6           do temp <- A[i+1]
7              A[i+1] <- A[i]
8              A[i] <- temp
9              i <- i -1
10         A[i + 1] <- key

我做错了什么吗? 请帮忙

in an Introduction to Algorithm 2nd edition, I found insertion sort pseudo code

INSERTION-SORT(A)
1   for j <- 2 to length[A]
2       do key <- A[j]
3          //Insert A[j] into the sorted sequence A[1 □ j - 1].
4          i <- j - 1
5          while i > 0 and A[i] > key
6           do A[i+1] <- A[i]
7              i <- i -1
8          A[i + 1] <- key

but I can't understand how swap works here.

I think it needs a swap operation like this

INSERTION-SORT(A)
1   for j <- 2 to length[A]
2       do key <- A[j]
3          //Insert A[j] into the sorted sequence A[1 □ j - 1].
4          i <- j - 1
5          while i > 0 and A[i] > key
6           do temp <- A[i+1]
7              A[i+1] <- A[i]
8              A[i] <- temp
9              i <- i -1
10         A[i + 1] <- key

did I get something wrong?
please help

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

岁月蹉跎了容颜 2025-01-05 03:57:41

插入排序中发生的不是交换。

它将大于要插入的项的每一项从当前排序部分的末尾向下移动一个索引,然后在旧值向上移动后将新记录插入到正确的位置。

What's happening in insertion sort is not a swap.

It is moving each item greater than the one you want to insert up by one index working down from the end of the currently sorted section, and then inserting the new record at the correct place after the old value is moved up.

心的憧憬 2025-01-05 03:57:41

但我不明白交换在这里是如何工作的。

不,事实并非如此。

该值在一开始就已保存。

它保存j,然后移动所有其他元素,直到找到正确的位置

but I can't understand how swap works here.

No it does not.

The value is already saved in the beggining.

It saves j and then shifts all the other elements until it finds the proper place

慈悲佛祖 2025-01-05 03:57:41

该代码正在执行“多次交换”,更准确地说,将 ak 元素就地旋转一个位置。这需要一个辅助 key 变量,就像交换一样。

The code is doing a "multi-swap", more precisely a rotation of a k elements by one position, in-place. This takes a single auxiliary key variable, like a swap does.

吃颗糖壮壮胆 2025-01-05 03:57:41

我遇到同样的问题。下面是正确实现上述伪代码的 C 代码。

棘手的部分是伪代码使用基于 1 的数组表示法,这与大多数编程语言不同!

#include <stdio.h>

int main(void)
{
  int A[] = { 50, 20, 10, 40, 60, 30 };
  int j, key, len, i;
  len = (sizeof(A)) / (sizeof(A[0]));

    for (j = 1; j < 6; j++) {  <-- Change here
      key = A[j];
      // Insert key into the sorted sequence A[1 .. j - 1].
      i = j - 1;
      while (i >= 0 && A[i] > key) {  <-- Change here
          A[i + 1] = A[i];
          i--;
      }
      A[i + 1] = key;
    }

    for (int z = 0; z < len; z++) {
       printf("%d ", A[z]);
    }
   printf("\n");
 }

I experience the same problem. Below is the code in C which implements the above pseudo-code correctly.

The tricky part about this was that the pseudo code is using 1-based array notations unlike most programming languages!

#include <stdio.h>

int main(void)
{
  int A[] = { 50, 20, 10, 40, 60, 30 };
  int j, key, len, i;
  len = (sizeof(A)) / (sizeof(A[0]));

    for (j = 1; j < 6; j++) {  <-- Change here
      key = A[j];
      // Insert key into the sorted sequence A[1 .. j - 1].
      i = j - 1;
      while (i >= 0 && A[i] > key) {  <-- Change here
          A[i + 1] = A[i];
          i--;
      }
      A[i + 1] = key;
    }

    for (int z = 0; z < len; z++) {
       printf("%d ", A[z]);
    }
   printf("\n");
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文