cpp
复制代码
class Solution {
public:
vector<int> relativeSortArray(vector<int>& arr1, vector<int>& arr2) {
int cur = 0;
for(int i = 0; i < arr2.size(); i++)
{
// arr2[i]
for(int j = cur; j < arr1.size(); j++)
{
if(arr1[j] == arr2[i])
{
swap(arr1[cur],arr1[j]);
cur += 1;
}
}
}
if(cur < arr1.size())
{
int left = cur;
int right = arr1.size() - 1;
quicksort(arr1, left, right);
}
return arr1;
}
void quicksort(vector<int>& arr1, int left, int right)
{
if(left>=right)
return;
int start = left;
int end = right;
int pivot = arr1[left];
while(left < right)
{
while(arr1[right]>=pivot && left<right)
{
right -= 1;
}
while(arr1[left]<=pivot && left<right)
{
left += 1;
}
swap(arr1[left],arr1[right]);
}
swap(arr1[left],arr1[start]);
quicksort(arr1, start, left - 1);
quicksort(arr1, left + 1, end);
}
void swap(int& a, int& b)
{
int tmp = a;
a = b;
b = tmp;
}
};