我正在学习算法第4期Robert Sedgewick的快速排序。快速排序:分区分析
我想知道的快速排序代码下面分区的长度为N的一个阵列
private static int partition(Comparable[] a, int lo, int hi)
{
int i = lo, j = hi+1;
while (true)
{
while (less(a[++i], a[lo]))
if (i == hi) break;
while (less(a[lo], a[--j]))
if (j == lo) break;
if (i>= j) break;
exch(a, i, j);
}
exch(a, lo, j);
return j;
}
我想知道Ť比较的数量(n)的代码,如上所述。 (对比)
在我看来,需要~2N比较。
原因是因为它需要N比较i
从左向右移动,并且j
分别从已经排序的数组(例如数组(A,B,C,D,E, F,G,H)。
比较少=()
你是什么意思'数组中的比较数? – lsof
是'number of compare'被引用'if(i> = j)break;'statement? – lsof
@RajasubaSubramanian只有'less()'。那么比较的数量是多少? – progresivoJS