Sort Algorithms Library

Download

.jar file
source code

Java Code

Note: Include the library in your classpath to use the sort algorithms.

Sort.java
package sorts;

import util.OArrays;

/**
 * Sort.java
 * Created by Stijn Strickx on May 21, 2008
 * Copyright 2008 Stijn Strickx, All rights reserved
 */

/**
 * These algorithms can be used to sort an array of
 * any primitive datatype / Comparable Object.
 * The datatype conversions for the arrays won't influence 
 * the time complexity of the sorting algorithms.
 * Without these conversions,
 * the algorithms would have to be re-written for every primitive datatype.
 */

public class Sort {
    
    public static <T extends Comparable<? super T>> void bubbleSort(T[] a){
        (new BubbleSort()).sort(a);
    }
    public static int[] bubbleSort(int[] a){
        Integer[] b = OArrays.toObjectArray(a);
        bubbleSort(b);
        return OArrays.toPrimitiveArray(b);
    }
    public static long[] bubbleSort(long[] a){
        Long[] b = OArrays.toObjectArray(a);
        bubbleSort(b);
        return OArrays.toPrimitiveArray(b);
    }
    public static double[] bubbleSort(double[] a){
        Double[] b = OArrays.toObjectArray(a);
        bubbleSort(b);
        return OArrays.toPrimitiveArray(b);
    }
    public static float[] bubbleSort(float[] a){
        Float[] b = OArrays.toObjectArray(a);
        bubbleSort(b);
        return OArrays.toPrimitiveArray(b);
    }
    public static char[] bubbleSort(char[] a){
        Character[] b = OArrays.toObjectArray(a);
        bubbleSort(b);
        return OArrays.toPrimitiveArray(b);
    }
    public static boolean[] bubbleSort(boolean[] a){
        Boolean[] b = OArrays.toObjectArray(a);
        bubbleSort(b);
        return OArrays.toPrimitiveArray(b);
    }
    public static byte[] bubbleSort(byte[] a){
        Byte[] b = OArrays.toObjectArray(a);
        bubbleSort(b);
        return OArrays.toPrimitiveArray(b);
    }
    public static <T extends Comparable<? super T>> void selectionSort(T[] a){
        (new SelectionSort()).sort(a);
    }
    public static int[] selectionSort(int[] a){
        Integer[] b = OArrays.toObjectArray(a);
        selectionSort(b);
        return OArrays.toPrimitiveArray(b);
    }
    public static long[] selectionSort(long[] a){
        Long[] b = OArrays.toObjectArray(a);
        selectionSort(b);
        return OArrays.toPrimitiveArray(b);
    }
    public static double[] selectionSort(double[] a){
        Double[] b = OArrays.toObjectArray(a);
        selectionSort(b);
        return OArrays.toPrimitiveArray(b);
    }
    public static float[] selectionSort(float[] a){
        Float[] b = OArrays.toObjectArray(a);
        selectionSort(b);
        return OArrays.toPrimitiveArray(b);
    }
    public static char[] selectionSort(char[] a){
        Character[] b = OArrays.toObjectArray(a);
        selectionSort(b);
        return OArrays.toPrimitiveArray(b);
    }
    public static boolean[] selectionSort(boolean[] a){
        Boolean[] b = OArrays.toObjectArray(a);
        selectionSort(b);
        return OArrays.toPrimitiveArray(b);
    }
    public static byte[] selectionSort(byte[] a){
        Byte[] b = OArrays.toObjectArray(a);
        selectionSort(b);
        return OArrays.toPrimitiveArray(b);
    }
    public static <T extends Comparable<? super T>> void insertionSort(T[] a){
        (new InsertionSort()).sort(a);
    }
    public static int[] insertionSort(int[] a){
        Integer[] b = OArrays.toObjectArray(a);
        insertionSort(b);
        return OArrays.toPrimitiveArray(b);
    }
    public static long[] insertionSort(long[] a){
        Long[] b = OArrays.toObjectArray(a);
        insertionSort(b);
        return OArrays.toPrimitiveArray(b);
    }
    public static double[] insertionSort(double[] a){
        Double[] b = OArrays.toObjectArray(a);
        insertionSort(b);
        return OArrays.toPrimitiveArray(b);
    }
    public static float[] insertionSort(float[] a){
        Float[] b = OArrays.toObjectArray(a);
        insertionSort(b);
        return OArrays.toPrimitiveArray(b);
    }
    public static char[] insertionSort(char[] a){
        Character[] b = OArrays.toObjectArray(a);
        insertionSort(b);
        return OArrays.toPrimitiveArray(b);
    }
    public static boolean[] insertionSort(boolean[] a){
        Boolean[] b = OArrays.toObjectArray(a);
        insertionSort(b);
        return OArrays.toPrimitiveArray(b);
    }
    public static byte[] insertionSort(byte[] a){
        Byte[] b = OArrays.toObjectArray(a);
        insertionSort(b);
        return OArrays.toPrimitiveArray(b);
    }
    public static <T extends Comparable<? super T>> void shellSort(T[] a){
        (new ShellSort()).sort(a);
    }
    public static int[] shellSort(int[] a){
        Integer[] b = OArrays.toObjectArray(a);
        shellSort(b);
        return OArrays.toPrimitiveArray(b);
    }
    public static long[] shellSort(long[] a){
        Long[] b = OArrays.toObjectArray(a);
        shellSort(b);
        return OArrays.toPrimitiveArray(b);
    }
    public static double[] shellSort(double[] a){
        Double[] b = OArrays.toObjectArray(a);
        shellSort(b);
        return OArrays.toPrimitiveArray(b);
    }
    public static float[] shellSort(float[] a){
        Float[] b = OArrays.toObjectArray(a);
        shellSort(b);
        return OArrays.toPrimitiveArray(b);
    }
    public static char[] shellSort(char[] a){
        Character[] b = OArrays.toObjectArray(a);
        shellSort(b);
        return OArrays.toPrimitiveArray(b);
    }
    public static boolean[] shellSort(boolean[] a){
        Boolean[] b = OArrays.toObjectArray(a);
        shellSort(b);
        return OArrays.toPrimitiveArray(b);
    }
    public static byte[] shellSort(byte[] a){
        Byte[] b = OArrays.toObjectArray(a);
        shellSort(b);
        return OArrays.toPrimitiveArray(b);
    }
    public static <T extends Comparable<? super T>> void mergeSort(T[] a){
        (new MergeSort()).sort(a);
    }
    public static int[] mergeSort(int[] a){
        Integer[] b = OArrays.toObjectArray(a);
        mergeSort(b);
        return OArrays.toPrimitiveArray(b);
    }
    public static long[] mergeSort(long[] a){
        Long[] b = OArrays.toObjectArray(a);
        mergeSort(b);
        return OArrays.toPrimitiveArray(b);
    }
    public static double[] mergeSort(double[] a){
        Double[] b = OArrays.toObjectArray(a);
        mergeSort(b);
        return OArrays.toPrimitiveArray(b);
    }
    public static float[] mergeSort(float[] a){
        Float[] b = OArrays.toObjectArray(a);
        mergeSort(b);
        return OArrays.toPrimitiveArray(b);
    }
    public static char[] mergeSort(char[] a){
        Character[] b = OArrays.toObjectArray(a);
        mergeSort(b);
        return OArrays.toPrimitiveArray(b);
    }
    public static boolean[] mergeSort(boolean[] a){
        Boolean[] b = OArrays.toObjectArray(a);
        mergeSort(b);
        return OArrays.toPrimitiveArray(b);
    }
    public static byte[] mergeSort(byte[] a){
        Byte[] b = OArrays.toObjectArray(a);
        mergeSort(b);
        return OArrays.toPrimitiveArray(b);
    }
    public static <T extends Comparable<? super T>> void quickSort(T[] a){
        (new QuickSort()).sort(a);
    }
    public static int[] quickSort(int[] a){
        Integer[] b = OArrays.toObjectArray(a);
        quickSort(b);
        return OArrays.toPrimitiveArray(b);
    }
    public static long[] quickSort(long[] a){
        Long[] b = OArrays.toObjectArray(a);
        quickSort(b);
        return OArrays.toPrimitiveArray(b);
    }
    public static double[] quickSort(double[] a){
        Double[] b = OArrays.toObjectArray(a);
        quickSort(b);
        return OArrays.toPrimitiveArray(b);
    }
    public static float[] quickSort(float[] a){
        Float[] b = OArrays.toObjectArray(a);
        quickSort(b);
        return OArrays.toPrimitiveArray(b);
    }
    public static char[] quickSort(char[] a){
        Character[] b = OArrays.toObjectArray(a);
        quickSort(b);
        return OArrays.toPrimitiveArray(b);
    }
    public static boolean[] quickSort(boolean[] a){
        Boolean[] b = OArrays.toObjectArray(a);
        quickSort(b);
        return OArrays.toPrimitiveArray(b);
    }
    public static byte[] quickSort(byte[] a){
        Byte[] b = OArrays.toObjectArray(a);
        quickSort(b);
        return OArrays.toPrimitiveArray(b);
    }
}
Sorter.java
package sorts;

