#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0

    Need to write a separate test code with first inserting exception into the first code


    Code:
    public class SelectionSort {
    
    public static void main(String[] args){
    
        Integer[] list = {34, 17, 23, 35, 45, 9, 1};
        System.out.println("Original Array: ");
        printArray(list);
    
        selectionSort(list);
        System.out.println("\nSelection sort:");
        printArray(list);
        Float[] flist = {34.4f, 17.6f, 23.0f};
        selectionSort(list);
    }
    
    //selection sort
    public static <E extends Comparable<E>> void selectionSort(E[] list)
    {
        for(int i=0; i<list.length -1; i++)
        {
            int iSmallest = i;
    
            for(int j=i+1; j<list.length; j++)
            {
                if(list[iSmallest].compareTo((list[j])) > 0  )
                {
                    iSmallest = j;
                }
            }
            E iSwap = list[iSmallest];
            list[iSmallest] = list[i];
            list[i] = iSwap;
    
        }
    }
    
    public static <E> void printArray(E[] list)
    {
    
        for(int i=0; i<list.length; i++)
        {
            System.out.print(list[i] + ", ");
        }
    }
    }
  2. #2
  3. Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    May 2004
    Location
    Superior, CO, USA
    Posts
    2,418
    Rep Power
    1713
    I'm not sure what your question is. What have you tried? Did is not work?

    We'll be glad to help but don't expect to get your homework done here.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0

    Understandable


    [QUOTE = response: ]
    It works, but need a postcondition throw exception code to test and see what could prevent the swap to asscending order.
    [/ QUOTE]

IMN logo majestic logo threadwatch logo seochat tools logo