Shell sort is an algorithm that first sorts the elements far apart from each other and successively reduces the interval between the elements to be sorted. It is a generalized version of insertion sort.
In shell sort, elements at a specific interval are sorted. The interval between the elements is gradually decreased based on the sequence used. The performance of the shell sort depends on the type of sequence used for a given input array.
shellSort(array, size)
for interval i <- size/2n down to 1
for each interval "i" in array
sort all the elements at interval "i"
end shellSort
Shell sort is an unstable sorting algorithm because this algorithm does not examine the elements lying in between the intervals.
- When the array is already sorted, the total number of comparisons for each interval (or increment) is equal to the size of the array.
- It occurs when the elements of the array are in jumbled order (neither ascending nor descending).
The space complexity for shell sort is O(1)
Shell sort is used when
- calling a stack is overhead.
- recursion exceeds a limit.
- Insertion sort does not perform well when the close elements are far apart. Shell sort helps in reducing the distance between the close elements. Thus, there will be less number of swappings to be performed.
- Python
python3 ShellSort.py