当前位置: 首页 > 知识库问答 >
问题:

合并排序算法不工作

姚阳德
2023-03-14

我正在尝试实现一个不能正常工作的mergesort算法。合并排序的工作方式如下:

i、 将未排序的列表划分为n个子列表,每个子列表包含1个元素(1个元素的列表被视为已排序)。

ii.重复合并子列表以产生新排序的子列表,直到只剩下1个子列表。这将是已排序的列表。下面提供了实现。

最初,递归调用此方法,直到只有一个元素。

public void mergeSort(int low, int high) {

    if (low >= high) {
        return;
    }

    int middle = (low + high) / 2;

    // recursion for the merge
    mergeSort(low, middle);
    mergeSort(middle + 1, high);

    merge(low, middle, high);
}

这是提供的合并方法。

private void merge(int low, int middle, int high) {

    int i = low, j = middle;
    int index = low;

    for (int k = 0; k <= high; k++) {
        tempArray[k] = nums[k];
    }

    /*
      Copy the smallest values from either the left
      or the right side back to the original array
     */
    while ((i <= middle) && (j <= high)) {
        if (tempArray[i] <= tempArray[j]) {
            nums[index++] = tempArray[i++];
        } else {
            nums[index++] = tempArray[j++];
        }
    }

    // fill the left side of the array
    while (i < middle) {
        nums[index++] = tempArray[i++];
    }

    // fill the right side of the array
    while (j < high) {
        nums[index++] = tempArray[j++];
    }
  }

这里的问题是什么?

输入是int[]arr={12, 3, 4, 56, -7, 1};输出是12 12 12 12 56 56

共有1个答案

习华灿
2023-03-14

我修改了合并函数,现在它开始工作了。特别是,需要在中间项j=中间1之后初始化j

 private void merge(int low, int middle, int high) {

    int i = low, j = middle+1;
    int index = low;

    for (int k = 0; k <= high; k++) {
        tempArray[k] = nums[k];
    }

    /*
      Copy the smallest values from either the left
      or the right side back to the original array
     */
    while ((i <= middle) && (j <= high)) {
        if (tempArray[i] <= tempArray[j]) {
            nums[index++] = tempArray[i++];
        } else {
            nums[index++] = tempArray[j++];
        }
    }

    // fill the left side of the array
    while (i <= middle) {
        nums[index++] = tempArray[i++];
    }

    // fill the right side of the array
    while (j <= high) {
        nums[index++] = tempArray[j++];
    }
}
 类似资料:
  • 我知道合并排序算法的基本概念,但是当涉及到通过递归实现它时,我很难理解它是如何工作的。据我所知,合并排序函数将我们当前的数组分成两半,并使用递归我们一直这样做,直到每边只剩下一个元素。 如果我们的数组是{38、27、43、3、9、82、10},那么我们的递归将从使用子数组(原始数组的左侧)调用自身开始,并每次重复该过程,将数组减半并存储最左侧,直到达到1个元素: 然后在我们的第二个子例程中,我们继

  • 我在理解外部排序算法中的合并步骤时遇到了一定的困难。我在维基百科上看到了这个例子,但我无法理解。 外部排序的一个例子是外部合并排序算法,它对每个适合RAM的块进行排序,然后将排序后的块合并在一起。例如,对于仅使用100 MB RAM对900 MB数据进行排序:1)读取主内存中的100 MB数据,并通过一些常规方法进行排序,如快速排序。2) 将排序后的数据写入磁盘。3) 重复第1步和第2步,直到所有

  • 问题是关于从16:43到23:34的视频中的合并排序http://youtu.be/M814OagXWTI?t=16m43s 在退出左/右排序合并递归后,我不清楚我们是如何合并回这些子数组的。让我们从最底部开始,当我们的元素被分成两个子数组时,一个左子数组称为B,一个右子数组称为C。在16:43左右,我们跳转到合并函数,对数组B和C进行排序,这两个数组只有8和3。合并排序函数(下面的代码)基本上通

  • 我正在尝试理解外部合并排序算法是如何工作的(我看到了相同问题的一些答案,但没有找到我需要的东西)。我正在阅读Jeffrey McConnell的《算法分析》一书,我正在尝试实现那里描述的算法。 例如,我有输入数据:,我只能将4个数字加载到内存中。 我的第一步是以4个数字块读取输入文件,在内存中对它们进行排序,然后将其中一个写入文件A和文件B。 我得到: 现在我的问题是,如果这些文件中的块不适合内存

  • 我有个小问题。我尝试实现合并排序算法递归。 现在我的问题: 左=合并排序(rrays.copyOfRange(iv_sort_list,0,iv_sort_list.length));右=合并排序(rrays.copyOfRange(iv_sort_list,iv_sort_list.length,iv_sort_list.length)); 如果我尝试分配我的左/右数组“mergeSort(..

  • 这些是家庭作业问题,但我想了解它们背后的概念,而不仅仅是得到答案。 我知道MergeSort的运行时间是O(nlogn)。似乎合并方法必须运行 n 次(因为它必须合并所有数组,最终会有 n 个数组)。因此,我想我可以推断出 MergeSort() 方法将被称为 logn times。我也认为这是有道理的,因为它正在划分数组,所以它会一直将自己除以 2,所以 logn。 因此,我觉得答案分别是C和A