簡體   English   中英

你如何實現快速排序以降序排序(java)?

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

當前的快速排序實現將對數組進行升序排序。 如何更改此代碼,以便它將數組排序為降序? 我需要一些幫助來扭轉邏輯。 建議將不勝感激。

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

所以基本概念是修改......

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

以滿足您的要求,但它並不是特別可重用。 相反,我們希望委托比較,以便我們可以使用相同的代碼,但可以允許使用不同的算法,例如......

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

然后可以使用類似...

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

這將輸出...

[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