在我的 Java 合并排序功能中,即使条件为假,它也会进入循环。为什么?

在我的“合并”函数中,在while循环中,给定的条件是a!=null && b!=null,但是当b为null时,它仍然进入while循环,然后给出错误。


int[] merge(int[]a, int[]b)

{

    int length = a.length+b.length;

    int[]c = new int[length];

    while (a!=null && b!=null)

    {

        if (a[0]<b[0])

        {

            c[length-1]=a[0];

            a = tail(a);

        }

        else

        {

            c[length-1]=b[0];

            b = tail(b);

        }

    }

    return c;

}


int[] mergeSort(int[]a)

{

    if (a.length==1)

        return a;


    int[] q = new int[a.length];

    int[] l = new int[a.length/2];

    int [] r = new int[a.length-l.length];


    for (int i=0; i<l.length; i++)

    {

        l[i] = a[i];

    }

    for (int i=l.length; i<r.length; i++)

    {

        r[i-l.length] = a[i];

    }

    q = merge(mergeSort(l), mergeSort(r));

    return q;

}


慕仙森
浏览 87回答 2
2回答

HUWWW

a我认为,当数组之一(或者或b)已经为空,而另一个 - 仍然有元素时,您将面临这种情况。所以,在merge函数中你应该考虑到这一点。这是您的完整实现mergeSort:public class Answer {&nbsp; public static void main(String[] args) {&nbsp; &nbsp; int[] unsorted = new int[]{4,3,4,2,1,1,3,3,3,3,3,5,6,6,9,9,10,7,7,8};&nbsp; &nbsp; System.out.println(Arrays.toString(mergeSort(unsorted)));&nbsp; }&nbsp; public static int[] mergeSort(int[]a) {&nbsp; &nbsp; if (a.length == 1) return a;&nbsp; &nbsp; int[] q = new int[a.length];&nbsp; &nbsp; int[] l = new int[a.length / 2];&nbsp; &nbsp; int [] r = new int[a.length - l.length];&nbsp; &nbsp; for (int i = 0; i < l.length; i++) {&nbsp; &nbsp; &nbsp; l[i] = a[i];&nbsp; &nbsp; }&nbsp; &nbsp; for (int i = 0; i < r.length; i++) {&nbsp; &nbsp; &nbsp; r[i] = a[l.length + i];&nbsp; &nbsp; }&nbsp; &nbsp; q = merge(mergeSort(l), mergeSort(r));&nbsp; &nbsp; return q;&nbsp; }&nbsp; private static int[] merge(int[]a, int[]b) {&nbsp; &nbsp; int length = a.length + b.length;&nbsp; &nbsp; int[] c = new int[length];&nbsp; &nbsp; int p = 0;&nbsp; &nbsp; while (a.length > 0 && b.length > 0) {&nbsp; &nbsp; &nbsp; if (a[0] < b[0]) {&nbsp; &nbsp; &nbsp; &nbsp; c[p++] = a[0];&nbsp; &nbsp; &nbsp; &nbsp; a = tail(a);&nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; c[p++] = b[0];&nbsp; &nbsp; &nbsp; &nbsp; b = tail(b);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; while (a.length > 0) {&nbsp; &nbsp; &nbsp; &nbsp;c[p++] = a[0];&nbsp; &nbsp; &nbsp; &nbsp;a = tail(a);&nbsp; &nbsp; }&nbsp; &nbsp; while (b.length > 0) {&nbsp; &nbsp; &nbsp; &nbsp;c[p++] = b[0];&nbsp; &nbsp; &nbsp; &nbsp;b = tail(b);&nbsp; &nbsp; }&nbsp; &nbsp; return c;&nbsp; }&nbsp; private static int[] tail(int[] a) {&nbsp; &nbsp; int[] t = new int[a.length - 1];&nbsp; &nbsp; for (int i = 0; i < t.length; i++) {&nbsp; &nbsp; &nbsp; t[i] = a[i + 1];&nbsp; &nbsp; }&nbsp; &nbsp; return t;&nbsp; }}

互换的青春

其实你的辅助方法有问题merge()。我强烈建议您以此为契机,对您的方法进行系统测试。考虑到我们在编写程序时都会经常犯错误,发现错误以纠正错误是工作的真正组成部分。让事情变得更容易的是对这个事实进行预测:sh*t发生。并花时间写下一些测试用例,包括输入数据和预期结果。int[] empty = {};int [] r1 = merge(empty, empty);System.out.println("Result should be empty\n");printArray(r1);int [] oneElement = { 23 };int [] r3 = merge(oneElement, empty);System.out.println("Result should be [23]\n");printArray(r2);...所以你有自动化测试(我们都懒得在代码第 35 次修改后每次都手动完成°.这将引导您进行“单元测试”....
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java