I’m currently playing with implementing various sorting algorithms in Java, mostly for fun, but I’m struggling with how to do it ‘right’. That is, I want the user to be able to call the sorting algorithm of choice on anything that is comparable – ints, longs, Strings, booleans (actually, are these comparable in Java?), their own classes; whatever. The question is how to do this.
I was thinking of using a class to represent the sorting algorithm, and therefore store the things to be sorted inside using a generic list or whatever (List<E>). This would also allow me to use multiple constructors and thus allow the user to pass in the data in various forms – Lists, Arrays, whatever. Is this the correct way to do it? My current problem is that I don’t wish for the user to have to create a class when they want to sort something, I’d rather it was able to be called much like System.out.println or the like.
// Example:
int[] myInts = {5,4,3,2,1};
// This is what I do *not* want.
InsertionSort mySort = new InsertionSort();
int[] sortedInts = mySort.sort(myInts);
// This is more like what I want.
int[] sortedInts = Sorting.insertionSort(myInts);
I apologise with what may seem like a basic question, but I am just learning my way with programming languages. Which is a bit ridicolous for a 2nd year Computing student working at a software company for his summer job, but you’d be surprised at how little programming knowledge is required for most of my work… it’s usually more design knowledge.
EDIT:
For clarity, my three main question are:
- Is it better to have the user create a class to do the sorting, or to have a static method in a class the user imports?
- Is it possible to deal with both primitive data types and generic objects easily? Since I want to be able to handle any generic object that implements comparable (or likewise), this then causes problems with primitives (as they don’t implement anything 😉 ).
- What is the best way to handle generic input – what should I check for before I try to sort them (implementing Comparable, for example)?
You could take as example the way
Collectionsprovides the binarySearch operation … And indeed, theis more java-way, even if I would personnally prefer
<DataType>ensure output data is of the same type than inputIterable<DataType>data input data is an iterable, to ensure maximum compatibility. Obviously, using a List would be by far simple, as it allows internal item reordering. Howeve”r, using an iterable ensure the implementor of this method will have to re-create the list in order to modify it, guaranteeing that the input list is left unchanged, and that the output list is another one.Since I just saw you edit your question, let me reply to it point by point (and consider chosing an answer after that, as it is easier to add new questions than to edit existing ones endlessly – unless you make your question a community wiki, like I do of this reply)
To my mind, using a static method in this case is preferable, as you here have to manipulate objects you didn’t create, in a quite “basic” fashion.
Have you heard about autoboxing ? It’s a feature of Java 5 which makes primary types “equivalents” of objects. That’s to say int are automatically converted into Integer, which, as you know, implement Comparable.
Notice that, due to my method declaration (the ), checking that input data implements Comparable is not done by you, but by the Jav compiler, allowing your IDE to show you mistakes.