by BehindJava

# Data Structures - Shell Sort Algorithm

Home » datastructures » Data Structures - Shell Sort Algorithm

In this tutorial, we are going to learn about shell sort in detail and its algorithmic implementation.

# Shell Sort

• In-place algorithm.
• Difficult to nail down the time complexity because it depends on the gap.

• Worst Case: O(n^2), but it can perform much better than that.
• Doesn’t require much shifting as insertion sort, So it usually performs better.
• Unstable algorithm.
• Most of the sorting algorithms have a run time complexity of O(N2).
• No sorting algorithm can have time complexity less than O(N).
• Donald Shell discovered the Shell sort.
• Shell Sort improves the efficiency and decreases the run time complexity to O(N^1.25).
• The algorithms for shell sort can be defined in two steps

• Step 1: divide the original list into smaller lists.
• Step 2: sort individual sub lists using any known sorting algorithm (like bubble sort, insertion sort, selection sort, etc)

# Shell Sort Algorithm

1. Initialize the value of h
2. Divide the list into smaller sub-list of equal interval h
3. Sort these sub-lists using insertion sort
4. Repeat until complete list is sorted

# Insertion Sort Programmatic Implementation in Java

``````public class ShellSort {
public static void main(String[] args) {

int[] intArray = { 20, 35, -15, 7, 55, 1, -22 };

for (int gap = intArray.length / 2; gap > 0; gap /= 2) {
for (int i = gap; i < intArray.length; i++) {
int newElement = intArray[i];
int j = i;
while (j >= gap && intArray[j - gap] > newElement) {
intArray[j] = intArray[j - gap];
j -= gap;
}
intArray[j] = newElement;
}
}

for (int i = 0; i < intArray.length; i++) {
System.out.print(intArray[i]+ " ");
}
}
}``````

Output:

``-22 -15 1 7 20 35 55 ``

# Shell Sort (Dividing the List)

• Choose a value K, which is known as increment.
• Based on the value of K, split the list into K sub lists.\ Example: if our original list is x, x, x, x….x and we choose 5 as the value for increment, K then we get the following sub lists.
* firstlist = x, x, x, x…….x
* second
list =x, x, x, x…..x
* thirdlist =x, x, x, x…….x
* forth
list =x, x, x, x…….x
* fifth_list =x, x, x, x …….x
• So the ith sub list will contain every Kth element of the original list starting from index i-1.

# Selecting K

• If we use the same value of K, we will get the same sub lists and every time we will sort the same sub lists, which will not result in the ordered final list.
• Sorting the five sub lists independently do not ensure that the full list is sorted!
• So we need to change the value of K (increase or decrease?) for every iteration.
• Donald Shell suggested

• Which give the increment series as: N/2, N/4, …….1
• Hibbard suggested the sequence of increment as
• Which gives the increment series as 1, 4, 13….. ht

# How many Iterations?

• Number of sub lists are equal to the value of K.
• So we will have to reduce the value of K after every iteration until K will become 1 (we will have only one sub list).
• Hence we know the termination condition for our algorithm is K = 1.
• Since for every iteration we are decreasing the value of the increment (K) the algorithm is also known as “Diminishing Increment Sort”.