/    /  Data Structures-Shell sort

Shell sort

 

Shell sort is a form of insertion sort which overcomes the drawbacks of insertion sort by comparing the elements.

It arranges the elements in tabular form and sorts columns using insertion sort, only each time the columns get shorter. 

 

Algorithm:

Pseudocode:

procedure shellSort()
   A : array of items 
 
   /* calculate interval*/
   while interval < A.length /3 do:
      interval = interval * 3 + 1     
   end while
   
   while interval > 0 do:

      for outer = interval; outer < A.length; outer ++ do:

      /* select value to be inserted */
      valueToInsert = A[outer]
      inner = outer;

         /*shift element towards right*/
         while inner > interval -1 && A[inner - interval] >= valueToInsert do:
            A[inner] = A[inner - interval]
            inner = inner - interval
         end while

      /* insert the number at hole position */
      A[inner] = valueToInsert

      end for

   /* calculate interval*/
   interval = (interval -1) /3;   

   end while
   
end procedure

 

Reference

Shell sort