【问题标题】:How to merge and print two sorted arrays of ints如何合并和打印两个排序的整数数组
【发布时间】:2019-09-03 18:50:20
【问题描述】:

我正在尝试创建一个方法,该方法接受两个已排序的 int 数组并返回一个新数组,该数组在不使用排序函数的情况下合并和重新排序这两个列表。我在我的循环中遇到问题,我不知道如何解决它。

我目前遇到错误:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
    at pack8.Assignment8Code.merge(Assignment8Code.java:20)
    at pack8.Assignment8Code.main(Assignment8Code.java:39)

代码如下:

public class Assignment8Code 
{   
    public static int[] merge(int[] arr1, int[] arr2)
    {
        //Create the first two arrays with testing numbers
        arr1 = new int[5];
        arr2 = new int[3];
        //Create a new array that will fit the length of the two given arrays
        int[] sortedArray = new int[(arr1.length + arr2.length)];
        //Create starting index values to check all arrays and merge
        int index1 = 0;
        int index2 = 0;

        //Test to see if the given arrays are populated
        while(index1 < arr1.length || index2 < arr2.length)
        {
            //Check to see which array starts with the higher number
            if(arr1[index1] < arr2[index2])
            {
                sortedArray[index1] = arr1[index1];
                index1++;
            }
            else
            {
                sortedArray[index2] = arr2[index2];
                index2++;
            }
        }
        return sortedArray;
    }
}

【问题讨论】:

  • 为什么要立即丢弃参数?删除这两个 new int 语句。 --- while(index1 &lt; arr1.length || index2 &lt; arr2.length) 表示即使其中 一个 结束,您也会继续。那么你为什么没想到if(arr1[index1] &lt; arr2[index2]) 在结束时会失败呢?如果两个输入都有要比较的值,则只能比较这两个值。
  • 您还需要在输出数组中保留一个单独的索引,而不是使用index1index2
  • @Kevin 或使用 index1 + index2

标签: java arrays sorting merge mergesort


【解决方案1】:

您的代码中存在多个问题:

  • 您应该使用单独的方法进行测试,在merge 方法中分配arr1arr2 会违背其目的。
  • 您必须为目标数组使用单独的索引。
  • 当您到达任一数组的末尾时,您应该停止比较元素
  • 您应该单独处理剩余的元素。

这是merge 方法的更正版本:

public class Assignment8Code 
{   
    public static int[] merge(int[] arr1, int[] arr2)
    {
        // Allocate a new array that will fit the length of the two given arrays
        int[] sortedArray = new int[(arr1.length + arr2.length)];

        int index1 = 0;
        int index2 = 0;
        int index3 = 0;

        // Merge the sorted arrays
        while (index1 < arr1.length && index2 < arr2.length)
        {
            //Check to see which array starts with the higher number
            if (arr1[index1] <= arr2[index2])
            {
                sortedArray[index3++] = arr1[index1++];
            }
            else
            {
                sortedArray[index3++] = arr2[index2++];
            }
        }
        // Append the remaining elements 
        while (index1 < arr1.length)
        {
            sortedArray[index3++] = arr1[index1++];
        }
        while (index2 < arr2.length)
        {
            sortedArray[index3++] = arr2[index2++];
        }
        return sortedArray;
    }
}

请注意,这种方法效率很低,因为它为每次合并分配一个新数组。以这种方式为大型数组实现合并排序会分配大量的 mmemory(log2(N) * N 整数),从而对垃圾收集器造成不必要的压力。对整个归并排序使用单个临时数组会更有效,但需要不同的 merge 方法。

【讨论】:

    猜你喜欢
    • 2022-11-17
    • 1970-01-01
    • 2021-12-09
    • 1970-01-01
    • 2021-10-11
    • 2015-03-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多