# [Java] Selection Sort

• Java
Hi, I'm currently working on my selection sort code, and I've come across some problems.

Java:
i
import java.util.Arrays;
import java.io.*;

public class SelectionSort
{
int a[];
static int n;

public static void main(String[] args)
{
int array[] = { 5,3,9,7,1,8 };
n = 6;
System.out.println(""+Arrays.toString(Sort(array)));
}

public static int[] Sort(int[] arr)
{
int t, min;
for(int i=0;i<n-1;i++)
{
min = i;
for(int j=i+1;j<n;j++)
{
if(a[min]>a[j])
min = j;
}
if(min!=i)
{
t = a[min];
a[min] = a;
a = t;
}
System.out.println(""+Arrays.toString(arr));
}
return arr;
}

}

Can someone please tell me what the problem is?

Last edited by a moderator:

## Answers and Replies

Borg
Gold Member
What have you tried so far and what do you think is wrong?

Well, I've taken the structure of my bubble sort and put it into my selection sort. I'm just trying to change the code for the actual selection part. I'm just getting simple mistakes now..

Borg