关闭

归并排序

标签: 排序
210人阅读 评论(0) 收藏 举报
分类:
<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>

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:564256次
    • 积分:4774
    • 等级:
    • 排名:第6282名
    • 原创:94篇
    • 转载:115篇
    • 译文:0篇
    • 评论:28条
    最新评论