-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0912.cpp
More file actions
51 lines (47 loc) · 1.34 KB
/
Copy path0912.cpp
File metadata and controls
51 lines (47 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class Solution {
private:
bool isSorted(vector<int> &nums) {
int previous = nums[0];
for (int num : nums) {
if (num < previous) return false;
previous = num;
}
return true;
}
void swap(vector<int> &nums, int i, int j) {
int value = nums[i];
nums[i] = nums[j];
nums[j] = value;
}
void shuffleArray(vector<int> &nums) {
for (int i = 0; i < nums.size(); i++)
swap(nums, i, rand() % nums.size());
}
int partition(vector<int> &nums, int start, int end) {
int pivot = end;
int left = start - 1, right = start;
while (right <= end) {
if (nums[right] <= nums[pivot]) {
swap(nums, right, left+1);
left++;
}
right++;
}
return left;
}
void quickSort(vector<int> &nums, int start, int end) {
if (start >= end) return;
int sortedIndex = partition(nums, start, end);
quickSort(nums, start, sortedIndex - 1);
quickSort(nums, sortedIndex + 1, end);
}
public:
vector<int> sortArray(vector<int> &nums) {
vector<int> sorted = nums;
if (!isSorted(nums)) {
shuffleArray(sorted);
quickSort(sorted, 0, nums.size() - 1);
}
return sorted;
}
};