希尔排序算法示例

什么是希尔排序?
Shell Sort, also called Shell's method, is an efficient in-place comparison-based sorting algorithm. Named after Donald Shell, who introduced the idea in 1959, it is a generalized extension of insertion sort that overcomes its quadratic behavior on scattered data.
基本思路是将相距较远的元素分组,对每组元素进行插入排序,然后逐步缩小组间距,直到间距变为 1。此时数组就基本排序完成了。
This gap, the interval, follows a chosen sequence such as Shell's original, Knuth's, Hibbard's, or Sedgewick's. Shell's original is n/2, n/4, ..., 1.
希尔排序算法
步骤1) 初始化区间值 h = n/2,其中 n 为数组的大小。
步骤2) 将距离为 h 的所有元素放入子列表中。
步骤3) 使用插入排序对每个子列表进行排序。
步骤4) 设定一个新的区间 h = h/2。
步骤5) 如果 h > 0,则返回步骤 2。否则,转到步骤 6。
步骤6) 现在生成的数组已完全排序。
希尔排序的工作原理
插入排序中,元素每次只移动一个位置。而希尔排序则根据一定的间隔将数组分割成若干个间隔较大的子列表,然后对每个子列表执行插入排序。
随着时间间隔的缩短,子列表的大小会增加。由于早期遍历会留下部分排序的数据,因此较小的时间间隔所需的交换次数远少于运行 插入排序 从零开始。下图展示了一次希尔排序过程。
希尔排序算法的工作原理及示例
Let's sort the array below using Shell Sort.
步骤1) 数组大小为 8,因此初始区间值为 h = 8/2 = 4。
步骤2) 将元素分组,每组元素相隔四个位置。子列表:{8, 1}, {6, 4}, {7, 5}, {2, 3}。
步骤3) 使用插入排序对每个子列表进行排序。元素移动时,一个临时变量会保存被插入的值。交换后,数组如下所示。
步骤4) 减小区间。新的区间为 h = 4/2 = 2。
步骤5) 因为 2 > 0,所以返回步骤 2,将相隔两个位置的元素分组:{1, 5, 8, 7} 和 {4, 2, 6, 3}。
对第一个子列表进行排序。数组变为:
对第二个子列表进行排序后:
再次将间隔减小到 h = 2/2 = 1。间隔为 1 时,希尔排序将对整个数组进行最后一次插入排序,如下所示。
步骤6) 再次将该区间除以 0,结果为 0。数组现在已完全排序:
伪Code 谢尔排序
Start Input array a of size n for (interval = n / 2; interval > 0; interval /= 2) for (i = interval; i < n; i += 1) temp = a[i]; for (j = i; j >= interval && a[j - interval] > temp; j -= interval) a[j] = a[j - interval]; a[j] = temp; End
C/ 中的 Shell 排序程序C++
输入:
//Shell Sort Program in C/C++ #include <bits/stdc++.h> using namespace std; void ShellSort(int data[], int size) { for (int interval = size / 2; interval > 0; interval /= 2) { for (int i = interval; i < size; i += 1) { int temp = data[i]; int j; for (j = i; j >= interval && data[j - interval] > temp; j -= interval) { data[j] = data[j - interval]; } data[j] = temp; } } } int main() { int data[] = {8, 6, 7, 2, 1, 4, 5, 3}; int size = sizeof(data) / sizeof(data[0]); ShellSort(data, size); cout << "Sorted Output: \n"; for (int i = 0; i < size; i++) cout << data[i] << " "; cout << "\n"; }
输出:
Sorted Output:
1 2 3 4 5 6 7 8
希尔排序示例 Python
输入:
#Shell Sort Example in Python def ShellSort(data, size): interval = size // 2 while interval > 0: for i in range(interval, size): temp = data[i] j = i while j >= interval and data[j - interval] > temp: data[j] = data[j - interval] j -= interval data[j] = temp interval //= 2 data = [8, 6, 7, 2, 1, 4, 5, 3] ShellSort(data, len(data)) print('Sorted Output:') print(data)
输出:
Sorted Output:
[1, 2, 3, 4, 5, 6, 7, 8]
希尔排序的应用
在注重栈空间或简洁性的现代系统中,希尔排序仍然被使用。
- 此 Linux内核 在需要避免调用堆栈的地方使用希尔排序。
- uClibc嵌入式C库使用Shell排序来保持较低的内存使用量。
- bzip2 使用希尔排序来避免在块排序过程中出现深度递归。
- 对于递归受限的小型数据集,嵌入式固件倾向于使用希尔排序。
希尔排序的优点和缺点
| 优势 | 缺点 |
|---|---|
| 不需要调用堆栈,这对于嵌入式系统来说是理想的。 | 对于非常大的数组来说,这不是最快的选择。 |
| 易于实现,只需少量代码。 | 对于元素分布范围较广的数据,性能会下降。 |
| 对于中等规模或部分排序的数组,效率很高。 | 最坏情况下的时间复杂度对所选的间隙序列很敏感。 |
| 由于是就地工作,因此它使用持续的辅助内存。 | 它不是一种稳定的排序方式,因此相同的键可能会改变相对顺序。 |
希尔排序复杂度分析
希尔排序的时间复杂度
希尔排序的时间复杂度取决于所使用的间隔序列。
在最好的情况下,当数组已经基本排列好时,每次遍历只需要对数次数的测试,即 O(n log n)。
In the worst case, the array is arranged so elements need the maximum comparisons, and the final increment dominates at O(n^2) with Shell's original sequence.
- 最佳情况下的复杂度:O(n log n)
- 平均情况下的复杂度:O(n log n) 到 O(n^(4/3)),具体取决于空位序列。
- Worst-case complexity: O(n^2) with Shell's original sequence
虽然 Sedgewick 和 Ciura 序列在实践中表现良好,但最佳通用间隙序列仍然是一个开放的研究问题。
希尔排序空间复杂度
希尔排序不需要辅助数组,因此空间复杂度为 O(1),与输入规模无关,这是它最大的实际优势之一。










