# 归并排序

<span style="font-size:14px;"><span style="font-size:18px;">#include <iostream>
using namespace std;

int combineSort( int a[], int low, int high )
{
int ret = 0;

if( NULL == a || low > high )
{
cout << "combineSort func: err -1, NULL==a || low > high" << endl;
ret = -1;
return ret;
}

if( low == high )
{
return ret;
}

int half = ( high + low ) / 2;

int *pArray1 = new int[ half - low + 2 ];

if( NULL == pArray1 )
{
cout << "combineSort func: err -1, NULL == pArray1" << endl;
ret = -1;
return ret;
}

int *pArray2 = new int[ high - half + 2 ];

if( NULL == pArray2 )
{
cout << "combineSort func: err -1, NULL == pArray2" << endl;
ret = -1;
return ret;
}

for( int i = low; i <= half; ++i )
{
pArray1[ i - low ] = a[i];
}

for( int i = half + 1; i <= high; ++i )
{
pArray2[ i - half - 1 ] = a[i];
}

combineSort( pArray1, 0, half - low );

combineSort( pArray2, 0, high - half - 1 );

int idx = low;
int i = 0;
int j = 0;

//cout << "begin combine" << endl;

for( i = 0, j = 0; i <= half - low && j <= high - half - 1; )
{
if( pArray1[i] < pArray2[j] )
{
a[ idx++ ] = pArray1[i];
++i;
}
else
{
a[ idx++ ] = pArray2[j];
++j;
}
}

while( i <= half - low )
{
a[ idx++ ] = pArray1[ i++ ];
}

while( j <= high - half - 1 )
{
a[ idx++ ] = pArray2[ j++ ];
}

/*

for( int i = 0; i <= half - low; ++i )
{
cout << pArray1[i] << ' ';
}
cout << endl;

for( int i = 0; i <= high - half - 1; ++i )
{
cout << pArray2[i] << ' ';
}
cout << endl;

cout << "low=" << low << ",high=" << high << endl;

for( int i = low; i <= high; ++i )
{
cout << a[i] << ' ';
}
cout << endl;*/

delete []pArray1;
pArray1 = NULL;
delete []pArray2;
pArray2 = NULL;

return ret;

}

int merge( int source[], int dest[], int start, int mid, int end )
{
int ret = 0;

if( NULL == source || NULL == dest || start > mid || mid > end )
{
cout << "mergeSort func: err -1, NULL==source || NULL==dest || start > mid || mid > end " << endl;
ret = -1;
return ret;
}

int k = start;
int i = 0;
int j = 0;

for( i = start, j = mid + 1; i <= mid && j <= end; )
{
if( source[i] < source[j] )
{
dest[ k++ ] = source[ i++ ];
}
else
{
dest[ k++ ] = source[ j++ ];
}
}

while( i <= mid )
{
dest[ k++ ] = source[ i++ ];
}

while( j <= end )
{
dest[ k++ ] = source[ j++ ];
}

for( int i = start; i <= end; ++i )
{
source[i] = dest[i];
}

return ret;
}

// ref: 归并排序 百度百科
int mergeSort( int source[], int dest[], int startIndex, int endIndex )
{
int ret = 0;

if( NULL == source || NULL == dest )
{
cout << "mergeSort func: err -1, NULL==source || NULL==dest " << endl;
ret = -1;
return ret;
}

if( startIndex < endIndex )
{
int midIndex = ( startIndex + endIndex ) / 2;
mergeSort( source, dest, startIndex, midIndex );
mergeSort( source, dest, midIndex + 1, endIndex );
merge( source, dest, startIndex, midIndex, endIndex );
}

return ret;
}

// mergeSort只需O(n)空间，而combineSort需要nlog(n)的空间

int main()
{
int ret = 0;

int a[] = { 2, 3, 1, 4, 7, 6, 5 };
int n = 0;

while( cin >> n )
{
int *p = new int[n];
int *p1 = new int[n];

for( int i = 0; i < n; ++i )
{
int data = 0;
cin >> p[i];

}

//combineSort( p, 0, n - 1 );
mergeSort( p, p1, 0, n - 1 );
cout << "after sort: " << endl;

for( int i = 0; i < n; i++ )
{
cout << p[i] << ' ';
}

cout << endl;

delete []p;
p = NULL;

}

return ret;
}</span></span>

#### 归并排序 sort 计算机排序算法

2009年12月17日 876KB 下载

#### 我对归并排序的理解

2016-03-08 14:47:22

#### 归并排序全解（含复杂度证明）

2016-08-08 12:32:13

#### 归并排序及其应用

2009-02-12 16:22:00

#### 归并排序c＋＋代码及详细注释

2014-12-11 20:03:27

#### 堆排序以及归并排序的理解

2016-12-02 16:44:48

#### 数据结构之---C语言实现归并排序

2015-09-08 15:32:19

#### 归并排序实现外排序

2016-10-29 15:16:15

#### 希尔排序和归并排序

2015-10-30 23:12:46

#### 排序之归并排序和快速排序

2017-04-02 16:34:08