理论
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
void Merge(vector<int>& v, int left,int right,int mid)
{
vector<int> t(v.size(), 0);
int i = left;
int j = mid + 1;
int k = left;
while(i<=mid && j<=right)
{
{
if (v[i] >= v[j])
{
t[k++] = v[j++];
}
else
t[k++] = v[i++];
}
}
while (i <= mid)
t[k++] = v[i++];
while (j <= right)
t[k++] = v[j++];
for (int i = left; i <=right; i++)
{
v[i] = t[i];
}
}
void MSort(vector<int>& v, int left, int right)
{
if(left<right)
{
int mid = (left + right) / 2;
MSort(v, left, mid);
MSort(v, mid + 1, right);
Merge(v,left, right, mid);
}
}
int main()
{
vector<int> t{ 0,-1,100,87,200,4 };
int left = 0;
int right = t.size()-1;
MSort(t,left,right);
for (auto x : t)
cout << x << ' ';
system("pause");
return 0;
}
|