My professor gave me the following definition of Shell Sort. I've included the Bubble and Insertion Sort algorithms as well.
What is the advantage of using Shell Sort vs just a regular Insertion Sort or Bubble Sort with gap=1
? Eventually, the Shell Sort boils down to that anyway, right?
I'm not asking you to do my homework. I'm legitimately confused and want to understand what's going on.
Also, I've already visited Wikipedia and seen the Time Complexity table and I already know what they say. I'm looking for the why, not the what.
def shell(a, n):
gap = n / 2
while gap >= 1:
insertion(a, n, gap) # or bubble
gap /= 2
def bubble(a, n, gap=1):
for i in range(n):
for j in range(n-i-gap):
if a[j] > a[j+gap]:
swap(a, j, j+1)
def insertion(a, n, gap=1):
for i in range(1,n):
x = a[i]
j = i-gap
while j>=0 and a[j]>x:
a[j+gap] = a[j]
j-=gap
a[j+gap]=x
Shell Sort improves its time complexity by taking the advantage of the fact that using Insertion Sort on a partially sorted array results in less number of moves. It is a generalization of: sorting by exchange (bubble 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 the exchange of far items. In Shell sort, we make the array h-sorted for a large value of h.
Shell sort is a highly efficient sorting algorithm and is based on insertion sort algorithm. This algorithm avoids large shifts as in case of insertion sort, if the smaller value is to the far right and has to be moved to the far left.
On average, the bubble sort performs poorly compared to the insertion sort. Due to the high number of swaps, it's expected to generate twice as many write operations and twice as many cache misses.
The logic of shell sort is to sort entries that are further away first. Given a partially sorted list you can in theory sort a lot faster than O(n^2). Also given a large unsorted array the probability that your final sorted position being far from your current position is high. So logically it makes sense to use a larger gap. But the main point of shell sorts is not really its performance, instead it is the simplicity of the algorithm and the low usage of stack memory.
Given that on average it does better than O(n^2) (depends of the gap sequence), small code sizes and stack usages it is very popular in embedded applications where memory constraints are a factor.
Shell sort allows swapping of indexes that are far apart, where bubble sort only swaps items that are adjacent.
The wikipedia entries on
cover the differences.
Edit:
Imagine that you've got a bunch of cards in your hand and the cards are almost in order, except the first and last are swapped. bubble sort would be a pain to do, because there'd be about 2n swaps, insertion sort would be better with n swaps, but shell sort could do it in 1. (the number of swaps varies based on algorithm implementation, this is just an example)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With