#include <iostream>
#include <vector>
#include <algorithm>
void quicksort(std::vector<int>& vec, int nbegin, int nend)
{
if (nbegin >= nend)
return;
int nPos = (nbegin + nend) / 2;
int nPosV = vec[nPos];
std::vector<int> less;
std::vector<int> more;
for (int i = nbegin; i <= nend; ++i)
{
if (i == nPos)
continue;
if (vec[i] < vec[nPos])
less.push_back(vec[i]);
else
more.push_back(vec[i]);
}
// ºÏ²¢vector ¿ÕŒä»»Ê±Œä
for (int i = 0; i < less.size(); ++i)
vec[nbegin + i] = less[i];
vec[nbegin + less.size()] = nPosV;
nPos = nbegin + less.size();
for (int i = 0; i < more.size(); ++i)
vec[nbegin + less.size() + i + 1] = more[i];
quicksort(vec, nbegin, nPos);
quicksort(vec, nPos + 1, nend);
}
int main()
{
std::vector<int> v{ 1, 7, 8, 9, 4, 5, 2 };
quicksort(v, 0, v.size() - 1);
int a;
return 0;
}
快速排序的实现
最新推荐文章于 2023-02-14 00:09:48 发布