标签:str highlight string stream merge void IV lse ios
#include<iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
int n;
void Merge(int *r1, int low, int mid, int high)
{
// int *r2 = new int(high - low + 1);
int *r2;
r2 = (int *)malloc(sizeof(int) * (high - low + 1));
int i = low, j = mid + 1, k = 0;
while(i <= mid && j <= high)
{
if(r1[i] < r1[j])
r2[k++] = r1[i++];
else
r2[k++] = r1[j++];
}
while(i <= mid)
{
r2[k++] = r1[i++];
}
while(j <= high)
{
r2[k++] = r1[j++];
}
for(i = 0, j = low; i < k && j <= high; i++)
r1[j++] = r2[i];
// cout << "test: " << low << " " << high << endl;
// for(int i = 0; i < k; i++){
// cout << r2[i] << " " ;
// }
// cout << endl;
}
void Msort(int *r1, int low, int high)
{
if(low < high)
{
int mid = (low + high) / 2;
Msort(r1, low, mid);
Msort(r1, mid + 1, high);
Merge(r1, low, mid, high);
}
}
int main()
{
cin >> n;
int num[1000];
for(int i = 1; i <= n; i++)
cin >> num[i];
Msort(num, 1, n);
for(int i = 1; i <=n; i++)
cout << num[i] << " ";
}
标签:str highlight string stream merge void IV lse ios
原文地址:https://www.cnblogs.com/zhumengdexiaobai/p/9218392.html