Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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!

share|improve this question
6  
Please don't do that: int this; –  Howard Sep 25 '11 at 15:18
1  
Using this as a variable name is an excellent way to confuse people reading your code, if not the parser itself. –  Brad Mace Sep 25 '11 at 15:18
    
Thank you. Like I mentioned, this is actually not my code. It is from my textbook. –  Kristian Sep 25 '11 at 15:31
    
@bemace. Don't worry, it doesn't compile. –  Ishtar Sep 25 '11 at 15:48
    
@Kristian:This is InsertionSort. Read on the algorithm to understand the logic better en.wikipedia.org/wiki/Insertion_sort –  Cratylus Sep 25 '11 at 16:23

2 Answers 2

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.

share|improve this answer
    
Thanks a lot :). I wasn't aware that the variable actually decrased in value after the execution of the inner loop. But then, of course, the final print out makes perfect sense! –  Kristian Sep 25 '11 at 15:33
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]);
 }
}

}

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.