在字符串数组上使用快速排序

6

我是一名编程学生,需要进行一个有关快速排序的字符串数组排序问题。在测试排序方法时,发现打印出来的字符串数组完全混乱无序,毫无规律可言。希望您能够帮助我找到代码错误或者我可能忽视的几个明显错误。提供的字符串数组列表包含以下65个名称:http://pastebin.com/jRrgeV1E,下面是排序方法的代码:

private static void quickSort(String[] a, int start, int end)
{
        // index for the "left-to-right scan"
        int i = start;
        // index for the "right-to-left scan"
        int j = end;

        // only examine arrays of 2 or more elements.
        if (j - i >= 1)
        {
            // The pivot point of the sort method is arbitrarily set to the first element int the array.
            String pivot = a[i];
            // only scan between the two indexes, until they meet.
            while (j > i)
            {
                // from the left, if the current element is lexicographically less than the (original)
                // first element in the String array, move on. Stop advancing the counter when we reach
                // the right or an element that is lexicographically greater than the pivot String.
                while (a[i].compareTo(pivot) < 0 && i <= end && j > i){
                    i++;
                }
                // from the right, if the current element is lexicographically greater than the (original)
                // first element in the String array, move on. Stop advancing the counter when we reach
                // the left or an element that is lexicographically less than the pivot String.
                while (a[j].compareTo(pivot) > 0 && j >= start && j >= i){
                    j--;
                }
                // check the two elements in the center, the last comparison before the scans cross.
                if (j > i)
                    swap(a, i, j);
            }
            // At this point, the two scans have crossed each other in the center of the array and stop.
            // The left partition and right partition contain the right groups of numbers but are not
            // sorted themselves. The following recursive code sorts the left and right partitions.

            // Swap the pivot point with the last element of the left partition.
            swap(a, start, j);
            // sort left partition
            quickSort(a, start, j - 1);
            // sort right partition
            quickSort(a, j + 1, end);
        }
    }
    /**
     * This method facilitates the quickSort method's need to swap two elements, Towers of Hanoi style.
     */
    private static void swap(String[] a, int i, int j)
    {
    String temp = a[i];
    a[i] = a[j];
    a[j] = temp;
    }

你的确切问题是什么?它没有排序吗?它是否显示多次值,因为我已经测试过了,它似乎工作正常。 - SomeJavaGuy
它对你起作用了吗?也许我应该发布我的代码的其余部分...当我“排序”后将其打印出来时,它对我显示为这个顺序:http://pastebin.com/5969hxGs,这太奇怪了! - Mackenzie
2个回答

7

好的,我错了,我以为它会起作用并发现了你微小的错误。

看一下维基百科的伪代码

你会注意到while循环中的条件导致了错误。

如果你将(a[i].compareTo(pivot) < 0 && i <= end && j > i)(a[j].compareTo(pivot) > 0 && j >= start && j > i)改为

(a[i].compareTo(pivot) <= 0 && i < end && j > i)(a[j].compareTo(pivot) >= 0 && j > start && j >= i)


1
非常感谢!现在它对我也起作用了,我发现只是简单的一个偏移错误让我功亏一篑。非常感谢,现在我可以安心休息了! - Mackenzie

1

如果你正在寻找基于快速排序方法的字符串排序算法,那么这篇文章可能会对你有所帮助。

public class StringQuickSort {

    String names[];
    int length;

    public static void main(String[] args) {
        StringQuickSort sorter = new StringQuickSort();
        String words[] = {"zz", "aa", "cc", "hh", "bb", "ee", "ll"}; // the strings need to be sorted are put inside this array
        sorter.sort(words);

        for (String i : words) {
            System.out.print(i);
            System.out.print(" ");
        }
    }

    void sort(String array[]) {
        if (array == null || array.length == 0) {
            return;
        }
        this.names = array;
        this.length = array.length;
        quickSort(0, length - 1);
    }

    void quickSort(int lowerIndex, int higherIndex) {
        int i = lowerIndex;
        int j = higherIndex;
        String pivot = this.names[lowerIndex + (higherIndex - lowerIndex) / 2];

        while (i <= j) {
            while (this.names[i].compareToIgnoreCase(pivot) < 0) {
                i++;
            }

            while (this.names[j].compareToIgnoreCase(pivot) > 0) {
                j--;
            }

            if (i <= j) {
                exchangeNames(i, j);
                i++;
                j--;
            }
        }
        //call quickSort recursively
        if (lowerIndex < j) {
            quickSort(lowerIndex, j);
        }
        if (i < higherIndex) {
            quickSort(i, higherIndex);
        }
    }

    void exchangeNames(int i, int j) {
        String temp = this.names[i];
        this.names[i] = this.names[j];
        this.names[j] = temp;
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接