Intermediate Topic 19 of 51 Algorithms
Shell Sort
Insertion sort over shrinking gaps.
How it works
Shell sort generalizes insertion sort by first comparing and moving elements that are far apart, using a decreasing gap sequence, and ending with a gap of 1 that is a normal insertion sort on nearly sorted data. Large early gaps move elements quickly toward their position. The running time depends on the gap sequence, from O(n squared) for simple halving to about O(n to the 4/3) or better for Sedgewick sequences.
Where it is used
Embedded systems and small libraries that need an in-place sort with little code, such as the uClibc qsort, and mid-sized arrays where recursion should be avoided use shell sort.
Common pitfall
Performance is hard to analyze and varies with the gap sequence, and the sort is not stable. A poor sequence such as powers of two can behave quadratically. It is rarely the fastest choice for large data compared with quicksort or merge sort.
Where it fits in the course
Shell Sort is topic 19 of 51 in Algorithms, rated Intermediate. It follows Heap Sort. The next topic is Bucket Sort.
Practice and explore
- Open the interactive lesson, with a mini quiz.
- Take the Algorithms quiz.
- Explore the Sorting Algorithms visualizer: Bubble, selection, insertion, merge and quick sort animated.
How to study this topic
- Read the explanation above and restate the definition in your own words.
- Open the interactive lesson and answer its mini quiz.
- Read the common pitfall, then write a short example that avoids it.
- Check your understanding in the Algorithms quiz, and revisit the topic if you miss a question.
Frequently asked questions
What is Shell Sort?
Where do people use Shell Sort?
What is the most common mistake with Shell Sort?
What should I learn before this topic?
How can I practice it?
Is Shell Sort hard to learn?
How long does it take to learn Shell Sort?
Which topics are related to Shell Sort?
What should I study after Shell Sort?
Related topics
- Heap Sort (Algorithms)
- Bucket Sort (Algorithms)
- Sliding Window (Algorithms)
- Timsort (Algorithms)
- Shell Scripting (DevOps & Tooling)
- Shell and Command Line (Operating Systems)