当两个数组有序时,如何进行就地排序?
我正在研究这个问题。我的函数原型是
static void Sort(byte[] arr, int leftPos, int rightPos)
在函数的第二部分中,我知道 leftPos 到 leftPos + (rightPos-leftPos)/2 和 (rightPos-leftPos)/2 到 rightPos 按顺序排序。
我尝试思考如何在知道这两个部分按顺序进行的情况下进行就地排序。我想不出任何一个。我查看了 合并排序 上的合并函数,但它使用输出数组而不是就地排序。
知道两个切片都按顺序排列时,如何对其进行排序?
注意:我想我可以传入一个与主数组长度相同的额外数组用作临时内存,但我想到的方式需要我在每次合并后执行 Array.Copy 。
I am working on this question. My function prototype is
static void Sort(byte[] arr, int leftPos, int rightPos)
In the 2nd part of the function i know leftPos to leftPos + (rightPos-leftPos)/2 and (rightPos-leftPos)/2 to rightPos are sorted in order.
I tried thinking of how i could do an in place sort knowing the two parts are in order. I couldnt think of any. I looked at the merge function on merge sort but it uses an output array rather than in place.
How do i sort it in place knowing both slices are in order?
Note: I was thinking i could pass in a extra array that is the same length as the main array to use as temp memory but the way i thought of would require me to do Array.Copy after each merge.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
就地合并是可能的,但它很复杂并且不会带来太多性能提升。下面是此处的一些示例代码。
from
是您的leftPos
,to
是您的rightPos
,pivot
是您的(rightPos-leftPos)/2
长度是每一半的长度。In-place merge is possible, but it's complicated and doesn't give much performance gain. Below is some sample code from here.
from
is yourleftPos
,to
is yourrightPos
,pivot
is your(rightPos-leftPos)/2
and the lengths are the lengths of each half.