Shell Sort
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).[3] The method starts by sorting pairs of elements far apart from each o ther, then progressively reducing the gap between elements to be compared. By starting with far apart elements, it can move some out-of-place elements into position faster than a simple nearest neighbor exchange. Donald Shell published the first version of this sort in 1959.[4][5] The running time of Shellsort is heavily dependent on the gap sequence it uses. For many practical variants, determining their time complexity remains an open problem. ——From Wikimedia
对于希尔排序的渐小增量d,可以理解为每组相同d的data独立出来作为一个新的子表进行直接插入排序,也可以理解为在整个数组中,有一个跳跃指针在进行自身递减的遍历运动。
1 | void shell_Sort(int A[],int n){ |
在label2的for()中,我们会在处理完每一个子表的相同位置的元素之后,才会进行下一个位置元素的对比操作(类似java的syn线程锁)。但如果理解为Wikimedia的跳跃指针的话,指针在整个表中移动,遍历完之后才会进行下一次遍历(类似上述下一个位置)。