Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

[Java] Selection Sort

Tags:
  1. Dec 16, 2014 #1
    Hi, I'm currently working on my selection sort code, and I've come across some problems.

    Code (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: Dec 16, 2014
  2. jcsd
  3. Dec 16, 2014 #2

    Borg

    User Avatar
    Science Advisor
    Gold Member

    What have you tried so far and what do you think is wrong?
     
  4. Dec 16, 2014 #3
    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..
     
  5. Dec 16, 2014 #4

    Borg

    User Avatar
    Science Advisor
    Gold Member

    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?
     
  6. Dec 16, 2014 #5

    Mark44

    Staff: Mentor

    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: [Java] Selection Sort
  1. Selection sort? (Replies: 2)

Loading...