Data Structures Algorithms 简明教程

Shell Sort Algorithm

希尔排序是一种高效的排序算法,基于插入排序算法。该算法避免了插入排序情况下的大量移动,即当较小的值位于最右侧并且必须移动到最左侧时。

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.

该算法对范围较广的元素使用插入排序,先对它们进行排序,再对间隔较小的元素进行排序。这种间距被称为 interval 。该间隔是根据克努斯公式计算的:

This algorithm uses insertion sort on a widely spread elements, first to sort them and then sorts the less widely spaced elements. This spacing is termed as interval. This interval is calculated based on Knuth’s formula as −

h = h * 3 + 1
where −
   h is interval with initial value 1

它针对中等规模的数据集具有相当高的效率,因为它的平均和最坏情况复杂度为 O(n),其中 n 是项目的数量。

This algorithm is quite efficient for medium-sized data sets as its average and worst case complexity are of O(n), where n is the number of items.

Shell Sort Algorithm

以下是希尔排序的算法。

Following is the algorithm for shell sort.

1. Initialize the value of h.
2. Divide the list into smaller sub-list of equal interval h.
3. Sort these sub-lists using insertion sort.
4. Repeat until complete list is sorted.

Pseudocode

以下是希尔排序的伪代码。

Following is the pseudocode for shell sort.

procedure shellSort()
   A : array of items

   /* calculate interval*/
   while interval < A.length /3 do:
      interval = interval * 3 + 1
   end while

   while interval > 0 do:
      for outer = interval; outer < A.length; outer ++ do:

         /* select value to be inserted */
         valueToInsert = A[outer]
         inner = outer;

            /*shift element towards right*/
            while inner > interval -1 &&  A[inner - interval]
			>= valueToInsert do:
               A[inner] = A[inner - interval]
               inner = inner – interval
            end while

         /* insert the number at hole position */
         A[inner] = valueToInsert
         end for

   /* calculate interval*/
   interval = (interval -1) /3;
   end while
end procedure

Example

让我们考虑以下示例,了解希尔排序是如何工作的。我们使用在之前示例中使用过的同一个数组。为理解方便起见,我们采用 4 的间隔。创建一个虚拟子列表,其中包含所有位于 4 个位置间隔处的值。此处的这些值是 {35、14}、{33、19}、{42、27} 和 {10、14}

Let us consider the following example to have an idea of how shell sort works. We take the same array we have used in our previous examples. 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, 14}

shell sort works

我们比较每个子列表中的值,并在原始数组中交换它们(如果必要)。此步骤结束后,新数组应如下所示 −

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 −

compare values

然后,我们采用 2 的间隔,此间距生成两个子列表 - {14、27、35、42}、{19、10、33、44}

Then, we take interval of 2 and this gap generates two sub-lists - {14, 27, 35, 42}, {19, 10, 33, 44}

two sub lists

我们比较该原始数组中的值,并在必要时交换它们。此步骤结束后,数组应如下所示 −

We compare and swap the values, if required, in the original array. After this step, the array should look like this −

compare values

最后,我们使用 1 的间隔对数组的其余部分进行排序。希尔排序使用插入排序对数组进行排序。

Finally, we sort the rest of the array using interval of value 1. Shell sort uses insertion sort to sort the array.

以下是逐步描述 −

Following is the step-by-step depiction −

step by step
step by step depiction
repalce 19 to 27
replace 10 with 27
replaced 27 with 10
replace 10 19
replace 10 14
replace values sorted
replace 33 35
replaced 33 with 35
choose 44
sorted array

我们看到只交换了四次就对数组的其余部分进行排序。

We see that it required only four swaps to sort the rest of the array.

Implementation

希尔排序是一种高效的排序算法,基于插入排序算法。该算法避免了插入排序情况下的大量移动,即当较小的值位于最右侧并且必须移动到最左侧时。

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.