考察排序算法,但是系统判定,快排和插入哪个都不好使,会超时,最后库函数提交通过
#include <iostream>
#include <vector>
using namespace std;
// class Solution {
// public:
// vector<int> sortArray(vector<int>& nums) {
// for (int i = 1; i < nums.size(); i++){
// int temp = nums[i];
// int j = i;
// while (temp < nums[j - 1] && j > 0){
// nums[j] = nums[j - 1];
// j--;
// }
// nums[j] = temp;
// }
// return nums;
// }
// };
class Solution {
public:
int Apart (vector<int>& nums, int left, int right) {
int index = left;
while (left < right){
while (left < right) {
if (nums[right] < nums[index]){
swap(nums[right], nums[index]);
index = right;
left++;
break;
}
else {
right--;
}
}
while (left < right) {
if (nums[left] > nums[index]){
swap(nums[left], nums[index]);
index = left;
right--;
break;
}
else {
left++;
}
}
}
return index;
}
vector<int> sort(vector<int>& nums, int left, int right) {
if (left < right) {
int index = Apart(nums, left, right);
sort(nums, left, index);
sort(nums, index + 1, right);
}
return nums;
}
vector<int> sortArray(vector<int>& nums) {
return sort(nums, 0, nums.size() - 1);
}
};
int main() {
Solution s;
vector<int> nums = {5, 3, 2, 1};
vector<int> sortedNums = s.sortArray(nums);
for (auto i : sortedNums)
cout << i << " ";
return 0;
}