需要帮助理解插入排序

发布于 2024-08-19 16:39:49 字数 1055 浏览 5 评论 0原文

在这段代码中,这是如何工作的(java):

/** Move A[A.length-1] to the first position, k, in A such that there
* are no smaller elements after it, moving all elements
* A[k .. A.length-2] over to A[k+1 .. A.length-1]. */

static void moveOver (int A[]) {
   moveOver (A, A.length-1);
}

/** Move A[U] to the first position, k<=U, in A such that there
* are no smaller elements after it, moving all elements
* A[k .. U-1] over to A[k+1 .. U]. */

static void moveOver (int A[], int U) {
  if (U > 0) {
    if (A[U-1] > A[U]) {
      /* Swap A[U], A[U-1] */
     moveOver (A, U-1);
    }
  }
}

我从我正在网上自学的伯克利计算机课程中得到了这个。这不是家庭作业(我希望是,但没有那么幸运)。我不明白的是:

假设 A[] 中的数字是 8, 2, 10, 5, 4, 12。当我在上面使用它们时,我会在迭代中得到它,并跟踪它。

  1. 最上面的下标是 U,或者在 本例为 12,U-1 为 4,无交换 完成
  2. U 现在是 4(递归 U-1),上面的数字是 5(另一个 U-1)。他们被交换了。
  3. U 现在是 4,因为 4 刚刚上升,而 10 是 U-1,他们被交换了。

我的序列现在是 8,2,4,10,5,12。

我的问题是如何获得我已经经过的号码?例如,如果我不再回到该下标进行测试,我将如何获得五个提升。

我认为我没有正确跟踪程序,并且我对递归感到困惑。为此,请假设交换已正确完成。

谢谢。

In this code how does this work (java):

/** Move A[A.length-1] to the first position, k, in A such that there
* are no smaller elements after it, moving all elements
* A[k .. A.length-2] over to A[k+1 .. A.length-1]. */

static void moveOver (int A[]) {
   moveOver (A, A.length-1);
}

/** Move A[U] to the first position, k<=U, in A such that there
* are no smaller elements after it, moving all elements
* A[k .. U-1] over to A[k+1 .. U]. */

static void moveOver (int A[], int U) {
  if (U > 0) {
    if (A[U-1] > A[U]) {
      /* Swap A[U], A[U-1] */
     moveOver (A, U-1);
    }
  }
}

I got this from a berkeley cs class I am going through online, teaching myself. it is not homework (i wish was but not that fortunate). what I don't understand is the following:

suppose the numbers in A[] are 8, 2, 10, 5, 4, 12. When I use them above I get this in my iterations, tracing it.

  1. the upper most subscript is U, or in
    this case 12, U-1 is 4, no swap is
    done
  2. U is now 4 (recursive U-1) and the number above it is 5 (the other U-1). they get swapped.
  3. U is now 4 because four just moved up and 10 is U-1 they get swapped.

My sequence is now 8,2,4,10,5,12.

My question is how do I get the numbers I already passed by? how will I get five, for example, to move up if I never go back to that subscript to test with.

I don't think I am tracing the program correctly and my be getting confused with the recursion. For the sake of this please assume the swap is done correctly.

Thank you.

se

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

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

发布评论

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

评论(2

离旧人 2024-08-26 16:39:49

我认为你对问题的误解的关键实际上隐藏在你的问题标题中

需要帮助理解插入排序

该算法确实只对当前元素进行排序,但其想法是每次将元素添加到数组时都会运行。这样每次调用时数组的其余部分就已经按顺序排列了。换句话说,您只是尝试对最后一个元素进行排序。

因此,使用您的示例数字 (8, 2, 10, 5, 4, 12) 并将它们按顺序一次添加/排序到数组中,顺序将如下所示(每一步的排序完全按照您已经进行的方式进行)描述):

To be added         | old array      | after push     | Result (after moveOver())
(8, 2, 10, 5, 4, 12)| []             | [8]            | [8]
(2, 10, 5, 4, 12)   | [8]            | [8,2]          | [2,8]
(10, 5, 4, 12)      | [2,8]          | [2,8,10]       | [2,8,10]
(5, 4, 12)          | [2,8,10]       | [2,8,10,5]     | [2,5,8,10]
(4, 12)             | [2,5,8,10]     | [2,5,8,10,4]   | [2,4,5,8,10]
(12)                | [2,4,5,8,10]   | [2,4,5,8,10,12]| [2,4,5,8,10,12]

I think the key to your mis-understanding of the problem is actually hidden in the title of your question

Needing help understanding insertion sort

The algorithm indeed only sorts the current element, but the idea is that its run every time an element is added to the array. That way each time its called the rest of the array is already in order. In other words you are only trying to sort into position that last element.

So using your example numbers (8, 2, 10, 5, 4, 12) and adding/sorting them into the array one at a time in order, the sequence would be as follows (the sorting at each step happens exactly how you already describe):

To be added         | old array      | after push     | Result (after moveOver())
(8, 2, 10, 5, 4, 12)| []             | [8]            | [8]
(2, 10, 5, 4, 12)   | [8]            | [8,2]          | [2,8]
(10, 5, 4, 12)      | [2,8]          | [2,8,10]       | [2,8,10]
(5, 4, 12)          | [2,8,10]       | [2,8,10,5]     | [2,5,8,10]
(4, 12)             | [2,5,8,10]     | [2,5,8,10,4]   | [2,4,5,8,10]
(12)                | [2,4,5,8,10]   | [2,4,5,8,10,12]| [2,4,5,8,10,12]
尴尬癌患者 2024-08-26 16:39:49

至于您的跟踪:您在第 2 点出错,因为 if 条件不成立,因此不会进行递归调用。

如果递归让您感到困惑,重写为迭代形式可能会有所帮助:

static void moveOver (int A[], int U) {
  while (U > 0 && A[U-1] > A[U]) {
    /* Swap A[U], A[U-1] */
    --U;
  }
}

现在很容易看出,一旦遇到较小的元素,循环就会停止。要成为一个完整的排序算法,还需要做更多的工作。不管怎样,这都不是插入排序;而是插入排序。对我来说,它看起来更像是部分冒泡排序。

现在你说你从哪里得到这个代码?

As to your trace: you are going wrong at point 2, because the if condition does not hold, so no recursive call is ever made.

If the recursion confuses you, it may help to rewrite into an iterative form:

static void moveOver (int A[], int U) {
  while (U > 0 && A[U-1] > A[U]) {
    /* Swap A[U], A[U-1] */
    --U;
  }
}

Now it's easy to see that the loop will stop as soon as a smaller element is encountered. For this to be a complete sorting algorithm, more work is needed. Either way, this is not insertion sort; it looks more like a partial bubble sort to me.

Now where did you say you got this code from?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文