[Java] Selection Sort

  • #1
2
0

Main Question or Discussion Point

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

  • #2
Borg
Science Advisor
Gold Member
1,871
2,268
What have you tried so far and what do you think is wrong?
 
  • #3
2
0
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..
 
  • #4
Borg
Science Advisor
Gold Member
1,871
2,268
It's doubt that it's that easy. I put the code in my IDE and it highlights several problems such that it won't compile. What's the error that you're getting when you try to compile?
 
  • #5
33,273
4,981
Can someone please tell me what the problem is?
It's difficult for us to say what the problem is merely by looking at your code. Make it easier by giving us a clue about what you're seeing? Are you getting compile errors? (Another poster in this thread said that your code doesn't compile.) If it doesn't compile, what error message is the compiler giving you?

If the code compiles but throws and exception or otherwise doesn't behave like it should, give us that information, and in fact, any information that would help us help you.
 

Related Threads on [Java] Selection Sort

  • Last Post
Replies
2
Views
2K
  • Last Post
Replies
9
Views
6K
Replies
5
Views
3K
Replies
6
Views
2K
Replies
2
Views
1K
Replies
6
Views
4K
  • Last Post
Replies
2
Views
3K
  • Last Post
Replies
2
Views
2K
  • Last Post
2
Replies
47
Views
13K
  • Last Post
Replies
17
Views
6K
Top