Herong's Tutorial Notes on Sorting
Dr. Herong Yang, Version 5.03

Sorting API in Java

Part:   1  2 

This chapter helps you to understand:

  • Why API?
  • HyObjec.java - My Sample Data Element Class
  • SortTest.java - My Testing Program

Why API?

When a sorting algorithm is implemented as a function in a particular programming language, it must interface with the application program that needs this function. So before writing an implementation of a sorting algorithm, we need to define the Application Programming Interface (API), to answer the following questions:

  • What are the requirements on the data elements to be sorted?
  • How to perform a comparison between two data elements?
  • How the collection of data elements to be sorted should be defined?
  • How the sorting algorithm should be invoked?

JDK provides a very good API for implementing sorting algorithms in Java. It can be summarized as:

  • Data elements to be sorted should be instances of a class that implements the Comparable interface.
  • Comparable interface requires one instance method called compareTo(Object d), which returns: 1. A positive integer, if this object is in higher order than d; 2. Zero, if this object is in equal order as d; 3, A negative integer, if this object is in lower order as d.
  • The collection of data elements should be stored in an array as: Object[].
  • Sorting algorithms should be implemented as static methods as: sort(Object[] a, int fromIndex, int toIndex), where "fromIndex" is inclusive, and "toIndex" is exclusive.

I will borrow JDK's sorting API to write and test my own implementations of sorting algorithms.

HyObjec.java - My Sample Data Element Class

Here is my sample data element class, HyObject.java:

/**
 * HyObject.java
 * Copyright (c) 1999 by Dr. Herong Yang
 */
import java.util.*; 
public class HyObject implements Comparable {
   private Object data;
   private int keyValue;
   private static Random randomGenerator;
   public static void setRandom(int s) {
      randomGenerator = new Random(s);
   }
   public HyObject() {
      data = null;
      keyValue = randomGenerator.nextInt();
   }
   public int compareTo(Object d) {
      if (this.keyValue>((HyObject)d).keyValue) {
         return 1;
      } else if (this.keyValue<((HyObject)d).keyValue) {
         return -1;
      } else {
         return 0;
      }      	
   }
   public String toString() {
      return String.valueOf(keyValue);
   }
}

Note that:

  • This class can adopted to represent any data elements in your application. You can have your information referenced by the "data" variable. Or directly add additional variables.
  • The "keyValue" is used to store an integer to help implementing the compareTo() method. Of course, you must replace it with the comparable property of your real data element.
  • The "randomGenerator" is used to assigne random value to "keyValue" to test the performance of the sorting algorithms.
  • I prefer to name the input object as "d", not as "o", which may cause confusion with number "0".

(Continued on next part...)

Part:   1  2 

Dr. Herong Yang, updated in 2007
Herong's Tutorial Notes on Sorting - Sorting API in Java