1、003_sort_merge.h
/***************************************************************
*版权所有 (C)2014,长沙铁信交通科技有限公司。
*
*文件名称:003_sort_merge.h
*内容摘要:实现归并排序
*其它说明:
*当前版本:V1.0
*作 者:伍定湘
*完成日期:2014年9月25日
*
*修改记录1:
* 修改日期:2014年9月25日
* 版本号:V1.0
* 修改人:伍定湘
* 修改内容:创建
***************************************************************/
#ifndef _SORT_MERGE_H_ //防止头文件被重复引用
#define _SORT_MERGE_H_
/**************************************************************
头文件引用
**************************************************************/
#include "typedef.h"//引入内置类型重定义
/**************************************************************
相关宏定义
**************************************************************/
/**************************************************************
相关结构体定义
**************************************************************/
/**************************************************************
本程序中出现的函数的声明
**************************************************************/
void sort_merge(INT32 aUnsorted[], const INT32 iArrLen);
void merge(INT32 aUnsorted[], const INT32 iOffsetAL, const INT32 iOffsetAR, const INT32 iOffsetAE);
#endif
2、003_sort_merge.c
/***************************************************************
*版权所有 (C)2014,长沙铁信交通科技有限公司。
*
*文件名称:003_sort_merge.c
*内容摘要:实现归并排序
*其它说明:
*当前版本:V1.0
*作 者:伍定湘
*完成日期:2014年9月25日
*
*修改记录1:
* 修改日期:2014年9月25日
* 版本号:V1.0
* 修改人:伍定湘
* 修改内容:创建
***************************************************************/
/**************************************************************
头文件引用
**************************************************************/
#include "typedef.h"//引入内置类型重定义
#include "003_sort_merge.h"//引入内置类型重定义
#include <stdio.h>
#include <stdlib.h>
/**************************************************************
全局变量定义
**************************************************************/
/**************************************************************
函数实现
**************************************************************/
/**********************************************************************
*功能描述:实现选择排序
*输入参数:aUnsorted - 尚未排序的数组
* iArrLen - 尚未排序的数组的长度
*输出参数:
*返回值:
*其它说明:
*修改日期 版本号 修改人 修改内容
* ---------------------------------------------------------------------
*2014年9月25日 V1.0 伍定湘 创建
***********************************************************************/
void sort_merge(INT32 aUnsorted[], const INT32 iArrLen)
{
INT32 iLoopCount = 0;
INT32 iOffsetAL;
for (; iLoopCount < 3; iLoopCount++)
{
iOffsetAL = 0;
for (; iOffsetAL < iArrLen; iOffsetAL += (2 << iLoopCount))//2*2->4
{
merge(aUnsorted, iOffsetAL, iOffsetAL + (1 << iLoopCount), iOffsetAL + (2 << iLoopCount) - 1);
}
}
}
/**********************************************************************
*功能描述:实现已排序子数组合并操作
*输入参数:aUnsorted - 尚未排序的数组
* iOffsetAL - 需要合并的部分左起点
* iOffsetAR - 需要合并的部分右起点
* iOffsetAE - 需要合并的部分终点
*输出参数:
*返回值:
*其它说明:aUnsorted[iOffsetAL..(iOffsetAR - 1)] 和 aUnsorted[iOffsetAR...iOffsetAE] 已分别排序
*修改日期 版本号 修改人 修改内容
* ---------------------------------------------------------------------
*2014年9月25日 V1.0 伍定湘 创建
***********************************************************************/
void merge(INT32 aUnsorted[], const INT32 iOffsetAL, const INT32 iOffsetAR, const INT32 iOffsetAE)
{
const INT32 iLenL = iOffsetAR - iOffsetAL;
const INT32 iLenR = iOffsetAE - iOffsetAR + 1;
INT32 *pL = (INT32 *)malloc(iLenL * sizeof(INT32));
INT32 *pR = (INT32 *)malloc(iLenR * sizeof(INT32));
INT32 iOffsetL = 0;
INT32 iOffsetR = 0;
INT32 iOffsetA = iOffsetAL;
for (; iOffsetL < iLenL; iOffsetL++)
{
pL[iOffsetL] = aUnsorted[iOffsetAL + iOffsetL];
}
for (; iOffsetR < iLenR; iOffsetR++)
{
pR[iOffsetR] = aUnsorted[iOffsetAR + iOffsetR];
}
iOffsetL = 0;
iOffsetR = 0;
for (; iOffsetA <= iOffsetAE; iOffsetA++)
{
if (iOffsetL >= iLenL)//左数组取完,右数组未取完
{
aUnsorted[iOffsetA] = pR[iOffsetR];
iOffsetR++;
continue;
}
if (iOffsetR >= iLenR)//右数组取完,左数组未取完
{
aUnsorted[iOffsetA] = pL[iOffsetL];
iOffsetL++;
continue;
}
if (pL[iOffsetL] < pR[iOffsetR])//左数组值小于右数组值,取左数组值
{
aUnsorted[iOffsetA] = pL[iOffsetL];
iOffsetL++;
}
else//取右数组值
{
aUnsorted[iOffsetA] = pR[iOffsetR];
iOffsetR++;
}
}
free(pL);
free(pR);
}