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
- Check if the given array contains all the divisors of some integer
- Sort an alphanumeric string such that the positions of alphabets and numbers remain unchanged
- Find number from its divisors
- Find the largest interval that contains exactly one of the given N integers.
- Maximize the size of array by deleting exactly k sub-arrays to make array prime
- Sort an array according to count of set bits | Set 2
- Count of integers of the form (2^x * 3^y) in the range [L, R]
- Program to print an array in Pendulum Arrangement with constant space
- Odd Even Transposition Sort / Brick Sort using pthreads
- Smallest multiple of N formed using the given set of digits
- Kth largest node among all directly connected nodes to the given node in an undirected graph
- Count number of subsets whose median is also present in the same subset
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above