**Sorting Algorithm Tutorials - Herong's Tutorial Examples** - v6.10, by Dr. Herong Yang

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 algorithms.

Table of Contents

Introduction of Sorting Algorithms

►Java API for Sorting Algorithms

HyObject.java - Data Element Class

SortTest.java - Testing Program

java.util.Arrays.sort() - Performance

Performance Summary of Java Implementations

Insertion Sort Algorithm and Java Implementation

Selection Sort Algorithm and Java Implementation

Bubble Sort Algorithm and Java Implementation

Quicksort Algorithm and Java Implementation

Merge Sort Algorithm and Java Implementation

Heap Sort Algorithm and Java Implementation

Shell Sort Algorithm and Java Implementation

Sorting Algorithms Implementations in PHP

Sorting Algorithms Implementations in Perl

Sorting Algorithms Implementations in Python