【问题标题】:Using quicksort on a string array在字符串数组上使用快速排序
【发布时间】:2015-05-31 10:40:16
【问题描述】:

我是一名编程专业的学生,​​我不会发布整个作业,而是寻求帮助来解决我已经尝试了几个小时才能理解的内容。我的任务是使用快速排序方法对字符串数组进行排序。作为这个问题的一部分,我所承担的所有其他任务都很好,但是当我通过打印出字符串数组来测试排序方法时,它完全混乱了,没有任何看似押韵或理由。请帮助我查明代码中的错误,或者我忽略的几个明显错误。提供的字符串数组是 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;
    }

【问题讨论】:

  • 您的具体问题是什么?它不排序。它是否多次显示值,因为我对其进行了测试并且它似乎工作正常
  • 它对你有用??也许我应该发布我的其余代码......当我在“排序”后打印出来时,它会为我显示这个顺序:pastebin.com/5969hxGs 这太奇怪了!

标签: java arrays string sorting quicksort


【解决方案1】:

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

看看wikipedias pseudo code

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

如果您将(a[i].compareTo(pivot) &lt; 0 &amp;&amp; i &lt;= end &amp;&amp; j &gt; i)(a[j].compareTo(pivot) &gt; 0 &amp;&amp; j &gt;= start &amp;&amp; j &gt;= i) 更改为

(a[i].compareTo(pivot) &lt;= 0 &amp;&amp; i &lt; end &amp;&amp; j &gt; i)(a[j].compareTo(pivot) &gt;= 0 &amp;&amp; j &gt; start &amp;&amp; j &gt;= i)

【讨论】:

  • 非常感谢!它现在也适用于我,我发现它只是简单地被我撤消的一个错误所取代。谢谢一百万,现在我可以高枕无忧了!
【解决方案2】:

认为这对那些寻求基于快速排序方法的字符串排序算法的人有所帮助。

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;
    }
}

【讨论】:

    猜你喜欢
    • 2019-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-01
    • 1970-01-01
    • 2022-01-21
    相关资源
    最近更新 更多