Shellsort in Java

Below is an example of the Shellsort algorithm in Java. See the Shellsort page for more information and implementations.


shellsort in Java

public static void main(String[] args)
{
    int[] array = {12, 11, 15, 10, 9, 1, 2, 3, 13, 14, 4, 5, 6, 7, 8};
    shellSort(array);
    System.out.println(java.util.Arrays.toString(array));
}

static void shellSort(int[] array)
{
    /*
     * for-loop setup:
     *      1. set the gapSize to the length of the arrray / 2
     *      2. run the loop as long as gapSize > 0
     */
    for (int gapSize = array.length / 2; gapSize > 0; gapSize /= 2)
    {
        for (int currentIndex = gapSize; currentIndex < array.length; currentIndex++)
        {
            // save the currentIndex
            int currentIndexCopy = currentIndex;
            // save the value of the currentIndex
            int item = array[currentIndex];

            while (currentIndexCopy >= gapSize && array[currentIndexCopy - gapSize] > item)
            {
                array[currentIndexCopy] = array[currentIndexCopy - gapSize];
                currentIndexCopy -= gapSize;
            }

            array[currentIndexCopy] = item;
        }
    }
}

Discussion