简体   繁体   中英

how do i sort an array in ascending order, but by finding the greatest integer?

public static <T extends Comparable<? super T>> void modifiedSelectionSort(T[] a, int n) {
        for( int index = 0; index < n - 1; index++) {
            int indexOfGreatest = indexofGreatest(a, index, n -1);
            T temp = a[index];
            a[index] = a[indexOfGreatest];
            a[indexOfGreatest] = temp;
        }
    }
private static <T extends Comparable<? super T>> int indexofGreatest(T[] a, int first, int last) {
    T max = a[last];
    int indexOfMax = last;
    for (int index = last; index <= first; index-- ) {
        if (a[index].compareTo(max) > 0) {
                max = a[index];
                indexOfMax = last;
            }
        }
        return indexOfMax;
    }

I want this code to find the greatest integer in the array, put it in the back of the array, then search again and find the second greatest integer and place it in the second to last place and so on

At Line 12 "index" should be ">=" to "first" and not "<=":

 for (int index = last; index >= first; index-- ) {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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