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

  • Thread starter Thread starter 1/2"
  • Start date Start date
  • Tags Tags
    Sort
AI Thread Summary
The discussion revolves around a coding issue with a selection sort implementation intended to sort an array of names alphabetically. The provided code attempts to compare and swap elements but may not be functioning correctly due to potential logical errors in the sorting logic or array indexing. Participants suggest showing the output to identify specific issues and recommend formatting the code properly to maintain readability. The need for clarity in the output and code structure is emphasized to facilitate troubleshooting. Overall, the focus is on debugging the selection sort algorithm for sorting names.
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.
 

Similar threads

Replies
7
Views
2K
Replies
7
Views
3K
Replies
12
Views
2K
Replies
6
Views
2K
Replies
3
Views
2K
Replies
1
Views
2K
Back
Top