Would someone help me with Searching and Sorting Arrays?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bull Horn
    New Member
    • Jan 2011
    • 2

    Would someone help me with Searching and Sorting Arrays?

    This is basically what I have to do. I think I have the methods correct in terms of sorting, but how do I switch it from largest to smallest?

    The selectionSort method should take an array of integers as a
    parameter and will sort the numbers into order from largest to smallest.
    Place a println statement in the sort so every time a new maximum is
    found, it is printed to the screen. Once all are sorted, the method should
    print the array to the screen on the same line. Note that this method should
    change the original array and because arrays are passed by reference, the
    method does not need to return anything (the passed array will be changed
    at the source), so the return type should be void.

    The insertion method should take an integer and an array as
    parameters. This method will create a new array and that will hold
    the same values as the passed array, but allow for one extra space.
    The integer passed to the method will be inserted into the array in the
    appropriate space. This new array should be returned by the method.

    Code:
    public class SortingStuff
    {
    public SortingStuff()
    {
    }
    
    public void selectionSort()
    {
    int min, temp;
    for(int outer=0; outer<list.length-1; outer++)
    {
    min=outer;
    for(int inner=outer+1; inner<list.length; inner++)
    {
    if(list[inner]<list[min])
    {
    min=inner;
    }
    }
    temp=list[outer];
    list[outer]=list[min];
    list[min]=temp;
    }
    }
    public void insertion()
    {
    for(int outer=1; outer<list.length;outer++)
    {
    int position=outer;
    int key=list[position];
    
    while(position>0 && list[position-1] > key)
    {
    list[position]= list[position-1];
    position--;
    }
    list[position]=key;
    }
    }
    
    }
    
    import chn.util.*;
    public class SearchS
    {
    public static void main(String[] args)
    {
    ConsoleIO bam = new ConsoleIO();
    SortingStuff sortstuff = new SortingStuff();
    }
    }
Working...