Sorting and Shuffling Core Java

Computer old-timers will sometimes reminisce about how they had to use punched cards and how they actually had to program by hand algorithms for sorting. Nowadays, of course, sorting algorithms are part of the standard library for most programming languages, and the Java programming language is no exception.

The sort method in the Collections class sorts a collection that implements the List interface.

This method assumes that the list elements implement the Comparable interface. If you want to sort the list in some other way, you can pass a Comparator object as a second parameter. Here is how you can sort a list of items:

If you want to sort a list in descending order, then use the static convenience method Collections. reverseOrder(). It returns a comparator that returns b.compareTo(a). For example,

sorts the elements in the list staff in reverse order, according to the ordering given by the compareTo method of the element type. Similarly,

reverses the ordering of the itemComparator.

You may wonder how the sort method sorts a list. Typically, when you look at a sorting algorithm in a book on algorithms, it is presented for arrays and uses random element access. However, random access in a list can be inefficient. You can actually sort lists efficiently by using a form of merge sort (see, for example, Algorithms in C++ by Robert Sedgewick [Addison-Wesley, 1998, pp. 366–369]). However, the implementation in the Java programming language does not do that. It simply dumps all elements into an array, sorts the array by using a different variant of merge sort, and then copies the sorted sequence back into the list.

The merge sort algorithm used in the collections library is a bit slower than quick sort, the traditional choice for a general-purpose sorting algorithm. However, it has one major advantage: It is stable, that is, it doesn’t switch equal elements. Why do you care about the order of equal elements? Here is a common scenario. Suppose you have an employee list that you already sorted by name. Now you sort by salary. What happens to employees with equal salary? With a stable sort, the ordering by name is preserved. In other words, the outcome is a list that is sorted first by salary, then by name.

Because collections need not implement all of their “optional” methods, all methods that receive collection parameters must describe when it is safe to pass a collection to an algorithm. For example, you clearly cannot pass an unmodifiableList list to the sort algorithm. What kind of list can you pass? According to the documentation, the list must be modifiable but need not be resizable.

The terms are defined as follows:

  • A list is modifiable if it supports the set method.
  • A list is resizable if it supports the add and remove operations.

The Collections class has an algorithm shuffle that does the opposite of sorting it randomly permutes the order of the elements in a list. For example:

If you supply a list that does not implement the RandomAccess interface, then the shuffle method copies the elements into an array, shuffles the array, and copies the shuffled elements back into the list. The program in Listing below fills an array list with 49 Integer objects containing the numbers 1 through 49. It then randomly shuffles the list and selects the first 6 values from the shuffled list. Finally, it sorts the selected values and prints them.

ShuffleTest.java

java.util.Collections 1.2

  • static <T extends Comparable<? super T>> void sort(List<T> elements)
  • static <T> void sort(List<T> elements, Comparator<? super T> c)sorts the elements in the list, using a stable sort algorithm. The algorithm is guaranteed to run in O(n log n) time, where n is the length of the list.
  • static void shuffle(List<?> elements)
  • static void shuffle(List<?> elements, Random r)
    randomly shuffles the elements in the list. This algorithm runs in O(n a(n)) time, where n is the length of the list and a(n) is the average time to access an element.
  • static <T> Comparator<T> reverseOrder()
    returns a comparator that sorts elements in the reverse order of the one given by the compareTo method of the Comparable interface.
  • static <T> Comparator<T> reverseOrder(Comparator<T> comp)
    returns a comparator that sorts elements in the reverse order of the one given by comp.

Binary Search

To find an object in an array, you normally visit all elements until you find a match. However, if the array is sorted, then you can look at the middle element and check whether it is larger than the element that you are trying to find. If so, you keep looking in the first half of the array; otherwise, you look in the second half. That cuts the problem in half. You keep going in the same way. For example, if the array has 1024 elements, you will locate the match (or confirm that there is none) after 10 steps, whereas a linear search would have taken you an average of 512 steps if the element is present, and 1024 steps to confirm that it is not.

The binarySearch of the Collections class implements this algorithm. Note that the collection must already be sorted or the algorithm will return the wrong answer. To find an element, supply the collection (which must implement the List interface more on that in the note below) and the element to be located. If the collection is not sorted by the compareTo element of the Comparable interface, then you must supply a comparator object as well.

A return value of ≥ 0 from the binarySearch method denotes the index of the matching object. That is, c.get(i) is equal to element under the comparison order. If the value is negative, then there is no matching element. However, you can use the return value to compute the location where you should insert element into the collection to keep it sorted. The insertion location is

It isn’t simply -i because then the value of 0 would be ambiguous. In other words, the operation

adds the element in the correct place.

To be worthwhile, binary search requires random access. If you have to iterate one by one through half of a linked list to find the middle element, you have lost all advantage of the binary search. Therefore, the binarySearch algorithm reverts to a linear search if you give it a linked list.

