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

  • Thread starter 1/2"
  • Start date
  • Tags
    Sort
In summary, the conversation is about accepting 10 names and printing them alphabetically. The attempt at a solution involves using a for loop to compare the names and swapping them if they are not in alphabetical order. The code provided is attempting to do this, but the output may be incorrect due to a mistake in the alphabetical sorting. The person asking for help has not provided the output, and they have been asked to put their code within a [ CODE ] block to preserve the indenting.
  • #1
1/2"
99
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
  • #2
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.
 

1. Why is my selection sort algorithm not sorting my data correctly?

This could be due to a few different reasons. One possibility is that there is an error in your implementation of the selection sort algorithm. Another possibility is that there is an issue with your data, such as duplicates or unsorted elements. It is important to carefully check your code and data to identify the source of the problem.

2. What are common mistakes to avoid when implementing selection sort?

Some common mistakes to avoid include not properly initializing the minimum or maximum values, not correctly swapping elements, and using the wrong loop conditions. It is also important to remember to always update the minimum or maximum value after each iteration to ensure all elements are sorted.

3. How can I optimize my selection sort algorithm for better performance?

There are a few ways to optimize selection sort for better performance. One approach is to use an adaptive selection sort, where the algorithm checks if an element is already in its correct position before swapping it. Another option is to use a hybrid sorting algorithm, such as merge sort or quicksort, which have better time complexity than selection sort.

4. Can selection sort be used for sorting large datasets?

While selection sort can be used for sorting large datasets, it is not the most efficient choice. It has a time complexity of O(n^2), which means it can take a long time to sort large datasets. It is better suited for smaller datasets or as a step in a larger sorting algorithm.

5. Is there a way to make selection sort work for sorting in descending order?

Yes, selection sort can be easily modified to sort in descending order. Instead of finding the minimum value, the algorithm can be adjusted to find the maximum value in each iteration. This will result in the elements being sorted in descending order.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
911
  • Programming and Computer Science
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
Back
Top