## C Program For Shell Sort Algorithm in Data Structure

Learn How To Sort Integer Arrays using Shell Sort Algorithm in C Programming Language. It is important that we should know about **How A For Loop Works** before getting further with the C Program Code. Shell sort technique is quite complex than other sorting algorithms in c programming.

#### What is Shell Sort Algorithm?

The Shell Sort Algorithm is also known as Diminishing Increment Sort Algorithm. It is an improved version of **Insertion Sort Algorithm**. This algorithm first compares Elements that are far apart from each other and then it compares the subsequent Closer Elements. Therefore, the Distance between the Elements to be compared reduces after each pass until the last pass where the Elements become the Adjacent Elements.

The elements in the even and odd index positions will not be compared till the last pass. Therefore, if the increment value is even, it is incremented by 1. If the value of the increment is a prime number, better efficiency is achieved.

#### Shell Sort Algorithm Analysis

Shell Sort is Not a **Stable Sort**. Since it requires only one temporary variable, it is an** In-Place Sort**. Space Complexity is O(1). The run time complexity of the shell sort technique varies from **O(n (log n) ^{2})** and

**O(n**.

^{1.25})#### C Program To Sort Arrays using Shell Sort Algorithm

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | #include<stdio.h> int main() { int elements[20], temp, i, j; int limit, increment; printf("Enter Limit for Array:\t"); scanf("%d", &limit); printf("\nEnter %d Elements in the Array\n", limit); for(i = 0; i < limit; i++) { scanf("%d", &elements[i]); } printf("\nEnter Maximum Increment (Odd Number) Value:\t"); scanf("%d", &increment); while(increment >= 1) { for(i = increment; i < limit; i++) { temp = elements[i]; for(j = i - increment; j >= 0 && temp < elements[j]; j = j - increment) { elements[j + increment] = elements[j]; } elements[j + increment] = temp; } increment = increment - 2; } printf("\nSorted List:\n"); for(i = 0; i < limit; i++) { printf("%d\t", elements[i]); } printf("\n"); return 0; } |

#### Output

If you have any compilation errors or doubts in this Code To Sort Array using Shell Sort C Program in Data Structure, let us know about in the Comment Section below.