标签:算法 quicksort partitioning 3-way
3-way partitioning:
将数组分为三份:(小于、等于、大于拆分值)
?Let v be partitioning item a[lo].
?Scan i from left to right.
– (a[i] < v): exchange a[lt] with a[i]; increment both lt and i
– (a[i] > v): exchange a[gt] with a[i]; decrement gt
– (a[i] == v): increment i
void threeWayQsort(int a[], int lo, int hi)
{
if(hi <= lo)
return;
int lt = lo;
int gt = hi;
int parVal = a[lo];
int i = lo;
while(i <= gt)
{
if(a[i] < parVal)
swap(a[i++],a[lt++]); //lt: start pos of (items = parVal)
else if(a[i] > parVal) //gt: end pos of (items = parVal)
swap(a[i],a[gt--]);
else
i++;
}
threeWayQsort(a, lo, lt-1);
threeWayQsort(a, gt+1, hi);
}
Application:
Color Sort:
标签:算法 quicksort partitioning 3-way
原文地址:http://blog.csdn.net/kzq_qmi/article/details/46454865