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
- Nth Term of a Fibonacci Series of Primes formed by concatenating pairs of Primes in a given range
- Maximize shortest path between given vertices by adding a single edge
- Minimize the Sum of all the subarrays made up of the products of same-indexed elements
- Minimum cost required to convert all Subarrays of size K to a single element
- Maximum number of unique Triplets such that each element is selected only once
- Maximize the Sum of the given array using given operations
- Maximize difference between the Sum of the two halves of the Array after removal of N elements
- Find a pair in Array with second largest product
- Detect cycle in Directed Graph using Topological Sort
- Rearrange an Array such that Sum of same-indexed subsets differ from their Sum in the original Array
- Minimize count of array elements to be removed to maximize difference between any pair up to K
- Reduce given three Numbers by decrementing in Pairs
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.