Insertion Sort code questions
Date: 06/24/06
(Algorithms) Keywords: web
Here is some code from a uc berkeley lecture on insertion sort (available from the amazing berkeley webcasts, CS61B, "Sorting I" lecture ):
Presumably A is an existing array.
for (int i=1; i int j;
Object x = A[i];
for (j=i-1; j>= 0; j-= 1) {
if (A[j].compareTo (x) <= 0) /* (1) */
break;
A[j+1] = A[j];
}
A[j+1] = x;
}
I have two questions (I'm not in the class, just watching the webcasts):
1) In the code "A[j].compareTo(x)", I imagine that compareTo is some kind of callback which is supposed to already be implemented on this type of data. Is there a way I'm supposed to know how the callback is supposed to behave - I figure it returns 0 when the values are equal, but what about if a is greater than b? By the logic of this program, a.compareTo(b) returns something less than 0 if a is less than b. Is this behavior standard, or just a function of this program?
2) There is a comment that says "/* (1) */"
I don't understand what this means. Does anyone else?
Thanks.
Source: http://community.livejournal.com/algorithms/79867.html