简体   繁体   中英

How to implement a descending selection sort

The method I have to create should take as parameter an array of integers and return the integer array with its contents sorted in descending order — largest to smallest. Note - no libraries should be used in your implementation for this method.

I've attempted to use a regular selection sort and using a swap at the end but syntax errors just occurred:

public static int[] reverseSelectionSort(int[] arrayToBeSorted) {
    // implementation of Task 3 goes here
    for(int i = 0; i < arrayToBeSorted.length; i++){
        int maxPosition=i;
        int minPosition=i;
        for(int j = i+1; j < arrayToBeSorted.length - i; j++){
            if(arrayToBeSorted[j] < arrayToBeSorted[minPosition]){
                minPosition = j;
            }
            if(arrayToBeSorted[j] > arrayToBeSorted[maxPosition]){
                maxPosition = j;
            }
        }
        swap(arrayToBeSorted,minPosition,maxPosition);
        swap(arrayToBeSorted,maxPosition,i);
        swap(arrayToBeSorted,minPosition,arrayToBeSorted.length-i-1);
    }
    return arrayToBeSorted; // change this to return the sorted array
}

public static void main(String[] args) {
    int[] array2 = {3, 6, 8, 3, 5, 7, 1};
    int[] sorted = reverseSelectionSort(array2);

    System.out.print("task: [");

    for (int i = 0; i < sorted.length; i++) {
        if (i > 0) {
            System.out.print(", ");
        }

        System.out.print(sorted[i]);
    }

    System.out.println("]");
}

If you call the method on the array [3, 6, 8, 3, 5, 7, 1], then the method should return the array [8, 7, 6, 5, 3, 3, 1].

Once you add the implementation of swap to your code (and put it all inside a class), it produces exactly the output you wanted it to. Maybe you thought swap was a java uitl, which it is for Collections, but an array is not a Collection. So very little has changed in the below:

    public class soSelect{ //embed in class, added
public static int[] reverseSelectionSort(int[] arrayToBeSorted) {
    // implementation of Task 3 goes here
    for(int i = 0; i < arrayToBeSorted.length; i++){
    int maxPosition=i;
    int minPosition=i;
    for(int j = i+1; j < arrayToBeSorted.length - i; j++){
        if(arrayToBeSorted[j] < arrayToBeSorted[minPosition]){
            minPosition = j;
        }
        if(arrayToBeSorted[j] > arrayToBeSorted[maxPosition]){
            maxPosition = j;
        }
    }
    swap(arrayToBeSorted,minPosition,maxPosition);
    swap(arrayToBeSorted,maxPosition,i);
    swap(arrayToBeSorted,minPosition,arrayToBeSorted.length-i-1);
}
    return arrayToBeSorted; // change this to return the sorted array
}
public static void swap(int[] a, int i, int j){ //had to implement it
    int temp = a[i];
    a[i] = a[j];
    a[j] = temp;
}
public static void main(String[] args) {
    int[] array2 = {3, 6, 8, 3, 5, 7, 1};
    int[] sorted = reverseSelectionSort(array2);

    System.out.print("task: [");

    for (int i = 0; i < sorted.length; i++) {
        if (i > 0) {
            System.out.print(", ");
        }

        System.out.print(sorted[i]);
    }

    System.out.println("]");
}
}//added outer brace

I ran it in java doodle and it printed:

task: [8, 7, 6, 5, 3, 3, 1]

I did not test beyond that, but this should at least get you unstuck, or, at best, finished. For sure you could compare with the implementation to which the above comment points you.

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