-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort_LC164_quickSortExe.cpp
95 lines (87 loc) · 1.69 KB
/
sort_LC164_quickSortExe.cpp
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
Maximum Gap
*/
#include<iostream>
using namespace std;
#include<vector>
/*
.quicksortExercise
*/
class Solution {
public:
int maximumGap(vector<int>& nums) {
if (nums.size() < 2) {
return 0;
}
quickSort(nums, 0, nums.size());
int res = 0;
int time = nums.size() - 1;
int i = 0;
/*
while (time-- > 0) {
res = max(res, nums[++i] - nums[i]);
}
*/
for (int i = 0; i <= time; i++) {
printf("%d", nums[i]);
}
return res;
}
private:
int max(int a, int b) {
return (a > b) ? a : b;
}
/*
quick sort for exercise
*/
/* open interval */
void quickSort(vector<int> &nums, int low, int high) {
if (high - low < 2)return;
int mid = partition(nums, low, high-1);
quickSort(nums, low, mid);
quickSort(nums, mid + 1, high);
}
/* closed interval*/
int partition(vector<int> &nums, int low, int high) {
int randIndex = low + rand() % (high - low + 1);
swap(nums[low], nums[randIndex]);
int pivot = nums[low];
while (low < high) {
while (low < high&&nums[high] >= pivot) {
high--;
}
nums[low] = nums[high];
while (low < high&&nums[low] <= pivot) {
low++;
}
nums[high] = nums[low];
}
nums[low] = pivot;
return low;
}
void swap(int &a, int &b) {
int temp = a;
a = b;
b = temp;
}
};
int main() {
Solution so;
vector<int> nums = { 3,6,9,1,3,7,4 };
so.maximumGap(nums);
system("pause");
return 0;
}
/*
for the LC question
Try to solve it in linear time/space
Bucket Sort, I will be back after reviewing the bucket sort.
*/
class Solution {
public :
int maximumGap(vector<int> &nums) {
if (nums.size() < 2) {
return 0;
}
}
};