开发者

Sorting string array in Java

There is an example in my textbook for how to sort string arrays, but I am having a hard time understanding the logic of the code. We have the following array:

String[] words = {"so", "in", "very", "every", "do"};

The method itself is as follows:

public static void sortArray(Comparable[] compTab) {
    for (int next=1; next < compTab.length; next++) {
        Comparable value = compTab[next];
        int this;
        for (this = next; this > 0 && value.compareTo(compTab[this-1]) < 0; this--) {
            compTab[this] = compTab[this-1];
        }
        compTab[this] = value;
        writeArray(next + " run through: ", compTab);
    }
}

This last writeArray call results in the following text being printed for first run through: "1. run through: in so very every do"

OK. Like I said, I have some problems with the logic in this code. If we go through the loop for the first time, this is what I see happening:

  1. We have: Comparable value = compTab[1]. This means that value = "in".

  2. We start the inner loop with this = next (which == 1). Thus, Java will only go through the inner loop once. It turns out that for this first run value.compareTo(comp开发者_如何学编程Tab[this-1]) is indeed less than 0. Thus we have: compTab[1] = compTab[0]. This means that the word that used to be in position [1] is now replaced with the word that used to be in position [0]. Thus, we now have the word "so" in position [1] of the array.

  3. The next step in the method is: compTab[this] = value. This is where I get confused. This tells me that since this = 1, we here get compTab[1] = value. However, earlier in the method we defined value = "in". This tells me that position [1] in the array yet again assumes the word "in".

  4. The way I see this, the final print out should then be:

"1. run through: so in very every do".

In other words, the way I follow the logic of the code, the final print out of the array is just the same as it was before the method was implemented! Clearly there is some part of my logic here which is not correct. For instance - I don't see how the word that used to be in position [1] is now in position [0]. If anyone can help explain this to me, I would be extremely grateful!


The issue is within the following statement:

The next step in the method is: compTab[this] = value. This is where I get confused. This tells me that since this = 1, we here get compTab[1] = value. However, earlier in the method we defined value = "in". This tells me that position [1] in the array yet again assumes the word "in".

Since you ran through the loop once (see your statement 2), also the this-- was executed once and therefore this==0.


public class A {

static  String Array[]={" Hello " , " This " , "is ", "Sorting ", "Example"};
String  temp; 


public static void main(String[] args)

{    

 for(int j=0; j<Array.length;j++)
 {
     for (int i=j+1 ; i<Array.length; i++)
     {
         if(Array[i].trim().compareToIgnoreCase(Array[j].trim())<0)
         {
             String temp= Array[j];
             Array[j]= Array[i]; 
             Array[i]=temp;


         }
     }

     System.out.print(Array[j]);
 }
}

}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