public class ShellsortGeneric < T extends Comparable <? super T >> {
public static void main ( String [] args )
{
// example using Strings
String [] arrayOfStrings = { "Andree" , "Leana" , "Faviola" , "Loyce" , "Quincy" , "Milo" , "Jamila" , "Toccara" , "Nelda" , "Blair" , "Ernestine" , "Chara" , "Kareen" , "Monty" , "Rene" , "Cami" , "Winifred" , "Tara" , "Demetrice" , "Azucena" };
ShellsortGeneric < String > stringShellsort = new ShellsortGeneric <>();
stringShellsort . shellsort ( arrayOfStrings );
System . out . println ( java . util . Arrays . toString ( arrayOfStrings ));
// example using Doubles
Double [] arrayOfDoubles = { 0.35 , 0.02 , 0.36 , 0.82 , 0.27 , 0.49 , 0.41 , 0.17 , 0.30 , 0.89 , 0.37 , 0.66 , 0.82 , 0.17 , 0.20 , 0.96 , 0.18 , 0.25 , 0.37 , 0.52 };
ShellsortGeneric < Double > doubleShellsort = new ShellsortGeneric <>();
doubleShellsort . shellsort ( arrayOfDoubles );
System . out . println ( java . util . Arrays . toString ( arrayOfDoubles ));
}
void shellsort ( T [] array )
{
/*
* for-loop setup:
* 1. set the gapSize to the length of the array / 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
T item = array [ currentIndex ];
while ( currentIndexCopy >= gapSize && array [ currentIndexCopy - gapSize ]. compareTo ( item ) > 0 )
{
array [ currentIndexCopy ] = array [ currentIndexCopy - gapSize ];
currentIndexCopy -= gapSize ;
}
array [ currentIndexCopy ] = item ;
}
}
}
}