Kyle-Falconer
10/30/2013 - 6:46 AM

Implementation of the Shell Sort algorithm, as given in CSC 232 on 14/03/2012 by Dr. Richard Martin.

Implementation of the Shell Sort algorithm, as given in CSC 232 on 14/03/2012 by Dr. Richard Martin.

class ShellSorter {

    private long[] data;
    private int len;

    public ShellSorter(int max) {
        data = new long[max];
        len = 0;
    }

    public void insert(long value) {
        data[len] = value;
        len = len + 1;
    }

    public void display() {
        System.out.print("data");
        for (int i = 0; i < len; i++) {
            System.out.print(data[i] + " \n");
        }
    }

    public void ShellSorter() {
        int inner, outer;
        long temp;
        int h = 1;
        while (h <= len / 3) {
            h = h * 3 + 1;
        }

        while (h > 0) {

            for (outer = h; outer < len; outer++) {
                temp = data[outer];
                inner = outer;

                while (inner > h - 1 && data[inner - h] > temp) {
                    data[inner] = data[inner - h];
                    inner = inner - h;
                    data[inner] = temp;
                    h = (h - 1) / 3;
                }	// while
            }	// for

        }
    }
    public static void main(String[] args) {
        int maxSize = 10;
        ShellSorter arr = new ShellSorter(maxSize);
        for (int j = 0; j < maxsize; j++) {
            long n = (int) Math.random * 99;
            arr.insert(n);
        }
        arr.ShellSorter();
        arr.display();
    }
}