void quiksort(vector<int> &vec, int i, int j) { if (i < j) { int temp = vec[i];//存储基准值 int left = i; int right = j; while (left < right) { //后指针向前遍历,寻找比基准值小的数字 while (left < right && vec[right] >= temp) { right--; } //填数 vec[left] = vec[right]; //前指针向后遍历,寻找比基准值大的数字 while (left < right && vec[left] <= temp) { left++; } //填数 vec[right] = vec[left]; } vec[right] = temp; //递归调用 quiksort(vec, i, right - 1); quiksort(vec, right + 1, j); } }
int main() { int n; int temp; vector<int> vec = {}; scanf_s("%d", &n); while (n > 0) { scanf_s("%d", &temp); vec.push_back(temp); n--; } quiksort(vec, 0, vec.size() - 1); for (int i = 0; i < vec.size(); i++) { cout << vec[i]<<" "; } system("pause"); return 0; }