[an error occurred while processing this directive]
public void sort()
- this is the method that a client
calls to sort the Vector vec
held by a VectorSorter object.
In fact, it just calls the next method, supplying the appropriate
parameters.
protected void sort(int ilo, int ihi)
- this method
actually has all of the quicksort code.
It is a recursion, with the parameters specifying the range of indices that
should be sorted.
protected void swap(int i, int j)
- this method is called
by the sort(int, int)
method to swap entries with indices
i
and j
.
You will also need to add a few lines of code to the main()
method of the Test1 class.
The easiest method to write is the first, so you should start there.
sort()
Methodpublic void sort() { sort(0, vec.size() - 1); } // public void sort()As you can see, it looks a lot like a C function definition. The one statement in the function just calls the
sort(int,
int)
method, supplying parameters 0
and
vec.size() - 1
as parameters that specify the range of indices
to be sorted.
To determine the number of entries in a Vector, you send it a
size()
message.
This returns an int that can be assigned to a variable, used as a
parameter, or, in this case, used as part of a more complex expression.
swap(int, int)
Methodi
of vec
to a temporary variable, copy the entry at index j
to the
entry at index i
, and then copy the temporary variable to the
entry at index j
.
For the first statement, use the code
Object temp = vec.get(i);The variable is declared as an Object to allow any kind of object to be stored there.
To copy an value to the i-th position in a Vector you use code with the form
vectorVariable.set(i, value);That is, you send a
set()
message to the Vector, passing the
index and desired value as arguments.
You will need two statements like this to complete the swap()
method.
After it is done you can try recompiling the class to check for errors.
sort(int, int)
Methodsort()
method is the longest piece of code
in the assignment.
It's code is similar to a C quicksort function as described in
quicksort.C.
The pivot value variable described there should be declared as an Object
initialized to null
.
The code in that web page uses in-line code in two places to swap elements.
Your code should replace the in-line code with calls to your
swap()
method.
This means that you will not need the tempEntry
variable - it
is incorporated into the swap()
method.
Most of the rest of the Java code is identical to the C code except for a
different method names and differences in accessing entries of Vectors.
Remember to use the get()
method to retrieve a Vector
entry and set()
to modify a Vector entry.
Finally, the Comparator comp
of your VectorSorter is used for
comparing two objects.
For example, the following expression is true when the entry at index
uhi
of vec
is greater than the pivot:
comp.compare(vec.get(uhi), pivot) > 0That is, you pass two things to be comped as arguments for the
compare
method of the Comparator.
It returns a negative value if the first object is less than the second, 0
if the two objects are equal, and a positive value if the first object is
greater than the second.
main()
method of the Test1
class.
At the end of the method, send a sort()
message to the
VectorSorter vs
.
Then send another print()
message to the VectorStatement.
To make the output clearer, you should preceed the
print()
message with a short output statement indicating that
the output that follows is after sorting.
When you are done with the modifications recompile the class. Then you can execute it again. This time you should see the Vector contents printed twice, once before sorting and once after sorting.
[an error occurred while processing this directive]