最近换了Thinkbook16+的新电脑,跑代码比实验室的台式机还舒服,以快速排序这一伟大算法抒发心中愉悦之情。
void quick_sort(vector<int>& input, int begin, int end)
{
if (begin >= end)
return;
int temp = input[begin];
int i=begin, j=end;
while (i<j)
{
while (i<j&&temp <= input[j])
j--;
while (i<j&&temp >= input[i])
i++;
if (i<j)
{
std::swap(input[i], input[j]);
}
}
input[begin] = input[i];
input[i] = temp;
quick_sort(input, begin, i - 1);
quick_sort(input, i + 1, end);
}
void quickSort(vector<int>& array)
{
int n = array.size() - 1;
quick_sort(array, 0, n);
}