Shell Sort Program Code in C++

Shell sort in c++ is the insertion sort but with some gap value.

Basically, Gap value is the number of iteration to reach the next array element.

In Insertion sort gap value =1. That’s why we compare an array element with its adjacent element of 1 index far way and so on.

Shell sort is same like insertion sort. In fact more efficient than insertion sort.

It is because instead of comparing values 1 index away, We start comparing values with the index difference of gap=length/2 and decreasing the gap value, so on.

This makes array more sorted till we reach insertion sorting stage ie gap=1.

When finally the gap=1, then it becomes insertion sort.


Online Compiler


Shell Sort

  • In-place algorithm(We work on the original array).
  • Difficult to compute time complexity because it will depend on gap value.
  • Worst case: O(n²), Usually it performs much better than this.
  • Unstable algorithm (same numbers can be interchanged which is useless)

Also Read:

Merge Sort in C.

Counting Sort in C++.

QuickSort in C++.


I am a IT Student from India. Technology and programming is the most enthusiastic thing for me in this world. I like learning new techniques and use them for real-world application because I feel tech is future. Let's learn and grow TOGETHER.