简体   繁体   English

如何修复重复的快速排序算法

[英]How do i fix my duplicating QuickSort algorithm

Im having some problems with figuring out what is wrong with the following quicksort.我在弄清楚以下快速排序有什么问题时遇到了一些问题。 The problem exists it doesnt sort everything correctely and even looks like it duplicates numbers存在问题,它没有正确排序所有内容,甚至看起来像重复数字

public static List<Archer> quickSort(List<Archer> archers, Comparator<Archer> scoringScheme) {
        var sortedArray = archers.toArray(new Archer[archers.size()]);
        quickSort(sortedArray, 0, archers.size(), scoringScheme);
        return Arrays.asList(sortedArray);


    }
 private static void quickSort(Archer[] array, int min, int max, Comparator<Archer> scoringScheme) {
        if (min >= max - 1) {
            return;
        }
        int pivot = (min + max) / 2;
        var pivotItem = array[pivot];
        var lowerIndex = min;
        var higherIndex = max - 1;
        while (lowerIndex <= higherIndex) {
            while (lowerIndex <= higherIndex && scoringScheme.compare(array[lowerIndex], pivotItem) <= 0) {
                lowerIndex++;
            }
            while (higherIndex >= lowerIndex && scoringScheme.compare(array[higherIndex], pivotItem) >= 0) {
                higherIndex--;
            }
            if (lowerIndex < higherIndex) {
                var temp = array[lowerIndex];
                array[lowerIndex] = array[higherIndex];
                array[higherIndex] = temp;
                lowerIndex++;
                higherIndex--;
            }
        }
        if (lowerIndex > pivot) {
            lowerIndex--;
            array[pivot] = array[lowerIndex];
            array[lowerIndex] = pivotItem;
        } else {
            higherIndex++;
            array[pivot] = array[higherIndex];
            array[higherIndex] = pivotItem;
        }
        quickSort(array, min, lowerIndex , scoringScheme);
        quickSort(array, higherIndex +1, max, scoringScheme);

    }

the result is following:结果如下:

Expected [135810 ( 187 / 217), 135810 ( 184 / 214), 135810 ( 183 / 213), 135810 ( 179 / 209), 135810 ( 177 / 207), 135810 ( 176 / 206), 135810 ( 170 / 200), 135810 ( 167 / 197), 135810 ( 167 / 197), 135810 ( 165 / 195), 135810 ( 162 / 192), 135810 ( 161 / 191), 135810 ( 161 / 191), 135810 ( 158 / 188), 135810 ( 158 / 188), 135810 ( 151 / 181), 135810 ( 151 / 181), 135810 ( 150 / 180), 135810 ( 147 / 177), 135810 ( 147 / 177), 135810 ( 146 / 176), 135810 ( 140 / 170), 135810 ( 138 / 168)]
Actual [135810 ( 187 / 217), 135810 ( 187 / 217), 135810 ( 184 / 214), 135810 ( 179 / 209), 135810 ( 177 / 207), 135810 ( 176 / 206), 135810 ( 176 / 206), 135810 ( 167 / 197), 135810 ( 167 / 197), 135810 ( 165 / 195), 135810 ( 165 / 195), 135810 ( 162 / 192), 135810 ( 162 / 192), 135810 ( 158 / 188), 135810 ( 158 / 188), 135810 ( 158 / 188), 135810 ( 158 / 188), 135810 ( 151 / 181), 135810 ( 151 / 181), 135810 ( 150 / 180), 135810 ( 150 / 180), 135810 ( 147 / 177), 135810 ( 147 / 177)]

Here is a simple example of quicksort implementation:这是快速排序实现的一个简单示例:

https://www.baeldung.com/java-quicksort https://www.baeldung.com/java-quicksort

To avoid duplicated values, could you not use one of the implementations of the Set instead of List interface?If so, just implement equals and hashcode methods of you Archer object.为了避免重复值,你能不使用 Set 接口的实现之一而不是 List 接口吗?如果是这样,只需实现 Archer object 的equalshashcode方法。

Without the calling code, I'm not sure about the parameters.没有调用代码,我不确定参数。 Suggested changes noted in comments.建议的更改在评论中注明。 The code would be a bit simpler if the external call was如果外部调用是

    quickSort(array, 0, length-1, compare);

this would eliminate the need for the max-1 in the code.这将消除代码中对 max-1 的需要。

 private static void quickSort(Archer[] array, int min, int max, Comparator<Archer> scoringScheme) {
        if (min >= max - 1) {
            return;
        }
        int pivot = (min + max) / 2;        // may need to be (min+max-1)/2
        var pivotItem = array[pivot];
        var lowerIndex = min;
        var higherIndex = max - 1;
        while (lowerIndex <= higherIndex) {
            while (scoringScheme.compare(array[lowerIndex], pivotItem) < 0) {  // change
                lowerIndex++;
            }
            while (scoringScheme.compare(array[higherIndex], pivotItem) > 0) { // change
                higherIndex--;
            }
            if (lowerIndex <= higherIndex) {                // change (<=)
                var temp = array[lowerIndex];
                array[lowerIndex] = array[higherIndex];
                array[higherIndex] = temp;
                lowerIndex++;
                higherIndex--;
            }
        }
        // change ... deleted 9 lines
        quickSort(array, min, higherIndex+1 , scoringScheme);  // change
        quickSort(array, lowerIndex, max, scoringScheme);      // change

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM