3
  public class selectionsorter
  {
public selectionsorter(int[] x)
{
    theArray=x;
}
public void sort()
{
    for(int i=0; i<theArray.length-1;i++)
    {
        start=i;
        findMinPos();
    }
}
public void findMinPos()
{
    int minpos=start;
    for(int i=0;i<theArray.length;i++)
    {
        if(i>start)
        {
        if(theArray[i]<theArray[start])
        {
        start=i;
        }
        }
    }
    swap();
}
public void swap()
{
    temp=theArray[start];
    theArray[start]=theArray[minpos];
    theArray[minpos]=temp;
}
private int[] theArray;
private int minpos;
private int start;
private int temp;
   }

Tester File

   public class selectionsortertester
  {
public static void main(String[] args)
{
    int[] x ={3,7,5,6,9,2};
    selectionsorter y=new selectionsorter(x);

    y.sort();
    for(int i=0; i<x.length;i++)
    System.out.print(x[i]+" ");
}
  }

I want it to sort the array it from lowest to highest and it does the first number, and the output is "2 7 5 6 9 3" Please Help and Thanks Does anyone know why it is doing this and how I can fix it, Thanks

  • You should use [Java naming conventions](http://java.about.com/od/javasyntax/a/nameconventions.htm), Class names should start with Upper case – Infinite Recursion Dec 06 '13 at 05:35
  • Welcome to SO. If any of the answers help you succesfully resolves your issue, [please accept it](http://stackoverflow.com/help/accepted-answer) – Infinite Recursion Dec 06 '13 at 06:49

2 Answers2

2

You can do it this way

Arrays.sort(x);

See docs here.

Alex
  • 11,451
  • 6
  • 37
  • 52
  • After `int[] x ={3,7,5,6,9,2};` in your main code. And you don't need `selectionsorter` at all. – Alex Dec 06 '13 at 05:34
  • it still isn't working correctly, I want it to output the array from lowest to highest, but I just swaps the first number – user3014333 Dec 06 '13 at 05:37
  • Remove this code: `selectionsorter y=new selectionsorter(x); y.sort();` – Alex Dec 06 '13 at 05:39
  • You should add `import java.util.Arrays;` in the beginning of file after `package`. But I can't believe that you do it manually. – Alex Dec 06 '13 at 05:43
  • Thanks for your help, I finally got it. In the class file in the findMinPos method I declared minpos – user3014333 Dec 06 '13 at 05:56
0

The problem is that you kept all the variables as Class attributes instead of local variables in the methods, and all your methods are changing the class variables.

This is incorrect.
Remove these from the class

private int minpos;
private int start;
private int temp;

Now pass the values as method arguments:

public class SelectionSorter {
    public SelectionSorter(int[] x) {
        theArray = x;
    }

    public void sort() {
        for (int i = 0; i < theArray.length - 1; i++) {
            findMinPos(i);
        }
    }

    public void findMinPos(int start) {
        int minpos = start;
        for (int i = start + 1; i < theArray.length; i++) {
            if (theArray[i] < theArray[start]) {
                minpos = i;
            }
        }
        if (minpos != start) {
            swap(minpos,start);
        }
    }

    public void swap(int minpos, int start) {
        int temp = theArray[minpos];
        theArray[minpos] = theArray[start];
        theArray[start] = temp;
    }

    private int[] theArray;
}

Refer this SO question to learm more about Class attributes

Community
  • 1
  • 1
Infinite Recursion
  • 6,511
  • 28
  • 39
  • 51