# Thread: [SOLVED] how do i reverse this method for sorting?

1. Member
Join Date
Jan 2009
Posts
90
Rep Power
0

## [SOLVED] how do i reverse this method for sorting?

this method sorts numbers from smallest to biggest
and i want it to be the opposite from biggest to smallest
i tried to exchange places with the first and second for loop, but that doesn't seem to work.
doesnt anybody have any suggestion?

Java Code:
public class SelectionSort {

/** The method for sorting the numbers */
public static void selectionSort(double[] list) {
for (int i = list.length - 1; i >= 1; i--) {
// Find the maximum in the list[0..i]
double currentMax = list[0];
int currentMaxIndex = 0;

for (int j = 1; j <= i; j++) {
if (currentMax < list[j]) {
currentMax = list[j];
currentMaxIndex = j;
}
}

// Swap list[i] with list[currentMaxIndex] if necessary;
if (currentMaxIndex != i) {
list[currentMaxIndex] = list[i];
list[i] = currentMax;
}
}
}
}

2. Senior Member
Join Date
Sep 2008
Posts
564
Rep Power
12
whenever you're comparing for max/min, swap it for the opposite. or you can just reverse the resulting list.

3. Member
Join Date
Jan 2009
Posts
90
Rep Power
0
Originally Posted by emceenugget
whenever you're comparing for max/min, swap it for the opposite. or you can just reverse the resulting list.
Hey! i never thought of that, reverse the result list.
Thank You so much, i had this in my head for hours!

4. Member
Join Date
Jan 2009
Posts
90
Rep Power
0
if i want to change the codes to do the opposite i need to change this?
Java Code:
if (currentMaxIndex != i) {
list[currentMaxIndex] = list[i];
list[i] = currentMax;
how do i change the i though?

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•