Results 1 to 2 of 2
  1. #1
    zoe
    zoe is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default Help with sort algorithm

    Hello, I need to re-write the Sorts class for arrays so that it sorts in descending order, example: high to low.
    The best I can do is: if i put in 5 numbers, 1-5, and it sorts, it shows up as 1,4,5,2,3.
    This Sorts class came from the book we use, and is the version that our teacher wants us to use.
    In reference to the stickied topic, i have spent the better part of a week working on this, and the best i get is 1,4,5,2,3. I don't think anybody else in my class has finished this one either.

    Java Code:
    public class Sorts
    {
       //-----------------------------------------------------------------
       //  Sorts the specified array of integers using the selection
       //  sort algorithm.
       //-----------------------------------------------------------------
       public static void selectionSort (int[] numbers)
       {
          int min, temp;
    
          for (int index = 0; index < numbers.length-1; index++)
          {
             min = index;
             for (int scan = index+1; scan < numbers.length; scan++)
                if (numbers[scan] < numbers[min])
                   min = scan;
    
             // Swap the values
             temp = numbers[min];
             numbers[min] = numbers[index];
             numbers[index] = temp;
          }
       }
    
       //-----------------------------------------------------------------
       //  Sorts the specified array of integers using the insertion
       //  sort algorithm.
       //-----------------------------------------------------------------
       public static void insertionSort (int[] numbers)
       {
          for (int index = 1; index < numbers.length; index++)
          {
             int key = numbers[index];
             int position = index;
    
             // shift larger values to the right
             while (position > 0 && numbers[position-1] > key)
             {
                numbers[position] = numbers[position-1];
                position--;
             }
                
             numbers[position] = key;
          }
       }
    Thanks

  2. #2
    fernando is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default

    Here you go
    Java Code:
       
       public static void selectionSort (int[] numbers)
       {
          int max, temp;
    
          for (int index = 0; index < numbers.length-1; index++)
          {
             max = index;
             for (int scan = index+1; scan < numbers.length; scan++)
                if (numbers[scan] > numbers[max])
                   max = scan;
    
             // Swap the values
             temp = numbers[max];
             numbers[max] = numbers[index];
             numbers[index] = temp;
          }
        }
    
       //-----------------------------------------------------------------
       //  Sorts the specified array of integers using the insertion
       //  sort algorithm.
       //-----------------------------------------------------------------
       public static void insertionSort (int[] numbers)
       {
          for (int index = 1; index < numbers.length; index++)
          {
             int key = numbers[index];
             int position = index;
    
             // shift larger values to the left
             while (position > 0 && numbers[position-1] < key)
             {
                numbers[position] = numbers[position-1];
                position--;
             }
                
             numbers[position] = key;
          }
          
       }
    Greetings.

Similar Threads

  1. How to sort a list using Bubble sort algorithm
    By Java Tip in forum Algorithms
    Replies: 3
    Last Post: 04-29-2008, 09:04 PM
  2. Help with algorithm
    By susan in forum New To Java
    Replies: 1
    Last Post: 07-13-2007, 11:26 PM
  3. Help me with this algorithm
    By Marcus in forum Advanced Java
    Replies: 3
    Last Post: 07-02-2007, 02:30 PM
  4. Help with Algorithm
    By Daniel in forum Advanced Java
    Replies: 2
    Last Post: 07-02-2007, 06:51 AM
  5. Insertion sort algorithm
    By Albert in forum Advanced Java
    Replies: 2
    Last Post: 06-28-2007, 09:26 PM

Posting Permissions

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