数据结构 希尔排序代码
时间: 2023-12-19 08:32:13 浏览: 75
```python
def shellSort(arr):
n = len(arr)
gap = n // 2
while gap > 0:
for i in range(gap, n):
temp = arr[i]
j = i
while j >= gap and arr[j - gap] > temp:
arr[j] = arr[j - gap]
j -= gap
arr[j] = temp
gap //= 2
arr = [12, 34, 54, 2, 3]
shellSort(arr)
print("排序后的数组:", arr) # 输出:[2, 3, 12, 34, 54]
```
相关问题
数据结构希尔排序c
以下是希尔排序的C语言实现代码:
```c
void shellSort(int arr[], int n) {
for (int gap = n / 2; gap > 0; gap /= 2) {
for (int i = gap; i < n; i++) {
int temp = arr[i];
int j;
for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) {
arr[j] = arr[j - gap];
}
arr[j] = temp;
}
}
}
```
阅读全文