/**
 * Sorter.java
 * Created by Stijn Strickx on May 21, 2008
 * Copyright 2008 Stijn Strickx, All rights reserved
 */

/**
 * Information about each algorithm's time/memory complexity and stability
 * is provided in their respective classes.
 * 
 * n is the number of records to be sorted.
 * 
 * A sorting algorithm is stable if 
 * whenever there are two records R and S with the same key 
 * and with R appearing before S in the original list, 
 * R will appear before S in the sorted list.
 */

public abstract class Sorter {
    
    /**
     * Swap the contents of a[i] and a[j]
     */
    protected void swap(Object[] a, int i, int j){
        Object tmp = a[i];
        a[i] = a[j];
        a[j] = tmp;
    }    
    public abstract <T extends Comparable<? super T>> void sort(T[] a);
}
BubbleSort.java
Go here or download the source code.
SelectionSort.java
Go here or download the source code.
InsertionSort.java
Go here or download the source code.
ShellSort.java
Go here or download the source code.
MergeSort.java
Go here or download the source code.
QuickSort.java
Go here or download the source code.
OArrays.java
/**
 * OArrays.java
 * Created by Stijn Strickx on May 21, 2008
 * Copyright 2008 Stijn Strickx, All rights reserved
 */

package util;

public class OArrays {
    
