简体   繁体   English

你如何实现快速排序以降序排序(java)?

[英]How do you implement quick sort to sort in descending order (java)?

This current quicksort implementation will sort an array to be in ascending order.当前的快速排序实现将对数组进行升序排序。 How do I change this code so that it will sort an array to be in descending order?如何更改此代码,以便它将数组排序为降序? I need some help reversing the logic.我需要一些帮助来扭转逻辑。 Advice would be much appreciated.建议将不胜感激。

    public void quickSort(int[] array) {
    
    // An array of size 1 is already sorted                                                                             
    if (array.length < 2)
        return;
    
    // Find the largest element and put it at the end of the array                                                      
    int max = 0;
    for (int i = 1; i < array.length; i++) {
        if (array[i] > array[max]) {
            max = i;
        }
    }
    swap(array, array.length-1, max);
    
    // Call the main quicksort method                                                                                   
    quicksort(array, 0, array.length-1);
}

public void quicksort(int[] array, int first, int last) {
    
    int lower = first + 1, upper = last;
    
    // Use the middle array element as the bound (pivot) and                                                            
    // move it out of the way into the first array element                                                              
    swap(array, first, (first + last)/2);
    int bound = array[first];
    
    // Partition the array                                                                                              
    while (lower <= upper) {
        while (array[lower] < bound) {
            lower++;
        }
        while (array[upper] > bound) {
            upper--;
        }
        if (lower < upper) {
            swap(array, lower++, upper--);
        }
        else {
            lower++;
        }
    }
    
    // Move the pivot into its proper position in the array                                                             
    swap(array, upper, first);
    
    // Recursively sort the lower and upper subarrays                                                                   
    if (first < upper-1) {
        quicksort(array, first, upper-1);
    }

    if ((upper+1) < last) {
        quicksort(array, upper+1, last);
    }
}

So the basic concept would be to modify...所以基本概念是修改......

while (array[lower] < bound) {
    lower++;
}
while (array[upper] > bound) {
    upper--;
}

to match your requirements, but it's not particularly reusable.以满足您的要求,但它并不是特别可重用。 Instead, we want to delegate the comparison so that we can use the same code, but which can allow for different algorithms to be used, for example...相反,我们希望委托比较,以便我们可以使用相同的代码,但可以允许使用不同的算法,例如......

public class QuickSort {

    public static interface Matcher {

        public int compare(int lhs, int rhs);
    }

    public static void sort(int[] array, Matcher matcher) {

        // An array of size 1 is already sorted                                                                             
        if (array.length < 2) {
            return;
        }

        // Find the largest element and put it at the end of the array                                                      
        int max = 0;
        for (int i = 1; i < array.length; i++) {
            if (matcher.compare(array[i], array[max]) > 0) {
                max = 1;
            }
        }
        swap(array, array.length - 1, max);

        // Call the main quicksort method                                                                                   
        sort(array, 0, array.length - 1, matcher);
    }

    protected static void sort(int[] array, int first, int last, Matcher matcher) {

        int lower = first + 1, upper = last;

        // Use the middle array element as the bound (pivot) and                                                            
        // move it out of the way into the first array element                                                              
        swap(array, first, (first + last) / 2);
        int bound = array[first];

        // Partition the array                                                                                              
        while (lower <= upper) {
            while (matcher.compare(array[lower], bound) < 0) {
                lower++;
            }
            while (matcher.compare(array[upper], bound) > 0) {
                upper--;
            }
            if (lower < upper) {
                swap(array, lower++, upper--);
            } else {
                lower++;
            }
        }

        // Move the pivot into its proper position in the array                                                             
        swap(array, upper, first);

        // Recursively sort the lower and upper subarrays                                                                   
        if (first < upper - 1) {
            sort(array, first, upper - 1, matcher);
        }

        if ((upper + 1) < last) {
            sort(array, upper + 1, last, matcher);
        }
    }

    protected static void swap(int[] values, int first, int second) {
        int temp = values[first];
        values[first] = values[second];
        values[second] = temp;
    }
}

Which can then be used something like...然后可以使用类似...

int[] values = new int[]{1, 0, 2, 9, 3, 8, 4, 7, 5, 6};

QuickSort.sort(values, new QuickSort.Matcher() {
    @Override
    public int compare(int lhs, int rhs) {
        return Integer.compare(lhs, rhs);
    }
});
System.out.println(Arrays.toString(values));

QuickSort.sort(values, new QuickSort.Matcher() {
    @Override
    public int compare(int lhs, int rhs) {
        return Integer.compare(rhs, lhs);
    }
});
System.out.println(Arrays.toString(values));

which will output...这将输出...

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

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

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