ShellSort

class  ShellSort 
{
    
public   static   void  main(String[] args) 
    {
        
int [] array  =   new   int []{  3 5 , 2 , 8 , 4 , 9 , - 1 , 7  };

        
for ( int  num : array)
            System.out.println(num);

        
long  timeStart  =  System.currentTimeMillis();
        
for ( int  i = 0 ; i < 1000000 ; i ++ )
            ShellSort.shellSort(array);
        
long  timeEnd  =  System.currentTimeMillis();
        System.out.println(
" time:  " + (timeEnd - timeStart));

        
for ( int  num : array)
            System.out.println(num);

    }
    
static   void  shellSort( int  array[]){
        
int  gap  =  array.length / 2 ;
        
int  temp;
        
int  i,j;
        
while (gap != 0 ){
            
for (i = gap; i < array.length; i ++ ){
                temp 
=  array[i];
                
for (j = i; j >= gap  &&  temp < array[j - gap]; j -= gap)
                    array[j] 
=  array[j - gap];
                array[j] 
=  temp;
            }
            gap
/= 2 ;
        }
    }
}

你可能感兴趣的:(职场,休闲,ShellSort)