int partition(vector<int>& vec, int lo, int hi)
{
int pivot = vec[lo];
int i = lo, j = lo;
while (i <= hi)
{
if (vec[i] < pivot) //使得区间[lo+1, j]小于pivot, [j+1, i]区间大于pivot,直到i == hi结束
{
j++;
std::swap(vec[i], vec[j]);
}
i++;
}
swap(vec[lo], vec[j]); //交换pivot
return j;
}
void quick_sort(vector<int>& vec, int lo, int hi)
{
if (lo > hi) return;
int mid = partition(vec, lo, hi);
quick_sort(vec, lo, mid-1);
quick_sort(vec, mid + 1, hi);
}
快排
最新推荐文章于 2023-07-12 13:07:43 发布