Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default 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?
    Java Code:
     if (currentMaxIndex != i) {
            list[currentMaxIndex] = list[i];
            list[i] = currentMax;
    how do I change the i though?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    emceenugget has already given you the answer:
    whenever you're comparing for max/min, swap it for the opposite. or you can just reverse the resulting list.
    This has nothing to do with changing loops.

  3. #3
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default

    ok i think i got it.
    Thanks and
    sorry about that...
    alot of stuff went through my head, im not thinking straight lately.

Similar Threads

  1. [SOLVED] how do i reverse this method for sorting?
    By PureAwesomeness in forum New To Java
    Replies: 3
    Last Post: 03-08-2009, 10:37 PM
  2. Reverse and Replace a String in Linear Time
    By colin.cruise in forum New To Java
    Replies: 5
    Last Post: 07-01-2008, 10:02 PM
  3. How to reverse two dimensional
    By masaka in forum New To Java
    Replies: 4
    Last Post: 05-19-2008, 11:02 AM
  4. Replies: 1
    Last Post: 03-26-2008, 12:41 PM
  5. Reverse engineer a java code
    By lenny in forum Advanced Java
    Replies: 1
    Last Post: 07-26-2007, 12:14 AM

Posting Permissions

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