ShellSort

class ShellSort 
{
    
public static void main(String[] args) 
    {
        
int[] array = new int[]{ 35,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;
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章