Does your company or startup need an expert? If so, write to me to talk about possibilities of professional cooperation.
PROGRAMMING on-line course is approaching! Write to me if you are interested in free early access.

Marcin Jamro, PhD, DSc

How to sort array with Shell sort?

C#  |  .NET  |  Algorithms

The Shell sort is a variation of the insertion sort. The algorithm performs h-sorting to sort virtual subarrays consisting of elements with distance equal to h, using the insertion sort. At the beginning, h is set to a half of the array length and is divided by 2 in each iteration, until it is equal to 1.

The code is as follows:

void Sort(int[] a)
    for (int h = a.Length / 2; h > 0; h /= 2)
        for (int i = h; i < a.Length; i++)
            int j = i;
            int ai = a[i];

            while (j >= h && a[j - h] > ai)
                a[j] = a[j - h];
                j -= h;

            a[j] = ai;

This entry can contain content, including parts of code, from one of my books, namely from the Second Edition of C# Data Structures and Algorithms, published by Packt Publishing in 2024. If you want to read more about the Shell sort algorithm, as well as about other kinds of sorting algorithms, I encourage you to take a look at the mentioned book. You can buy it here.

The content, including any part of code, is presented without warranty, either express or implied. The author cannot be held liable for any damages caused or alleged to have been caused directly or indirectly by any content shown on this website.

Hello, I am Marcin

Reliable entrepreneur with 10+ years of companies operation, such as CEO at a few IT companies. I was an author of a few software & hardware products, still open to new ideas & cooperation.

Helpful expert with 10+ years of experience, together with PhD and DSc in Computer Science. I was an author of books and publications, as well as an expert in international projects.

Experienced developer with 10+ years of development and 100+ completed projects. I worked on various complex international projects, e.g. at Microsoft in USA and as CTO at a few companies. I have MCP, MCTS and MCPD certificates.

You can read more about me in my short bio. I am waiting for contact at [email protected], as well as at my Facebook and LinkedIn profiles.


I am an author of a few books and numerous publications, also in high-quality international scientific journals.

If you want to learn data structures and algorithms in the context of C#, let's take a look at my newest book. It is the second edition of C# Data Structures and Algorithms. You can buy it here.


Do you like traveling? If so, discover an amazing world with local guides, right here and right now at:


I love travels, learning new cultures and regions, meeting outstanding people, as well as taking pictures of beautiful places. Take a look at my travel diary.