根号n段归并排序(递归)
题目描述
根号n 段合并排序算法:
将数组 划分为 根号n个子数组,每个子数组有根号n 个元素。然后递归地对分割后的子数组进行排序,最后将所得到的根号n 个排好序的子数组合并排序。
#include<stdio.h>
#include<math.h>
#include<iostream>
#include<algorithm>
using namespace std;
void Merging(int *arr, int first1, int last1, int first2, int last2)//将两个子数组合并排序
{
int arr3[last2-first1+1];
int arr1[last1-first1+1];
int arr2[last2-first2+1];
int c=last2-first1+1;
int a=last1-first1+1;
int b=last2-first2+1;
int a1=0, b1=0, c1=0;
for(int i=0;i<a;i++)
{
arr1[i]=arr[i+first1];
}
for(int i=0;i<b;i++)
{
arr2[i]=arr[i+first2];
}
for(a1=0, b1=0;a1<a&&b1<b; )
{
if(arr1[a1]<=arr2[b1])
{
arr3[c1]=arr1[a1];
a1++;
c1++;
}
else
{
arr3[c1]=arr2[b1];
c1++;
b1++;
}
}
if(a1<a)
{
while(a1<a)
{
arr3[c1++]=arr1[a1++];
}
}
else
{
while(b1<b)
{
arr3[c1++]=arr2[b1++];
}
}
for(int i=0;i<c;i