-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] + ",");
        }
    }
}
2
  • Why did you add the C language tag? Commented Nov 12, 2017 at 19:10
  • just change list[minIndex] < list[smallestIndex] to list[minIndex] > list[smallestIndex] i mean this < to > Commented Nov 12, 2017 at 19:33

4 Answers 4

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;
}
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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].

Comments

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] + ",");
        }
    }
}

4 Comments

Thanks , this worked perfectly!
@oblamios Please select this as answer if it worked for you.
How do I do that?
You will find it in the options @oblamios. You can also upvote by clicking on the up arrow on the sides.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.