NOTE: Java SE 1.3 had no separate interface for an ordered collection with efficient random access, and the binarySearch method employed a very crude device, checking whether the list parameter extended the AbstractSequentialList class. This was fixed in Java SE 1.4. Now the binarySearch method checks whether the list parameter implements the RandomAccess interface. If it does, then the method carries out a binary search. Otherwise, it uses a linear search.

java.util.Collections 1.2

  • static <T extends Comparable<? super T>> int binarySearch(List<T> elements, T key)
  • static <T> int binarySearch(List<T> elements, T key, Comparator<? super T> c)

searches for a key in a sorted list, using a linear search if elements extends the AbstractSequentialList class, and a binary search in all other cases. The methods are guaranteed to run in O(a(n) log n) time, where n is the length of the list and a(n) is the average time to access an element. The methods return either the index of the key in the list, or a negative value i if the key is not present in the list. In that case, the key should be inserted at index –i – 1 for the list to stay sorted.

Simple Algorithms

The Collections class contains several simple but useful algorithms. Among them is the example from the beginning of this section, finding the maximum value of a collection. Others include copying elements from one list to another, filling a container with a constant value, and reversing a list. Why supply such simple algorithms in the standard library? Surely most programmers could easily implement them with simple loops. We like the algorithms because they make life easier for the programmer reading the code. When you read a loop that was implemented by someone else, you have to decipher the original programmer’s intentions. When you see a call to a method such as Collections.max, you know right away what the code does. The following API notes describe the simple algorithms in the Collections class.

java.util.Collections 1.2

  • static <T extends Comparable<? super T>> T min(Collection<T> elements)
  • static <T extends Comparable<? super T>> T max(Collection<T> elements)
  • static <T> min(Collection<T> elements, Comparator<? super T> c)
  • static <T> max(Collection<T> elements, Comparator<? super T> c)
    returns the smallest or largest element in the collection. (The parameter bounds are simplified for clarity.)
  • static <T> void copy(List<? super T> to, List<T> from)
    copies all elements from a source list to the same positions in the target list. The target list must be at least as long as the source list.
  • static <T> void fill(List<? super T> l, T value)
    sets all positions of a list to the same value.
  • static <T> boolean addAll(Collection<? super T> c, T... values) 5.0
    adds all values to the given collection and returns true if the collection changed as a result.
  • static <T> boolean replaceAll(List<T> l, T oldValue, T newValue) 1.4 replaces all elements equal to oldValue with newValue.
  • static int indexOfSubList(List<?> l, List<?> s) 1.4
  • static int lastIndexOfSubList(List<?> l, List<?> s) 1.4
    returns the index of the first or last sublist of l equalling s, or –1 if no sublist of l equals s. For example, if l is [s, t, a, r] and s is [t, a, r], then both methods return the index 1.
  • static void swap(List<?> l, int i, int j) 1.4
    swaps the elements at the given offsets.
  • static void reverse(List<?> l)
    reverses the order of the elements in a list. For example, reversing the list [t, a, r] yields the list [r, a, t]. This method runs in O(n) time, where n is the length of the list.
  • static void rotate(List<?> l, int d) 1.4
    rotates the elements in the list, moving the entry with index i to position (i + d) % l.size(). For example, rotating the list [t, a, r] by 2 yields the list [a, r, t].

This method runs in O(n) time, where n is the length of the list.

  • static int frequency(Collection<?> c, Object o) 5.0
    returns the count of elements in c that equal the object o.
  • boolean disjoint(Collection<?> c1, Collection<?> c2) 5.0
    returns true if the collections have no elements in common.

Writing Your Own Algorithms

If you write your own algorithm (or in fact, any method that has a collection as a parameter), you should work with interfaces, not concrete implementations, whenever possible. For example, suppose you want to fill a JMenu with a set of menu items. Traditionally, such a method might have been implemented like this:

However, you now constrained the caller of your method the caller must supply the choices in an ArrayList. If the choices happen to be in another container, they first need to be repackaged. It is much better to accept a more general collection. You should ask yourself this: What is the most general collection interface that can do the job? In this case, you just need to visit all elements, a capability of the basic Collection interface. Here is how you can rewrite the fillMenu method to accept collections of any kind.

Now, anyone can call this method, with an ArrayList or a LinkedList, or even with an array, wrapped with the Arrays.asList wrapper.

NOTE: If it is such a good idea to use collection interfaces as method parameters, why doesn’t the Java library follow this rule more often? For example, the JComboBox class has two constructors:

The reason is simply timing. The Swing library was created before the collections library. If you write a method that returns a collection, you may also want to return an interface instead of a class because you can then change your mind and reimplement the method later with a different collection. For example, let’s write a method getAllItems that returns all items of a menu.

Later, you can decide that you don’t want to copy the items but simply provide a view into them. You achieve this by returning an anonymous subclass of AbstractList.

Of course, this is an advanced technique. If you employ it, be careful to document exactly which “optional” operations are supported. In this case, you must advise the caller that the returned object is an unmodifiable list.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Core Java Topics