Shellsort
Algorithm · Stretch
Step through it: Sort First pattern
Step 1 of 4
Goal: does the list contain a duplicate? Checking every pair works but is slow. Idea: sort first, so equal numbers end up side by side.
Step 2 of 4
After sorting: 2, 2, 5, 7, 9. Equal numbers are now neighbours.
Step 3 of 4
Now one pass comparing each number with the next: 2 and 2 match. Duplicate found. Sorting often turns a hard search into a simple neighbour check, and makes tricks like two pointers and interval merging possible.
Step 4 of 4
Your turn.
Complexity
| Name | Best | Average | Worst | Memory | Stable | Comments |
|---|---|---|---|---|---|---|
| Shell sort | n log(n) | depends on gap sequence | n (log(n))2 | 1 | No |
Shellsort, also known as Shell sort or Shell's method, is an in-place comparison sort. It can be seen as either a generalization of sorting by exchange (bubble sort) or sorting by insertion (insertion sort). The method starts by sorting pairs of elements far apart from each other, then progressively reducing the gap between elements to be compared. Starting with far apart elements, it can move some out-of-place elements into position faster than a simple nearest neighbor exchange
How Shell Sort Works
For our example and ease of understanding, we take the interval
of 4. Make a virtual sub-list of all values located at the
interval of 4 positions. Here these values are
{35, 14}, {33, 19}, {42, 27} and {10, 44}
We compare values in each sub-list and swap them (if necessary) in the original array. After this step, the new array should look like this
Then, we take interval of 2 and this gap generates two sub-lists
{14, 27, 35, 42},{19, 10, 33, 44}
We compare and swap the values, if required, in the original array. After this step, the array should look like this
UPD: On the picture below there is a typo and result array is supposed to be
[14, 10, 27, 19, 35, 33, 42, 44].
Finally, we sort the rest of the array using interval of value 1. Shell sort uses insertion sort to sort the array.
Code
import Sort from '../Sort';
export default class ShellSort extends Sort {
sort(originalArray) {
// Prevent original array from mutations.
const array = [...originalArray];
// Define a gap distance.
let gap = Math.floor(array.length / 2);
// Until gap is bigger then zero do elements comparisons and swaps.
while (gap > 0) {
// Go and compare all distant element pairs.
for (let i = 0; i < (array.length - gap); i += 1) {
let currentIndex = i;
let gapShiftedIndex = i + gap;
while (currentIndex >= 0) {
// Call visiting callback.
this.callbacks.visitingCallback(array[currentIndex]);
// Compare and swap array elements if needed.
if (this.comparator.lessThan(array[gapShiftedIndex], array[currentIndex])) {
const tmp = array[currentIndex];
array[currentIndex] = array[gapShiftedIndex];
array[gapShiftedIndex] = tmp;
}
gapShiftedIndex = currentIndex;
currentIndex -= gap;
}
}
// Shrink the gap.
gap = Math.floor(gap / 2);
}
// Return sorted copy of an original array.
return array;
}
}References
Practice
Sort First
- Contains DuplicateEasy
- Valid AnagramEasy
- Meeting RoomsEasy
- Merge Sorted ArrayEasy
- Sort ColorsMedium
- Largest NumberMedium
- H-IndexMedium
- Wiggle Sort IIMedium
- Sort an ArrayMedium
- Custom Sort StringMedium
- Insertion Sort ListMedium
- Maximum GapHard
- Count of Smaller Numbers After SelfHard
Spotted a mistake or something unclear? Report it on GitHub.