为什么归并排序的 Merge() 函数有一个条件第二个循环?
merge1(int low, int high, int S[], U[])
{
int k = (high - low + 1)/2
for q (from low to high) U[q] = S[q]
int j = low
int p = low
int i = low + k
while (j <= low + k - 1) and (i <= high) do
{
if ( U[j] <= U[i] )
{
S[p] := U[j]
j := j+1
}
else
{
S[p] := U[i]
i := i+1
}
p := p+1
}
if (j <= low + k - 1)
{
for q from p to high do
{
S[q] := U[j]
j := j+1
}
}
}
merge_sort1(int low, int high, int S[], U[])
{
if low < high
{
int k := (high - low + 1)/2
merge_sort1(low, low+k-1, S, U)
merge_sort1(low+k, high, S, U)
merge1(low, high, S, U)
}
}
所以,基本上,这是我的课堂笔记。我发现它总体上很混乱,但我理解其中最重要的部分。我不明白的是“if (j <= low + k - 1)”部分的需要。看起来它检查左侧部分是否有任何“剩余”元素。合并排序时这可能吗?
merge1(int low, int high, int S[], U[])
{
int k = (high - low + 1)/2
for q (from low to high) U[q] = S[q]
int j = low
int p = low
int i = low + k
while (j <= low + k - 1) and (i <= high) do
{
if ( U[j] <= U[i] )
{
S[p] := U[j]
j := j+1
}
else
{
S[p] := U[i]
i := i+1
}
p := p+1
}
if (j <= low + k - 1)
{
for q from p to high do
{
S[q] := U[j]
j := j+1
}
}
}
merge_sort1(int low, int high, int S[], U[])
{
if low < high
{
int k := (high - low + 1)/2
merge_sort1(low, low+k-1, S, U)
merge_sort1(low+k, high, S, U)
merge1(low, high, S, U)
}
}
So, basically, this is on my lecture notes. I find it quite confusing in general but I understand the biggest part of it. What I don't understand is the need of the "if (j <= low + k - 1)" part. It looks like it checks if there are any elements "left" in the left part. Is that even possible when mergesorting?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
合并两个排序列表(我们称其为
left
和right
)时,您会不断取出一项并将其添加到result
列表中,直到您left
或right
列表中的项目已用完。这是由第一个while
循环完成的。现在您需要将左侧或右侧列表中剩余的元素添加到结果列表中。有两个选项:左边的列表没有元素,右边的列表还有一些。按照此处编写代码的方式,我们不需要执行任何操作,因为
S
数组的末尾已经包含right
列表中的最后一个元素。 p>右边的列表已经没有元素了,左边的列表还有一些。然后我们将剩余的元素复制到
S
的末尾。这就是merge1
末尾的if
的作用。关于您的问题此代码是否“不好”:代码是正确的,但我会进行一些更改以使其更具可读性:
left
和right
列表的中点传递给merge1
,而不是重新计算。When merging two sorted lists (let's call them
left
andright
), you keep taking one item and adding it to theresult
list, until you run out of items in either theleft
orright
list. This is done by the firstwhile
loop. Now you need to add the elements remaining in the left or right list to the result list. There are two options:The left list is out of elements, and the right list still has some. The way the code is written here, we don't need to do anything, since the end of the
S
array already contains the last elements in theright
list.The right list is out of elements, and the left list still has some. Then we copy the remaining elements to the end of
S
. This is what theif
at the end ofmerge1
does.Regarding your question if this code is "bad": The code is correct, but I would make some changes to make it more readable:
left
andright
lists tomerge1
instead of having it recalculated.