1.6 希尔排序 #80
utterances-bot
started this conversation in
Comments
Replies: 10 comments 4 replies
|
7 line 需要对间隔为gap的数据进行插入排序 第7行应该为 range(0,size,gap) |
0 replies
这应该是两种写法。
这里的第 7 ~ 20 行每遍历一次,就是对每组内元素进行了插入排序。并不存在重复计算。 而另一种每次增加 gap 的遍历(即 range(0, size, gap))则需要再套一层循环(即 range(0, gap))对分组下进行插入排序。 |
0 replies
|
理解了 确实是两个思路不一样 谢谢 |
0 replies
public class Solution {
public int[] shellSort(int[] arr) {
int size = arr.length;
int gap = size / 2;
while (gap > 0) {
for (int i = gap; i < size; i++) {
int temp = arr[i];
int j = i;
while (j >= gap && arr[j - gap] > temp) {
arr[j] = arr[j - gap];
j -= gap;
}
arr[j] = temp;
}
gap = gap / 2;
}
return arr;
}
public int[] sortArray(int[] nums) {
return shellSort(nums);
}
} |
0 replies
|
0 replies
|
博主,有个问题想请教下,这里的gap参数应该指的是下标位置的固定间隔数是吧?因为我看到过程图中的真实元素间隔数 = gap - 1 |
0 replies
|
总觉得j = i有点奇怪,这样是不是清楚一些。 def shell_sort(lst):
gap = len(lst) // 2
while gap >= 1:
for i in range(gap,len(lst)): # i:抽出固定与其他牌比较的牌
j = i - gap # 与i进行比较的牌
temp = lst[i]
while j >= 0 and lst[j] > temp:
lst[j+gap] = lst[j]
j -= gap
else:
lst[j+gap] = temp
gap = gap // 2
return lst
lst = [7,2,6,8,0,4,1,5,9,3]
print(shell_sort(lst)) |
2 replies
|
竟然有人看😲好嘞辛苦
ITCharge ***@***.***>于2024年8月2日 周五上午9:01写道:
… 这个粘贴缩进全没了,铸币啊
没事,我帮你改一下
—
Reply to this email directly, view it on GitHub
<#80 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/BFTAO575LKNEQ6BN23N4HY3ZPLK7FAVCNFSM6AAAAABL243MXKVHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTAMRRG44DSNY>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
0 replies
def shell(lst):
gap = len(lst) // 2
while gap > 0:
for i in range(gap,len(lst)):
temp = lst[i]
j = i - gap
while j >= 0 and lst[j] > temp:
lst[j+gap] = lst[j]
j -= gap
else:
lst[j+gap] = temp
gap = gap // 2
return lst
lst = [7,2,6,8,0,4,1,5,9,3]
print(shell(lst)) |
2 replies
|
python3.9里会报警告:应为类型 [int],但实际为 list[Literal[5, 4, 3, 2, 1]] |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
1.6 希尔排序
希尔排序 # 1. 希尔排序算法思想 # 希尔排序(Shell Sort)基本思想: 将整个序列切按照一定的间隔取值划分为若干个子序列,每个子序列分别进行插
https://algo.itcharge.cn/01_array/01_06_array_shell_sort/
All reactions