目录
一、题目
二、代码
一、题目
912. 排序数组 - 力扣(LeetCode)
二、代码
class Solution {
public:
void _MergeSort(vector<int>&data,vector<int>&tmp,int begin,int end)
{
if(begin>=end)
return;//结束条件
int mid = (begin+end)/2;
_MergeSort(data,tmp,begin,mid);
_MergeSort(data,tmp,mid+1,end);
int j=begin;
int k=begin;
int begin1=begin,end1=mid;
int begin2=mid+1,end2=end;
while(begin1<=end1&&begin2<=end2)
{
if(data[begin1]<data[begin2])
{
tmp[k++] = data[begin1++];
}
else
{
tmp[k++] =data[begin2++];
}
}
while(begin1<=end1)
{
tmp[k++] = data[begin1++];
}
while(begin2<=end2)
{
tmp[k++] = data[begin2++];
}
for(j;j<=end2;j++)
{
data[j] = tmp[j];
}
}
vector<int> sortArray(vector<int>& nums) {
vector<int>tmp(nums.size(),0);
_MergeSort(nums,tmp,0,nums.size()-1);
return nums;
}
};