generated from threeal/project-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolution.cpp
33 lines (28 loc) · 921 Bytes
/
solution.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
#include <string>
#include <vector>
class Solution {
public:
std::vector<std::string> sortPeople(
std::vector<std::string>& names, std::vector<int>& heights) {
quickSort(names.data(), heights.data(), 0, names.size() - 1);
return names;
}
// This function implements quick sort to sort the given range of arrays.
// See: https://www.geeksforgeeks.org/quick-sort/
void quickSort(
std::string* names, int* heights, int low, int high) {
if (low >= high) return;
int pivot = low - 1;
for (int i = low; i < high; ++i) {
if (heights[i] <= heights[high]) continue;
++pivot;
std::swap(names[pivot], names[i]);
std::swap(heights[pivot], heights[i]);
}
++pivot;
std::swap(names[pivot], names[high]);
std::swap(heights[pivot], heights[high]);
quickSort(names, heights, low, pivot - 1);
quickSort(names, heights, pivot + 1, high);
}
};