Why Java API Is Needed?
This section describes why Java API is needed for sorting algorithms The Java API (Application Programming Interface) is needed to provide a single standard calling interface for all Java applications that want to use the sorting algorithm.
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 (Java Development Kit) 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
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
PDF Printing Version