#include<iostream>
#include<fstream>
#include<stdio.h>
#include<stdlib.h>
#include<string>
//#include <array>
#include <sstream>
using namespace std;
void Merge(int a[],int tmp[],int lPos,int rPos,int rEnd)
{
int i, lEnd, NumElements, tmpPos;
lEnd = rPos - 1;
tmpPos = lPos;
NumElements = rEnd - lPos + 1;
while (lPos <= lEnd&&rPos <= rEnd)
{
if (a[lPos] <= a[rPos])
tmp[tmpPos++] = a[lPos++];
else
tmp[tmpPos++] = a[rPos++];
}
while (lPos <= lEnd)
tmp[tmpPos++] = a[lPos++];
while (rPos <= rEnd)
tmp[tmpPos++] = a[rPos++];
for (i = 0; i < NumElements; i++, rEnd--)
{
a[rEnd] = tmp[rEnd];
}
}
void msort(int a[], int tmp[], int low, int high)
{
if (low >= high)
return;
int middle = (low+high)/2;
msort(a, tmp, low, middle);
msort(a,tmp,(middle),high);
Merge(a,tmp,(middle+1),high);
}
void merge_sort(int a[],int len)
{
int *tmp = NULL;
tmp = new int[len];
if(tmp! = NULL)
{
msort(a,tmp,0,len-1);
delete []temp;
}
}
//打印
void print_arrary(int a[], int len)
{
int i;
for (i = 0; i < len; i++)
{
cout << a[i] << "";
}
cout << endl;
}
int main(int argc, int *argv[])
{
int a[] = { 45, 68, 20, 39, 88, 97, 46, 59 };
merge_sort(a,8);
return 0;
}
#include<fstream>
#include<stdio.h>
#include<stdlib.h>
#include<string>
//#include <array>
#include <sstream>
using namespace std;
void Merge(int a[],int tmp[],int lPos,int rPos,int rEnd)
{
int i, lEnd, NumElements, tmpPos;
lEnd = rPos - 1;
tmpPos = lPos;
NumElements = rEnd - lPos + 1;
while (lPos <= lEnd&&rPos <= rEnd)
{
if (a[lPos] <= a[rPos])
tmp[tmpPos++] = a[lPos++];
else
tmp[tmpPos++] = a[rPos++];
}
while (lPos <= lEnd)
tmp[tmpPos++] = a[lPos++];
while (rPos <= rEnd)
tmp[tmpPos++] = a[rPos++];
for (i = 0; i < NumElements; i++, rEnd--)
{
a[rEnd] = tmp[rEnd];
}
}
void msort(int a[], int tmp[], int low, int high)
{
if (low >= high)
return;
int middle = (low+high)/2;
msort(a, tmp, low, middle);
msort(a,tmp,(middle),high);
Merge(a,tmp,(middle+1),high);
}
void merge_sort(int a[],int len)
{
int *tmp = NULL;
tmp = new int[len];
if(tmp! = NULL)
{
msort(a,tmp,0,len-1);
delete []temp;
}
}
//打印
void print_arrary(int a[], int len)
{
int i;
for (i = 0; i < len; i++)
{
cout << a[i] << "";
}
cout << endl;
}
int main(int argc, int *argv[])
{
int a[] = { 45, 68, 20, 39, 88, 97, 46, 59 };
merge_sort(a,8);
return 0;
}