简体   繁体   中英

Infinite loop when calling a method

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) ;
}

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;

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM