Shell Sort program in C#

Shell Sort is an optimized version of insertion sort that allows the exchange of items that are far apart in the array. It starts with a large gap between compared elements and progressively reduces this gap until it becomes 1. This algorithm was developed by Donald Shell in 1959, hence the name.

Unlike insertion sort which compares adjacent elements, Shell Sort compares elements separated by a gap. This reduces the number of shifts needed and makes the algorithm more efficient for larger datasets.

How Shell Sort Works

Shell Sort works by dividing the array into smaller sub-arrays based on a gap value. Elements in each sub-array are then sorted using insertion sort. The gap is gradually reduced until it becomes 1, at which point the algorithm performs a final insertion sort on the nearly sorted array.

Shell Sort Gap Reduction Process Gap = 4 9 5 1 8 Gap = 2 5 1 8 9 Gap = 1 1 5 8 9 Compare gap=4 Compare gap=2 Final sort Gap reduces: 4 ? 2 ? 1 Each pass makes the array more sorted

Algorithm Steps

  • Start with a gap value (typically n/2 where n is array length).

  • Compare elements that are gap positions apart.

  • Swap them if they are in wrong order.

  • Reduce the gap (usually by half) and repeat.

  • Continue until gap becomes 1, then perform final insertion sort.

Example

using System;

namespace ShellSortDemo {
    public class Example {
        static void shellSort(int[] arr, int n) {
            int i, j, gap, temp;
            gap = n / 2;
            
            while (gap > 0) {
                for (i = gap; i < n; i++) {
                    temp = arr[i];
                    j = i;
                    
                    while ((j >= gap) && (arr[j - gap] > temp)) {
                        arr[j] = arr[j - gap];
                        j = j - gap;
                    }
                    arr[j] = temp;
                }
                gap = gap / 2;
            }
        }
        
        static void Main(string[] args) {
            int[] arr = new int[] { 56, 12, 99, 32, 1, 95, 25, 5, 100, 84 };
            int n = arr.Length;
            int i;
            
            Console.WriteLine("Shell Sort");
            Console.Write("Initial array is: ");
            for (i = 0; i < n; i++) {
                Console.Write(arr[i] + " ");
            }
            
            shellSort(arr, n);
            
            Console.Write("\nSorted Array is: ");
            for (i = 0; i < n; i++) {
                Console.Write(arr[i] + " ");
            }
        }
    }
}

The output of the above code is −

Shell Sort
Initial array is: 56 12 99 32 1 95 25 5 100 84 
Sorted Array is: 1 5 12 25 32 56 84 95 99 100 

Using Different Gap Sequences

Shell Sort performance depends on the gap sequence used. Here's an example using the original Shell sequence −

using System;

class ShellSortOriginal {
    static void ShellSortWithOriginalGap(int[] arr) {
        int n = arr.Length;
        
        // Start with gap = n/2, reduce by half each time
        for (int gap = n / 2; gap > 0; gap /= 2) {
            Console.WriteLine("Processing with gap: " + gap);
            
            for (int i = gap; i < n; i++) {
                int temp = arr[i];
                int j;
                
                for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) {
                    arr[j] = arr[j - gap];
                }
                arr[j] = temp;
            }
        }
    }
    
    static void Main() {
        int[] numbers = { 64, 34, 25, 12, 22, 11, 90 };
        
        Console.WriteLine("Original array:");
        foreach (int num in numbers) {
            Console.Write(num + " ");
        }
        Console.WriteLine();
        
        ShellSortWithOriginalGap(numbers);
        
        Console.WriteLine("\nFinal sorted array:");
        foreach (int num in numbers) {
            Console.Write(num + " ");
        }
    }
}

The output of the above code is −

Original array:
64 34 25 12 22 11 90 
Processing with gap: 3
Processing with gap: 1

Final sorted array:
11 12 22 25 34 64 90 

Time and Space Complexity

Case Time Complexity Space Complexity
Best Case O(n log n) O(1)
Average Case O(n^1.25) O(1)
Worst Case O(n^2) O(1)

Conclusion

Shell Sort is an efficient sorting algorithm that improves upon insertion sort by comparing elements separated by a gap. It performs better than insertion sort on larger datasets due to its ability to move elements closer to their final positions early in the sorting process. The algorithm's performance depends significantly on the gap sequence chosen.

Updated on: 2026-03-17T07:04:35+05:30

932 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements