UG二次开发常用排序算法 - 堆排序

/**************************************************
* 文件名称:  CArrayHeapSort.h
* 功能说明:  排序(从小到大)
* 作    者: 梅雷
**************************************************/
#pragma once
#include 
#include 

typedef CArray  CDoubleArray;

struct int_data_t
{
	int i;//位置
	int num;//数字
};
typedef CArray CIntDataArray;

struct double_data_t
{
	int i;//位置
	double num;//数字
};
typedef CArray CDoubleDataArray;

static void CUIntArrayDataHeapAdjust(CIntDataArray &H, int s, int length);
static void CUIntArrayDataBuildingHeap(CIntDataArray &H);
void CUIntArrayDataHeapSort(CIntDataArray &H);//堆排序,从小到大

static void CDoubleArrayDataHeapAdjust(CDoubleDataArray &H, int s, int length);
static void CDoubleArrayDataBuildingHeap(CDoubleDataArray &H);
void CDoubleArrayDataHeapSort(CDoubleDataArray &H);//堆排序,从小到大
/**************************************************
* 文件名称:  CUIntArrayDataArrayHeapSort.cpp
* 功能说明:  排序(从小到大)
* 作    者: 梅雷
**************************************************/
#include "CArrayHeapSort.h"

template  static void Swap(T &a, T &b)
{//两值交换
	T t;
	t = a; a = b; b = t;
}

static void CUIntArrayDataHeapAdjust(CIntDataArray &H, int s, int length)  
{  
	int child = 2*s+1;
	int_data_t temp;
	temp.i = H[s].i;
	temp.num = H[s].num;
	while (child < length) 
	{  
		if(child+1 < length && H[child].num < H[child+1].num) 
			++child; 
		if(H[s].num < H[child].num) 
		{
			H[s].i = H[child].i;
			H[s].num = H[child].num;
			s = child;
			child = 2*s+1;  
		}
		else
		{
			break;  
		}  
		H[s].i = temp.i;
		H[s].num = temp.num;
	}   
}  

static void CUIntArrayDataBuildingHeap(CIntDataArray &H)  
{   
	for (int i = ((int)H.GetSize() -1) / 2 ; i >= 0; --i)  
		CUIntArrayDataHeapAdjust(H, i, (int)H.GetSize());  
}  

void CUIntArrayDataHeapSort(CIntDataArray &H)  
{  
	if((int)H.GetSize() < 2)
		return;
	CUIntArrayDataBuildingHeap(H);  
	for (int i = (int)H.GetSize() - 1; i > 0; --i)  
	{
		Swap(H[i], H[0]);
		CUIntArrayDataHeapAdjust(H, 0, i);  
	}  
}   

/**************************************************
* 文件名称:  CDoubleArrayDataArrayHeapSort.cpp
* 功能说明:  排序(从小到大)
* 作    者: 梅雷
**************************************************/
#include "CArrayHeapSort.h"

template  static void Swap(T &a, T &b)
{//两值交换
	T t;
	t = a; a = b; b = t;
}

static void CDoubleArrayDataHeapAdjust(CDoubleDataArray &H, int s, int length)  
{  
	int child = 2*s+1;
	double_data_t temp;
	temp.i = H[s].i;
	temp.num = H[s].num;
	while (child < length) 
	{  
		if(child+1 < length && H[child].num < H[child+1].num) 
			++child; 
		if(H[s].num < H[child].num) 
		{
			H[s].i = H[child].i;
			H[s].num = H[child].num;
			s = child;
			child = 2*s+1;  
		}
		else
		{
			break;  
		}  
		H[s].i = temp.i;
		H[s].num = temp.num;
	}   
}  

static void CDoubleArrayDataBuildingHeap(CDoubleDataArray &H)  
{   
	for (int i = ((int)H.GetSize() -1) / 2 ; i >= 0; --i)  
		CDoubleArrayDataHeapAdjust(H, i, (int)H.GetSize());
}  

void CDoubleArrayDataHeapSort(CDoubleDataArray &H)  
{  
	if((int)H.GetSize() < 2)
		return;
	CDoubleArrayDataBuildingHeap(H);  
	for (int i = (int)H.GetSize() - 1; i > 0; --i)  
	{
		Swap(H[i], H[0]);
		CDoubleArrayDataHeapAdjust(H, 0, i);  
	}  
}   

你可能感兴趣的:(UG二次开发,堆排序,UG二次开发,梅雷)