【发布时间】:2011-04-19 10:12:08
【问题描述】:
我是否正确地说,在这两种算法中,您所做的只是获取您的结构,递归地将其分成两部分,然后以正确的顺序构建您的结构?
那么,有什么区别呢?
编辑:我找到了以下用于在快速排序中实现分区的算法,但我不太明白它是如何工作的,特别是使用 (hi + low) >>> 1 作为参数的 swop 行!任何人都可以理解这一点吗?
private static int partition( int[] items, int lo, int hi )
{
int destination = lo;
swop( items, (hi + lo) >>> 1, hi );
// The pivot is now stored in items[ hi ].
for (int index = lo; index != hi; index ++)
{
if (items[ hi ] >= items[ index ])
{
// Move current item to start.
swop( items, destination, index );
destination ++;
}
// items[ i ] <= items[ hi ] if lo <= i < destination.
// items[ i ] > items[ hi ] if destination <= i <= index.
}
// items[ i ] <= items[ hi ] if lo <= i < destination.
// items[ i ] > items[ hi ] if destination <= i < hi.
swop( items, destination, hi );
// items[ i ] <= items[ destination ] if lo <= i <= destination.
// items[ i ] > items[ destination ] if destination < i <= hi.
return destination;
}
【问题讨论】:
标签: sorting quicksort mergesort