【问题标题】:Java Mergesort stringsJava 合并排序字符串
【发布时间】:2012-01-17 11:39:46
【问题描述】:

我正在尝试创建一种方法,允许我使用合并排序对 Java 中的字符串数组进行排序。我查看了一些代码示例并制作了自己的算法,但它似乎不起作用,而且我很难找到问题。代码如下:

/*
 * Sorting methods, implemented using mergesort to sort the array of names
 * alphabetically
 */
public String[] sort(String[] array) {
    // check if the number of elements < 2
    if (array.length > 1) {

        // divide into two smaller arrays
        // determine length of two smaller arrays
        int arrLen1 = array.length;
        int arrLen2 = array.length - arrLen1;

        // populate smaller arrays with elements from initial array
        String[] array1 = new String[arrLen1];
        String[] array2 = new String[arrLen2];

        for (int i = 0; i < arrLen1; i++) {
            array[i] = array1[i];
        }

        for (int i = arrLen1; i < array.length; i++) {
            array[i] = array2[i];
        }

        // now that we have the two halves, we can recursively call sort() on each to sort them
        array1 = sort(array1);
        array2 = sort(array2);

        // three counters are needed to keep track of where we are in the arrays, one for pos in final array, and one for each of the two halves
        // i => pos in main array
        // j => pos in array1
        // k => pos in array2
        int i = 0, j = 0, k = 0;

        while (array1.length != j && array2.length != k) {
            if (array1[i].compareTo(array2[i]) < 0) {
                // copy current element of array1 to final array as it preceeds array2's current element
                array[i] = array1[j];

                // increment the final array so we dont overwrite the value we just inserted
                i++;
                // increment array1 which we took the element from so we dont compare it again
                j++;
            }
            // If the element in array2 preceeds the element in array1

            else {
                // copy current element of array1 to final array as it preceeds array1's current element
                array[i] = array2[j];
                // increment the final array so we dont overwrite the value we just inserted
                i++;
                // increment array2 which we took the element from so we dont compare it again
                k++;
            }
        }

        // at this point one of the sub arrays have been exhausted, and no more elements to compare
        while (array1.length != j) {
            array[i] = array1[j];
            i++;
            j++;
        }

        while (array2.length != k) {
            array[i] = array2[k];
            i++;
            k++;

        }
    }

    return array;
}

【问题讨论】:

  • 尝试在每次排序调用中添加日志记录,并使用您知道它应该如何表现的小数组进行测试。

标签: java string sorting mergesort


【解决方案1】:

对于一个你在实际进行任何比较之前尝试递归排序的人。

你的代码是这样写的,array1总是等于array,然后你再次在array1上调用sort,这意味着你只是不断地绕圈子。

【讨论】:

    猜你喜欢
    • 2013-03-31
    • 1970-01-01
    • 2020-01-28
    • 2013-06-04
    • 2012-11-29
    • 2012-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多