3

Does anyone have a good reference for a set of Java code implementations of common searching and sorting algorithms.

There are many ways to skin a cat (its easy to find Java code of various algos on the net), however is there a list of the most efficient ways in Java to implement these various algorithms?

There is for example the http://www.algorithmist.com but most implementations are in C/C++.

Cheers!

5 Answers 5

3

I'd suggest looking at the Java Standard Library source code itself.

The Collections and Arrays classes have binary search and binary sort implementations which you can study.

Here's the source for Collections.java from openjdk: http://www.docjar.com/html/api/java/util/Collections.java.html

Sign up to request clarification or add additional context in comments.

Comments

1

I found the book "Algorithms in Java (parts 1-4)" by Sedgewick useful. The code is available online, but it might be hard to understand without the book.

Comments

1

Some standard Algorithms for Maps, Sets, Lists etc. are available. You have to download the src.zip from the Oracle-Java homepage, where it is available.

For common use cases, the algorithms should be pretty efficient, but of course, for every algorithm there is a worst-case scenario for the data, to be provided. So in exceptional cases, you will be able to find or produce better ones.

Comments

0

The runtime complexity of an algorithm is indifferent to the programming language it's implemented in - so you shouldn't really care too much. But since you ask, I would consider using grepcode. For example, look what I found when i searched "java quicksort".

2 Comments

Yes I understand, the core is the same but some impls have greater overheads, use additional vars where one isnt required (say keeping counters etc).
Two points: 1, if you understand, then why do you care? The impl gains will be drowned out as soon as you start using large enough data sets. 2. I listed grepcode explicitly because you asked.
0

https://intelligentjava.wordpress.com/2014/07/05/sorting-algorithms/ has good review with implementations and benchmark for performance comparison.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.