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

  • Thread starter Thread starter 1/2"
  • Start date Start date
  • Tags Tags
    Sort
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
1 reply · 2K views
1/2"
Messages
98
Reaction score
0

Homework Statement



to accept 10 names and print alphabetically

Homework Equations





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
 
Physics news on Phys.org
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.