    public static Integer[] toObjectArray(int[] a){
        Integer[] b = new Integer[a.length];
        for(int i=0; i < a.length;i++)
            b[i] = a[i];
        return b;
    }
    public static Long[] toObjectArray(long[] a){
        Long[] b = new Long[a.length];
        for(int i=0; i < a.length;i++)
            b[i] = a[i];
        return b;
    }
    public static Double[] toObjectArray(double[] a){
        Double[] b = new Double[a.length];
        for(int i=0; i < a.length;i++)
            b[i] = a[i];
        return b;
    }
    public static Float[] toObjectArray(float[] a){
        Float[] b = new Float[a.length];
        for(int i=0; i < a.length;i++)
            b[i] = a[i];
        return b;
    }
    public static Character[] toObjectArray(char[] a){
        Character[] b = new Character[a.length];
        for(int i=0; i < a.length;i++)
            b[i] = a[i];
        return b;
    }
    public static Boolean[] toObjectArray(boolean[] a){
        Boolean[] b = new Boolean[a.length];
        for(int i=0; i < a.length;i++)
            b[i] = a[i];
        return b;
    }
    public static Byte[] toObjectArray(byte[] a){
        Byte[] b = new Byte[a.length];
        for(int i=0; i < a.length;i++)
            b[i] = a[i];
        return b;
    }
    public static int[] toPrimitiveArray(Integer[] a){
        int[] b = new int[a.length];
        for(int i=0; i < a.length;i++)
            b[i] = a[i];
        return b;
    }
    public static long[] toPrimitiveArray(Long[] a){
        long[] b = new long[a.length];
        for(int i=0; i < a.length;i++)
            b[i] = a[i];
        return b;
    }
    public static double[] toPrimitiveArray(Double[] a){
        double[] b = new double[a.length];
        for(int i=0; i < a.length;i++)
            b[i] = a[i];
        return b;
    }
    public static float[] toPrimitiveArray(Float[] a){
        float[] b = new float[a.length];
        for(int i=0; i < a.length;i++)
            b[i] = a[i];
        return b;
    }
    public static char[] toPrimitiveArray(Character[] a){
        char[] b = new char[a.length];
        for(int i=0; i < a.length;i++)
            b[i] = a[i];
        return b;
    }
    public static boolean[] toPrimitiveArray(Boolean[] a){
        boolean[] b = new boolean[a.length];
        for(int i=0; i < a.length;i++)
            b[i] = a[i];
        return b;
    }
    public static byte[] toPrimitiveArray(Byte[] a){
        byte[] b = new byte[a.length];
        for(int i=0; i < a.length;i++)
            b[i] = a[i];
        return b;
    }
}

Home | Code | Learn
© 2007-2008 ProgLogic, all rights reserved. | ProgLogic.com is created by Stijn Strickx. | e-mail