Bifalgorithm post · Computer science one · Comparators
How does a Comparator determine the order of two elements?
How does a Comparator determine the order of two elements? (Computer science one · Comparators) — "A generic sorting algorithm such as Quick Sort specifies how…
Ideas in this flowchart
- A Comparator defines the ordering needed by a generic Quick Sort algorithm.
- Quick Sort specifies how to sort elements, but not how the elements are ordered.
- Quick Sort must determine whether two elements are in order, out of order, or equivalent.
- The elements may be numbers, strings, or user-defined objects.
- In Java, a Comparator object determines the proper ordering.
- Comparator<T> is parameterized to operate on any type T.
- It implements the method public int compare(T a, T b), whose arguments have type T.
- The sign of compare's return value expresses the ordering relationship.
- A negative value means that a comes before b.
- Zero means that a and b are equal.
- A positive value means that a comes after b.
- The magnitude of the returned value is not guaranteed; it need not be -1 or +1.