Search code examples
javasortingselection-sort

how to implement a descending selection sort in java?


i want to implement a selection sort method that takes an array of ints and sorts it in a descending order. however, the trick is to keep the original selection sort method unchanged but instead using simple arithmetic operations and without adding extra loops to swap the elements after the array finished sorting. this is my code and the idea is to store the position of the maximum value and the minimum value in local variables and swap them with the corresponding position after the inner loop finishes iteration. i even tried using a only one variable to find the lowest value and put it at the end of the array but i failed and i am getting the wrong results and i need help spotting the error. here is my code

public static void newSortMethod(int[]a){
    for(int i = 0; i < a.length-1; i++){
        int maxPosition=i;
        int minPosition=i;
        for(int j = i+1; j < a.length; j++){
            if(a[j] < a[minPosition]){
                minPosition = j;
            }
            if(a[j] > a[maxPosition]){
                maxPosition = j;
            }
        }
        swap(a,maxPosition,i);
        swap(a,minPosition,a.length-i-1);
    }
    System.out.println();
}

public static void swap(int[]a, int i, int j){
    int temp = a[i];
    a[i] = a[j];
    a[j] = temp;
}

public static void main(String[] args) {
    int[] a = {2,6,3,9,5,4,8,7,0,13,-3,1};
    newSortMethod(a);
}

here is the output of the program so far -3 8 2 9 13 5 4 6 3 1 7 0


Solution

  • Your original algorithm is wrong. Firstly, the if blocks should compare to minPosition and maxPosition, not i. Secondly, if you are selecting both minimum and maximum, then your inner for loop should stop at a.length - i, not a.length (since the top i elements are also sorted). Doing both gives you this as the ascending order algorithm.

    public static void newSortMethod(int[]a){
        for(int i = 0; i < a.length; i++){
            int maxPosition=i;
            int minPosition=i;
            for(int j = i+1; j < a.length - i; j++){
                if(a[j] < a[minPosition]){
                    minPosition = j;
                }
                if(a[j] > a[maxPosition]){
                    maxPosition = j;
                }
            }
            swap(a,maxPosition,i);
            swap(a,minPosition,a.length-i-1);
        }
    }
    

    To switch to descending order, simply add one line.

    public static void newSortMethod(int[]a){
        for(int i = 0; i < a.length; i++){
            int maxPosition=i;
            int minPosition=i;
            for(int j = i+1; j < a.length - i; j++){
                if(a[j] < a[minPosition]){
                    minPosition = j;
                }
                if(a[j] > a[maxPosition]){
                    maxPosition = j;
                }
            }
            swap(a,minPosition,maxPosition); // <-- this line
            swap(a,maxPosition,i);
            swap(a,minPosition,a.length-i-1);
        }
    }