Results 1 to 3 of 3
- 03-08-2009, 11:01 PM #1
Member
- Join Date
- Jan 2009
- Posts
- 90
- Rep Power
- 0
how do i reverse this method for sorting?Again!
in the previous thread i marked Solved and i realized that my problem not fully solved yet, so i post this thread again.
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; } } } }
if i want to change the codes to do the opposite i need to change this?
how do I change the i though?Java Code:if (currentMaxIndex != i) { list[currentMaxIndex] = list[i]; list[i] = currentMax;
-
emceenugget has already given you the answer:
This has nothing to do with changing loops.whenever you're comparing for max/min, swap it for the opposite. or you can just reverse the resulting list.
- 03-09-2009, 12:51 AM #3
Member
- Join Date
- Jan 2009
- Posts
- 90
- Rep Power
- 0
Similar Threads
-
[SOLVED] how do i reverse this method for sorting?
By PureAwesomeness in forum New To JavaReplies: 3Last Post: 03-08-2009, 09:37 PM -
Reverse and Replace a String in Linear Time
By colin.cruise in forum New To JavaReplies: 5Last Post: 07-01-2008, 09:02 PM -
How to reverse two dimensional
By masaka in forum New To JavaReplies: 4Last Post: 05-19-2008, 10:02 AM -
Data Modeler plug-in with Reverse Engineering feature?
By vpin3515 in forum EclipseReplies: 1Last Post: 03-26-2008, 11:41 AM -
Reverse engineer a java code
By lenny in forum Advanced JavaReplies: 1Last Post: 07-25-2007, 11:14 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks