1

When I try to use my Compare method in my Bubblesort code to determine the alphabetical order of strings to sort, I get stuck in an infinite loop in the Compare method, which works otherwise.

Any ideas? Thanks!

public static int Compare(String s1 , String s2) { //compares two strings alphabetically
    int result = 0 ;
    String newS1 = "";
    String newS2 = "";

    for (int i = 0; i< s1.length(); i++){  //loop converts strings from uppercase to lowercase
        char s1Char = s1.charAt(i);
        if (65 <= s1Char && s1Char <=90){
            s1Char = (char)( (s1Char + 32) ); 
        }
        newS1 = newS1 + s1Char;  
    }

    for (int i = 0; i< s2.length(); i++){
        char s2Char = s2.charAt(i);
        if (65 <= s2Char && s2Char<=90){
            s2Char = (char)( (s2Char + 32) ); 
        }
        newS2 = newS2 + s2Char;   
    }

    for (int i = 0 ; i <newS1.length();){    //compares the two lowercase strings by ascii value
        if (newS1.charAt(i) < newS2.charAt(i)){
            result = -1 ;
            System.out.println(newS1+ " is before " +newS2+ " in the dictionary");
            break ;
        }
        if (newS1.charAt(i) > newS2.charAt(i)){
            result = 1 ;
            System.out.println(newS2+ " is before " +newS1+ " in the dictionary");
            break ;
        }
        if (newS1.charAt(i) == newS2.charAt(i)) {
            i++ ;
        }
        else {
            result = 0 ;
            System.out.println("The words are the same.");
            break ;
        }
    }
    return result ;
}

public static int bubbleSort(String[] input_array) {  //sorts an array alphabetically
    int n = input_array.length ;
    String temp = "" ;
    int comparisons = 0 ;
    boolean swap = false ;
    while (swap == true) {
        swap = false ;
        for(int i = 1 ; i < input_array.length - 1 ; ){
            if(Compare(input_array[i] , input_array[i+1]) == -1)  { //use compare method above 
                comparisons++ ;                                     
                temp = input_array[i] ;
                input_array[i] = input_array[i-1] ;                 //swap if sorting needed
                input_array[i-1] = temp ;
                swap = true ;
                System.out.println(input_array);
                i++ ;
            }
            else {
                i++ ;
            }
        }
    }
    return comparisons;
}

public static void main(String[] args) {   // used to test code
    String[] testArray = {"b" , "c" ,"e" , "d"} ;
    bubbleSort(testArray) ;
}
Jacket
  • 93
  • 6

2 Answers2

1

Here you are assigning swap to be equal to true.

 while (swap = true) {

I guess you want to compare it instead.

boolean swap = true;
while (swap == true){ //Or better while (swap){
   swap = false;
zalox
  • 26
  • 2
  • I've fixed the assignment error, but now the it seems as if the code does not reach the for loop at all. – Jacket Oct 26 '14 at 17:10
0

The last if inside Compare should I think always be true, so the else never executes.

But the main issue I see is that Swap won't work.

i and j are copies of references to the strings, and the strings themselves are immutable. At the end of Swap, i points to the string originally pointed to by j and vice-versa, but the strings themselves haven't changed, and the array has them in the same locations.

If you need a separate Swap method, pass the array and the indices instead.

Vlad
  • 18,195
  • 4
  • 41
  • 71
  • I switched the swap method into the code itself, which eliminates the need to pass? But now the code does not execute at all. – Jacket Oct 26 '14 at 17:10
  • You're initializing `swap` to `false` right before the loop. – Vlad Oct 26 '14 at 17:13