HyObject.java - Data Element Class

This section describes a sample data element class, HyObject.java, which can be used to represent the data objects that you want to put them in a collection and sort them.

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

/**
 * HyObject.java
 * Copyright (c) 2011 by Dr. Herong Yang, herongyang.com 
 */
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:

Last update: 2011.

Table of Contents

 About This Book

 Introduction of Sorting Algorithms

Java API for Sorting Algorithms

 Why Java API Is Needed?

HyObject.java - Data Element Class

 SortTest.java - Testing Program

 Insertion Sort Algorithm and Implementation

 Selection Sort Algorithm and Implementation

 Bubble Sort Algorithm and Implementation

 Quicksort Algorithm and Implementation

 Merge Sort Algorithm and Implementation

 Heap Sort Algorithm and Implementation

 Shell Sort Algorithm and Implementation

 Performance Summary of Java Implementations

 References

 PDF Printing Version