[JAVA] Shellsort

merit

Processing speed is fast

ShellSort.java



class ShellSort{
  
  public static void main(Srring[] args){
   
   //Array before sorting
   int a[] = {10,4,2,1,3,8,5,6,};
   
   //Repeat halving the grouping interval
   for(int step = a.length / 2 ; step > 0; sttep /= 2){
      //Insertion sort at intervals
      //Repeatedly fetching "values to insert" one by one from the array
      for(int i = step; i < a.length ; i++){
         //Put the value to be inserted in a variable and save it
         int tmp = a[i];
         //Repeat the comparison from the removed position to the front
         int j = i ;
         for (j = i ; j >= step; j -= step){
            //If the value to be inserted is small, shift that value backward by the step width.
            a[j] = a[j-step];
         } else {
           //If the value to be inserted is not small, stop the shifting process there.
           break;
         }
       }
       //Insert "value to be inserted" at the end of the shift process
       a[j] = tmp;
     }
   }
   //Display of sorted array
   for (int i:a){
      System.out.println(i);
   }  
 }
}

Reference book [Enjoyable learning algorithm and programming pictorial book](https://www.amazon.co.jp/%E6%A5%BD%E3%81%97%E3%81%8F%E5%AD%A6%E3%81] % B6-% E3% 82% A2% E3% 83% AB% E3% 82% B4% E3% 83% AA% E3% 82% BA% E3% 83% A0% E3% 81% A8% E3% 83% 97% E3% 83% AD% E3% 82% B0% E3% 83% A9% E3% 83% 9F% E3% 83% B3% E3% 82% B0% E3% 81% AE% E5% 9B% B3% E9% 91% 91-% E6% A3% AE-% E5% B7% A7% E5% B0% 9A-ebook / dp / B01N56TW48)

Recommended Posts

Shellsort
java shellsort