You are on page 1of 9

Data Structure

Topic : Shell Sort (Adc 3rd )


Introduction:
• This method of sorting was introduced by Donald Shell in 1959.
• Shell Sort is basically a generalization of insertion sort.
• It is used to compare elements that are distant apart rather than
adjacent.
• Krnuths formula : n/2
• This formula is used to find and minimize gaps in between elements.
Working
• We will be explaining Shell Sort on an Array containing 8 elements indexing from 0 to 7.
steps
• We compare values in each sub-list and swap them (if necessary) in the original array. After this step, the
new array should look like this –

Then, we take an interval of 2 and will compare the elements with gap 2
• We compare and swap the values, if required, in the original array. After this step, the array should look like
this –

• Finally, we sort the rest of the array using an interval of value 1. Shell sort uses insertion sort to sort the array.

• Following is the step-by-step depiction –


Time Complexity:
• The time complexity of the above implementation of shell sort is O(n2). In the above
implementation, the gap is reduced by half in every iteration. There are many other
ways to reduce gaps which lead to better time complexity.
Algorithm
• Step 1: Initialize the gap size i.e. h
• Step 2: Divide the array into sub-arrays each having interval of h
• Step 3: Sort the sub-arrays with insertion sort
• Step 4: Reduce the value of h
• Step 5: Repeat the above steps until the array is sorted
conclusion
Shell sort is the highly efficient algorithm that comes to an improvement over the insertion sort.

While insertion sort works by incrementing its elements by 1, shell sort uses the parameter gap
to divide the array into sub-arrays whose elements are “gap” apart. Then we can sort the individual list using insertion sort to obtain
the complete sorted array.

Shell sort performs faster than insertion sort and takes fewer moves to sort the array when compared to insertion sort.

You might also like