#include <iostream>
using namespace std;
#include <string.h>
#include <vector>
void merge(int low,int mid,int high,vector<int>& ans,vector<int>& tem){
int l = low;
int h = mid + 1;
int k = low;
while(l <= mid && h <= high){
if(ans[l] < ans[h])
tem[k++] = ans[l++];
else
tem[k++] = ans[h++];
}
while(l <= mid)
tem[k++] = ans[l++];
while(h <= high)
tem[k++] = ans[h++];
for(int i = low;i <= high;i++)
ans[i] = tem[i];
}
void mergesort(int low,int high,vector<int>& ans,vector<int>& tem){
/*归*/
if(low >= high)
return ;
int mid = low + (high - low) / 2;
mergesort(low,mid,ans,tem);
mergesort(mid+1,high,ans,tem);
/*并*/
merge(low,mid,high,ans,tem);
}
int main()
{
vector<int> ans = {20,10,49,23,1,39,76,58,19,30};
vector<int> tem(10,0);
int n = ans.size() - 1;
mergesort(0,n,ans,tem);
for(int i : ans)
cout<<i<<endl;
return 0 ;
}
c++归并排序
最新推荐文章于 2024-06-22 23:53:37 发布