shellsort fun
Here is a imaginary scenario to help your brain pay attention:
Let's imagine there is a cooperation called ridicu Co. and you are the HR there, so that you get a personal feeling of the shell sort. Insertion sort is best suit for a bureaucracy system where only one level promotion is allowed at a time. An employee is suppose to be at the right position, or almost the right position, so that little promotion is needed for the system to be "sorted". In case you put CEO, CTO, CFO etc. to the entry level, it might take tons of promotions before those guys swim to the top. A logical improvement is to promote the "yet to be C*O" a few levels at a time instead of one level at a time. Each promotion will be a right decision by statistics. However, we can not apply this policy to all employees, if we promote an employee a few levels at a time, chances are, the decision will be wrong.
Shell sort is an improved bureaucracy system. It based on the assumption that the array is almost sorted, however, a few elements are ridiculously out of position. The goal is to move these out of position elements to the right place with a different sorting policy, then use insertion sort to finish the sorting. The different policy, as you guess, is to compare with the left neighbor h steps away instead of the closest left neighbor. If the element is smaller, then swap and continue, otherwise stop. The larger the value h is, and more ridiculously out of position an element is, the more favor is put on them by the system.
If your brain feels sleepy, here is a bad joke.
What I forget to mention is, in the imaginary organization ridicu Co., there is only one CEO position, when someone get promotion, someone get demotion to swap the position. In case the "yet to be CEO" reaches the top via 3 very aggressive promotions, 3 innocent guys get demoted to swap the position. They are ridiculously (little bit less than CEO was) out of position. Don't worry, there is another round of special promotions to take care of the victims. This time, the promotion policy is less aggressive, say only 3 level jump is allowed, each of the victims swim back to the right position via a few promotions -- leaving more victims behind. Now the special policy ceases to exist, back to 1 level only bureaucracy. The system is fine, there are still chance for promotion anyway.
Similarly, shell sort takes this strategy. During the first round, h is large, so those out of place elements aggressively moves. Next round, if we don't change h value, chances are, no ridiculously out of place elements left, after previous move, that still qualified for h level promotion. So in order to continue the promotion, we need to use a smaller h value, so that there some elements aggressively moves left. Then we do the third sorting round, with even smaller h value. Each round, there are elements move left, but with a pace more than 1. Overall, shell sort do some large pace left move, some moderate pace left move, a few 1 step left move, with the cost of some out of position right-ward move due to swap -- in most cases, better than insertion sort, which makes 1 step only left move, no bad right-ward move. Yes, the imaginary pure bureaucracy system don't make mistakes, it just very inefficient.
Both insertion sort and shell sort are inhuman, we are talking about ridicu Co. anyway.