Sorting algorithms are especially amenable to pictorial representations that make plain their dynamic characteristics. Visual representations are useful for understanding the operations of sorting algorithms.
I had to add thread interruptions to the fastest sorters (QuickSort, MergeSort, etc…) because if not, the animations were too fast. So don’t ever, never, use this Applet to take conclusions about which sorter is the fastest.
Well, in this post I will share with you 17 Java Sorting implementations I had to code in an algorithms course at college ! The package includes the following implementations (from the easiest to the more complex ones).
Bubble Sort (SorterType.BUBBLE)
Selection Sort (SorterType.SELECTION)
Insertion Sort (SorterType.INSERTION)
HSort (SorterType.H)
Shell Sort (SorterType.SHELL)
Quick Sort (SorterType.QUICK)
Quick “Cut” Sort (SorterType.QUICK_CUT)
Quick “Median of Three”Sort (SorterType.QUICK_MED_OF_THREE)
Quick “Three way Partition” Sort (SorterType.QUICK_THREE_PARTITION)
Bottom-Up Merge Sort (SorterType.MERGE_BOTTOM_UP)
Bottom-Up Linked-List Merge Sort
Top-Down Merge Sort (SorterType.MERGE)
Top-Down Linked-List Merge Sort
Binary Heap Sort (SorterType.HEAP_BINARY)
Ternary Heap Sort (SorterType.HEAP_TERNARY)
All implementations come with a brief explanation for you to understand the basic idea of how the algorithm sorts the elements !
API
All Sorting implementations extends AbstractSorter which implements Sorter. The Sorter interface is generified. This allows subsequent implementations to be capable to sort any collection of objects as long as exists a Comparator for that Type of objects.
It also isn’t a must to instance a SorterProvider. You can directly instance the Sorter implementation.
String[] surnames = new String[]{"Smith", "Garnil", "Valentino"};
Sorter shellSort = new ShellSorter();
shellSort.sort(new StringComparator(), Arrays.asList(surnames));
Testing the sorters
You can find a JUnit TestCase named SortersTest.java inside the anaydis.ngarnil.test package. This class tests correctness of the Sorter implementations.
A JUnit JAR (junit.jar) file is included in the lib folder.
public class SortersTest extends TestCase { ... }
I also built an Applet to run the sorters and study their dynamic characteristics.
Download !
Checkout the project from http://javasorting.googlecode.com. Don’t doubt to make questions or post issues, I will answer them as soon as I can.