Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
303 views
in Technique[技术] by (71.8m points)

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(compTab[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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...