0

I was searching for class that implemented algorithms.. i found some code online...

package sun.misc;

public class Sort {

    private static void swap(Object arr[], int i, int j) {
    Object tmp;

    tmp = arr[i];
    arr[i] = arr[j];
    arr[j] = tmp;
    }

    /**
     * quicksort the array of objects.
     *
     * @param arr[] - an array of objects
     * @param left - the start index - from where to begin sorting
     * @param right - the last index.
     * @param comp - an object that implemnts the Compare interface to resolve thecomparison.
     */
    public static void quicksort(Object arr[], int left, int right, Compare comp) {
    int i, last;

    if (left >= right) { /* do nothing if array contains fewer than two */
        return;          /* two elements */
    }
    swap(arr, left, (left+right) / 2);
    last = left;
    for (i = left+1; i <= right; i++) {
        if (comp.doCompare(arr[i], arr[left]) < 0) {
        swap(arr, ++last, i);
        }
    }
    swap(arr, left, last);
    quicksort(arr, left, last-1, comp);
    quicksort(arr, last+1, right, comp);
    }

    public static void quicksort(Object arr[], Compare comp) {
        quicksort(arr, 0, arr.length-1, comp);
    }
}

Now i am wondering if there are some specific classes for the most commonly algorithms like bubble sort / heap sort / binary tree etc in java ... Do you guys have any idea?

3
  • 1
    sorting-algorithms.com Commented Jan 3, 2012 at 20:02
  • 1
    I rolled back your edit - questions about the importance of knowing algorithms will get closed as off-topic here. Commented Jan 3, 2012 at 20:34
  • You changed the subject of your question (title and bold at the bottom) to ask a completely different question after multiple people took the time to answer. If you want to ask a different question I don't think you should be changing content. That being said; that kind of question is likely best asked on programmers programmers.stackexchange.com Commented Jan 3, 2012 at 20:35

4 Answers 4

3

There are two things you need to look into.

Collections#sort method

And the Comparator interface

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

Comments

1

Note that the goal of a standard library is to be useful, not theoretically interesting. Therefore, I strongly doubt that there are any standard implementations of bubble sort, insertion sort, or selection sort, as they are too slow to be useful in practice (except as e.g. subalgorithms in quicksort). Collections.sort() is likely either mergesort, heapsort, or randomized quicksort. TreeSet and SortedMap are likely red-black trees. However, the Java standard itself does not specify which algorithms or data structures are to be used; it only specifies certain performance characteristics. Whoever wrote your Java implementation may choose anything that fits the criteria. For instance, TreeSet must support insert, search, and delete operations in O(lg n) time, which probably makes red-black tree the only realistic choice.

1 Comment

+1 for explaining why not to expect to find some of the algorithms in a standard library.
0

java.util.Collections.sort() is a general purposes sorting method. It takes a collection of Comparable elements as an input (ArrayList, LinkedList, etc.) and stable-sorts it.

The sorting algorithm is a modified mergesort

(see: http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html#sort(java.util.List))

Comments

0

There are two built-in methods of which I'm aware.

Collections.sort() Javadocs here provides a "modified mergesort".

Arrays.sort() uses a "tuned Quicksort". (see Javadocs here for a citation)

1 Comment

And there's timsort since Java7 and quick/mergesort both (?) use SelectionSort as soon as some threshold is reached.

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.