Selection sort of String array using compareTo

I have been given an array of Strings ( [Kate, Bea, Mary, Bea, Zoe]). First I have to make a boolean method which checks if one string is smaller than the other. Then I have to sort this array using Selection sort and my boolean method.
The output should be: [Bea, Bea, Zoe, Kate, Mary]
But mine is: [Bea, Bea, Kate, Mary, Zoe].
I don’t know why Zoe won’t sort.
Can you please help me find my mistake?
Here is my code:

import java.util.Arrays;
public class StringCmp {

    public static boolean isLess (String s1, String s2){
      boolean smaller=false;
      int compare=s1.compareTo(s2);
      if(compare<0)
          smaller=true;
  return smaller;
    }
    public static void sortSel(String[]arr){
        for (int i = 0; i < arr.length ; i++) {
            for (int j = i+1; j < arr.length ; j++) {
                if(isLess(arr[j],arr[i])){
                    String tmp=arr[j];
                    arr[j]=arr[i];
                    arr[i]=tmp;
                }
            }
        }
    }
    public static void main (String[] args) {
        String[] arr = {"Kate", "Bea", "Mary", "Bea", "Zoe"};
        System.out.println(Arrays.toString(arr));
        sortSel(arr);
        System.out.println(Arrays.toString(arr));

    }
}

Whenever I use layers of loops, if I have to trouble shoot I just find it helps tons to put System.out.println statements at every level for debugging.

For example one run might be

for (int i = 0; i < arr.length ; i++) {
            System.out.printl(arr[i]);
            for (int j = i+1; j < arr.length ; j++) {
                if(isLess(arr[j],arr[i])){
                    String tmp=arr[j];
                    arr[j]=arr[i];
                    arr[i]=tmp;
                }
            }
        }

Then if that’s clear:

for (int i = 0; i < arr.length ; i++) {
            
            for (int j = i+1; j < arr.length ; j++) {
                System.out.printl(arr[i]);
                System.out.printl(arr[j]);
                if(isLess(arr[j],arr[i])){
                    String tmp=arr[j];
                    arr[j]=arr[i];
                    arr[i]=tmp;
                }
            }
        }

etc.

Point is, this should clarify how the loop is cycling and what exactly it’s comparing, then if that’s all fine you should look at the code block in your conditional inside the loop.

1 Like

Thank you for your answer. I actually tried that but it made me even more confused. I know that I’m probably making some mistake but I simply can’t spot it.

Well let’s work it out (since it’s more productive that just putting the answer), what’s the output for when you try just printing arr[i], or once in the inner loop, arr[i] and arr[j]?

output from first loop arr[i]:
Kate
Kate
Mary
Mary
Zoe
output from inner loop arr[i] arr[j]:
Kate
Bea
Bea
Mary
Bea
Bea
Bea
Zoe
Kate
Mary
Kate
Bea
Bea
Zoe
Mary
Kate
Kate
Zoe
Mary
Zoe

I’m going to add numbers an letter to your list to make it more readable:

output from first loop arr[i]:
Kate 4
Kate 4
Mary 4
Mary 4
Zoe 3
output from inner loop arr[i] arr[j]:
Kate 4 i
Bea 3 j
Bea 3 i
Mary 4 j
Bea 3 i
Bea 3 j
Bea 3 i
Zoe 3 j
Kate 4 i
Mary 4 j
Kate 4 i
Bea 3 j
Bea 3 i
Zoe 4 j
Mary 4 i
Kate 4 j
Kate 4 i
Zoe 3 j
Mary 4 i
Zoe 3 j


Still hard to read, right? (unless you do some mental gymnastics) It might also be useful to print a statement like “swapping x for y, current list is now: [x,y,z]” and possibly “no swaps made, list is still [x,y, z]” (string formatting works wonders with these type of debug statements).

I did what you suggested. After analysing it I tried to change some propreties of loops but in most cases the output remained the same. So I guess something must be wrong with my method (and also with my way of thinking). I tried to add a condition for Strings of equal size but it changed nothing (btw the task says that method should return true IF AND ONLY IF s1 < s2 ). I still can’t figure it out.