17 Clean implementations of Java Sorting Algorithms
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 “Non recursive” Sort (SorterType.QUICK_NON_RECURSIVE)
- 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.
public interface Sorter {
<T> void sort(Comparator<T> comparator, List<T> list) ;
SorterType getType();
}
public abstract class AbstractSorter implements Sorter { ... }
Implementations:
public class InsertionSorter extends AbstractSorter { ... }
How to use?
- Get a DataGenerator instance:
DataSetGenerator<Integer> dataGenerator = new IntegerDataSetGenerator();
- Get a SorterProvider instance:
SorterProvider sorterProvider = new SorterProvider();
- Create a random list and sort it using QuickSort:
List<Integer> randomList = dataGenerator.createRandom(1000); Sorter quickSort = sorterProvider.getSorterForType(SorterType.QUICK); quickSort.sort(dataGenerator.getComparator(), randomList);
- Create a descending list and sort it using SelectionSort:
List<Integer> descendingList = dataGenerator.createDescending(1000); Sorter selectionSort = sorterProvider.getSorterForType(SorterType.SELECTION); selectionSort.sort(dataGenerator.getComparator(), descendingList);
- It isn’t a must to create a DataGenerator instance but at some point you will have to implement a java.util.Comparator.
public class StringComparator implements Comparator<String> {
@Override
public int compare(String s1, String s2) {
return s1.compareTo(s2);
}
}
String[] names = new String[]{"Mery", "Paul", "Bruce"};
Sorter mergeSort = sorterProvider.getSorterForType(SorterType.MERGE);
mergeSort.sort(new StringComparator(), Arrays.asList(names));
- 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.
Direct download: From here!
If you liked this post please vote it in dzone !
Bye !
If you enjoyed this post follow me on twitter or leave a feedback !
Great! I’ve been looking for something like this for a while.
Is there a public repository (like on github). Or would you mind if I upload it to such a repository, so that other people can fork it?
@leifg
I uploaded the project to http://javasorting.googlecode.com with extra documentation. I also cleaned up some old .svn files which did not correspond so please checkout this new version.
Bye !
found your site on del.icio.us today and really liked it.. i bookmarked it and will be back to check it out some more later
thanks!
lets write them until the admit it, or stop doing it! i am writing them now!
Smashing equipment as usual…
That is some inspirational stuff. Never knew that opinions could be this varied. Thanks for all the enthusiasm to offer such helpful information here.
@GroppyGoN
I’m having kind of problems with comments, je… sorry !
Thanks to all ! It’s great to hear that.
I would recommend you add a method to your sorter interface:
public void <T extends Comparable> sort(List list);
It is inconvenient to have to implement a comparator for objects that are already comparable.
internally you would construct a private Comparator that compares generic Comparables and then delegate to the other sorter method
Good idea ! Didn’t thought about that. Keep around !
Interesting and informative. But will you write about this one more?
@LobkeeptAbsob
You can download the sources !
I was just seeking this information for some time. Almost 10 hours of online finding, finally I saw it in your article. I wonder why Yahoo never show this type of informative websites in the top of the list. Most of the time the top sites are full of rubbish. Maybe it’s time to use other search engine.
@Bagus
It’s very hard to be on the first search results, je. Great to hear you found what you were looking in my blog. Keep around!
how to show the output of the sorted one in ternaryHeap? thanks
public void printList (List list) {
StringBuilder sb = new StringBuilder();
sb.append(“[");
for (int i = 0; i < list.size(); i++) {
sb.append(list.get(i).toString());
if (i != list.size() -1) {
sb.append(", ");
}
}
sb.append("]")
System.out.println(sb.toString());
}
[url=http://adultfriendfinder3.WebStarts.com/friend-finder-free.html]friend finder free[/url]
[url=http://adultfriendfinder3.WebStarts.com/search-for-people.html]search for people[/url]
[url=http://adultfriendfinder3.WebStarts.com/free-dating-website.html]free dating website[/url]
[url=http://adultfriendfinder3.WebStarts.com/adultfriendfinder-registration.html]adultfriendfinder registration[/url]
[url=http://adultfriendfinder3.WebStarts.com/dating-sites-for-free.html]dating sites for free[/url]