ShellSort is mainly a variation of Insertion Sort. In insertion sort, we move elements only one position ahead. When an element has to be moved far ahead, many movements are involved. The idea of shellSort is to allow exchange of far items. In shellSort, we make the array h-sorted for a large value of h. We keep reducing the value of h until it becomes 1. An array is said to be h-sorted if all sublists of every h’th element is sorted.
Following is the implementation of ShellSort.
Array before sorting: 12 34 54 2 3 Array after sorting: 2 3 12 34 54
Time Complexity: Time complexity of above implementation of shellsort is O(n2). In the above implementation gap is reduce by half in every iteration. There are many other ways to reduce gap which lead to better time complexity. See this for more details.
Other Sorting Algorithms on GeeksforGeeks/GeeksQuiz:
- Selection Sort
- Bubble Sort
- Insertion Sort
- Merge Sort
- Heap Sort
- Radix Sort
- Counting Sort
- Bucket Sort
- C++ Program for ShellSort
- Java Program for ShellSort
- Minimum sum obtained by choosing N number from given N pairs
- 0/1 Knapsack using Least Count Branch and Bound
- Count of distinct numbers in an Array in a range for Online Queries using Merge Sort Tree
- Sorting algorithm visualization : Insertion Sort
- Sort a string lexicographically using triple cyclic shifts
- Minimize difference after changing all odd elements to even
- Longest subarray having sum K | Set 2
- Find the missing number in unordered Arithmetic Progression
- Count of anagrams of each string in an array present in another array
- Find duplicates in an Array with values 1 to N using counting sort
- Kth smallest or largest element in unsorted Array | Set 4
- Lexicographically largest string possible in one swap
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.