1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Don't know why selection sort isn't working?

  1. Jan 11, 2012 #1
    1. The problem statement, all variables and given/known data

    to accept 10 names and print alphabetically

    2. Relevant equations



    3. The attempt at a solution

    public class n1_s
    {void main(){
    int min,c,q;String t;
    String n[]={"b","k","a","w","e"};

    for(int i=0;i<4;i++)
    {min=i;

    for(int j=i+1;j<5;j++)
    {

    System.out.println(" j starting with ="+j+" n[j]="+n[j]+" n="+n+" n.compareTo(n[j]) = "+n.compareTo(n[j]));
    // int a=;
    if(n.compareTo(n[j])>0)
    { min=j;

    }

    }
    System.out.println(" exchanged s");
    System.out.println(" n["+min+"]/min ="+n[min]+" n["+i+"]="+n);
    t=n;
    n=n[min];
    n[min]=t;
    System.out.println(" exchanged");
    System.out.println("The new array after exchange ");
    for(int f=0;f<5;f++)
    {

    System.out.print(n[f]+" ");



    }
    System.out.println();
    }
    System.out.println(" ");
    System.out.println(" Result: ");
    for(int y=0;y<5;y++)
    {

    System.out.println(n[y]);



    }

    }}
    Please point out my mistake .
    Thank you
     
  2. jcsd
  3. Jan 11, 2012 #2

    DaveC426913

    User Avatar
    Gold Member

    You might want to show us your output. Otherwise how do we actually know it's incorrect? (Maybe the flaw is in your alphabetical prowess. :biggrin:)

    Also, put your code within a [ CODE ] block, to preserve the indenting.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Don't know why selection sort isn't working?
Loading...