-1

This array Sorts the number in Ascending and prints the output. I wanna change it to descending, but i can't figure it out.. i tried changing the + to - but it didn't seem to work.. Can someone tip me on how to do it?

public class ArraysSorting {
    public static void main(String[] args) {
        int [] scores = { 5,8,2,9,3};
        sortArrayDesc (scores, scores.length);
    }

    public static void sortArrayDesc( int [] list, int listLength) {
        int index;
        int smallestIndex;
        int minIndex;

        int temp;
        for (index = 0; index < listLength - 1; index++) {
            smallestIndex = index;
            for (minIndex = index + 1;
                 minIndex < listLength; minIndex++)
                if (list[minIndex] < list[smallestIndex])
                    smallestIndex = minIndex;

            temp = list[smallestIndex];
            list[smallestIndex] = list[index];
            list[index] = temp;
        }

        //display data after sorting
        System.out.print("sorted array");
        for (index=0; index<listLength; index++) {
            System.out.print(list[index] + ",");
        }
    }
}
Juan Carlos Mendoza
  • 5,736
  • 7
  • 25
  • 50
oblamios
  • 1
  • 3

4 Answers4

0

I suggest you look up articles on "bubble sort", which is fairly close to what you are doing here. Then, follow up with learning how other sort algorithms work. You generally do not want to use an n-squared sort algorithm.

To do the sort in the reverse order, you would want to use the smallestIndex variable to store the largest index instead. Change the name there, and use the > operator on the line if (list[minIndex] < list[smallestIndex]) to find the biggest number. You will then be storing the largest number in the first list position and the smallest in the last list position.

The updated loops would look like:

for (index = 0; index < listLength - 1; index++)
{
    biggestIndex = index;
    for (minIndex = index + 1; minIndex < listLength; minIndex++)
        if (list[minIndex] > list[biggestIndex])
            biggestIndex = minIndex;
    temp = list[biggestIndex];
    list[biggestIndex] = list[index];
    list[index] = temp;
}
wmorrell
  • 4,988
  • 4
  • 27
  • 37
0

A simple way to modify this code to sort array in descending order is to change the comparison operator from smaller than (<) to larger than (>).

Your code:

if (list[minIndex] < list[smallestIndex])

Change to:

if (list[minIndex] > list[smallestIndex])

You should also change the variable naming otherwise it does not make sense.

Maggie
  • 111
  • 1
  • 2
  • 10
0

I just made a few modifications. Changed the variable name and the important part

if (list[maxIndex] > list[largestIndex]) // changed less than sign to greater than

public class ArraysSorting {
    public static void main(String[] args) {
        int [] scores = { 5,8,2,9,3};
        sortArrayDesc (scores, scores.length);
    }

    public static void sortArrayDesc( int [] list, int listLength) {
        int index;
        int largestIndex;
        int maxIndex;
        int temp;
        for (index = 0; index < listLength - 1; index++) {
            largestIndex = index;
            for (maxIndex = index + 1;
                 maxIndex < listLength; maxIndex++)
                if (list[maxIndex] > list[largestIndex])
                    largestIndex = maxIndex;

            temp = list[largestIndex];
            list[largestIndex] = list[index];
            list[index] = temp;
        }

        //display data after sorting
        System.out.print("sorted array");
        for (index=0; index<listLength; index++) {
            System.out.print(list[index] + ",");
        }
    }
}
0

It sounds like you want a "for loop" to run in reverse:

for (i = listLength; i>0; i--) {

    System.out.print(list[i] + ",");
}

You need to start with

list[listLength] 

and increment down until you get to the beginning of the list/array which is

list[0].