Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

public void Selection_Sorted(){ int t; String [][]s = a2.clone(); for(int x=0; x

ID: 3572427 • Letter: P

Question

   public void Selection_Sorted(){
int t;
String [][]s = a2.clone();
for(int x=0; x<length; x++){
int index_of_min = x;
for(int y=x; y<length; y++){
if(s[index_of_min][1].charAt(0) > s[y][1].charAt(0)){
index_of_min = x;
}else if(s[index_of_min][1].charAt(0) == s[y][1].charAt(0)){
if(s[index_of_min][0].charAt(0) > s[y][0].charAt(0)){
index_of_min = y;
}
}
}
String[] temp = s[x];   
s[x] = s[index_of_min];
s[index_of_min] = temp;
t = pos[x];
pos[x] = pos[index_of_min];
pos[index_of_min] = t;
   }   
       System.out.println("Selection Sorted");
//uses proper formatting
       Print_Data(s, length, pos);
}
  

This is outputing:

Selection Sorted
1:a:Apple
0:a:apple
3:z:Zone
2:a:apple

But I need it to output:

What am I doing wrong?

Explanation / Answer


public class Sort {
  
   public void Selection_Sorted(){
int t;
String [][]s = a2.clone();
for(int x=0; x<length-1; x++){ //assumeing s[0][] to s[length-1][] is to be sorted
int index_of_min = x;
for(int y=x+1; y<length; y++){
if(s[index_of_min][1].charAt(0) > s[y][1].charAt(0)){
index_of_min = y;
}
}
}
  
if(index_of_min != x){
   String[] temp = s[x];   
s[x] = s[index_of_min];
s[index_of_min] = temp;
t = pos[x];
pos[x] = pos[index_of_min];
pos[index_of_min] = t;
}
System.out.println("Selection Sorted");
//uses proper formatting
Print_Data(s, length, pos);
}

}