Home > Java > 17 Clean implementations of Java Sorting Algorithms

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.

Powered by Cincopa WordPress plugin

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 !

Share
Categories: Java Tags: ,
  1. May 4th, 2010 at 03:00 | #1

    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?

  2. admin
    May 4th, 2010 at 05:46 | #2

    @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 !

  3. May 14th, 2010 at 23:49 | #3

    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

  4. May 14th, 2010 at 23:56 | #4

    thanks! :)

    lets write them until the admit it, or stop doing it! i am writing them now!

    :)

  5. May 15th, 2010 at 13:24 | #5

    Smashing equipment as usual…

  6. May 15th, 2010 at 23:31 | #6

    That is some inspirational stuff. Never knew that opinions could be this varied. Thanks for all the enthusiasm to offer such helpful information here.

  7. negarnil
    May 16th, 2010 at 00:19 | #7

    @GroppyGoN
    I’m having kind of problems with comments, je… sorry !

  8. negarnil
    May 16th, 2010 at 00:19 | #8

    Thanks to all ! It’s great to hear that.

  9. Tim McCollough
    May 28th, 2010 at 15:24 | #9

    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

  10. negarnil
    May 29th, 2010 at 11:41 | #10

    Good idea ! Didn’t thought about that. Keep around !

  11. June 7th, 2010 at 21:34 | #11

    Interesting and informative. But will you write about this one more?

  12. Nicolas Garnil
    August 19th, 2010 at 22:34 | #12

    @LobkeeptAbsob
    You can download the sources !

  13. January 29th, 2011 at 19:46 | #13

    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.

  14. January 30th, 2011 at 14:38 | #14

    @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!

  15. mikasa032
    August 17th, 2011 at 13:13 | #15

    how to show the output of the sorted one in ternaryHeap? thanks

  16. August 17th, 2011 at 14:58 | #16

    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());
    }

  17. December 9th, 2011 at 15:39 | #17

    [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]

  1. May 4th, 2010 at 05:43 | #1
  2. May 16th, 2010 at 17:34 | #2
*