Algorithms: 排序

/*--------------------------------------------------------------------------
 * Project: Sort.h
 * Name: zwp
 * Date: 2014/3
 *--------------------------------------------------------------------------*/



#ifndef SORT_H_
#define SORT_H_

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>


#define LeftChild(i)	(2 * (i) + 1)


typedef int ElementType;


/*
** 交换元素
*/
static void Swap(ElementType* num1, ElementType* num2)
{
	ElementType tmp = *num1;
	*num1 = *num2;
	*num2 = tmp;
}


/*
** 插入排序
*/
void InsertionSort(ElementType A[], int N)
{
	int j, p;

	ElementType Tmp;
	for(p = 1; p < N; ++ p)
	{
		Tmp = A[p];
		/* 前一个元素大于后一个元素 */
		for(j = p; j > 0 && A[j-1] > Tmp; j --)
			A[j] = A[j-1];	
		A[j] = Tmp;
				
	}

}


/*
** 希尔排序
*/
void ShellSort(ElementType A[], int N)
{
	int i, j, Increment;
	ElementType Tmp;

	/* 第一个循环为增量 */
	for(Increment = N/2; Increment > 0; Increment /= 2)
		for(i = Increment; i < N; i ++)
		{
			Tmp = A[i];

			for(j = i; j >= Increment;  j -= Increment)
				if(Tmp < A[j-Increment])
					A[j] = A[j - Increment];	
				else
					break;

			A[j] = Tmp;

		}
}


/*
** 下调函数
*/
void PrceDown(ElementType A[], int i, int N)
{
	int Child;
	ElementType Tmp;

	for(Tmp = A[i]; LeftChild(i) < N; i = Child)
	{
		Child = LeftChild(i);

		if(Child != N - 1 && A[Child+1] > A[Child])
			Child++;
		if(Tmp < A[Child])
			A[i] = A[Child];
		else
			break;

	}
	A[i] = Tmp;

}

/*
** 堆排序
*/
void HeapSort(ElementType A[], int N)
{
	int i;

	for(i = N/2; i >= 0; i --)
		PrceDown(A, i, N);			/* Build heap */

	for(i = N - 1; i > 0; i --)
	{
		Swap(&A[0], &A[i]);			/* Delete Max */
		PrceDown(A, 0, i);
	}									
}


/*
** Merge
** Lpos = start of left half, Rpos = start of ight half
*/
void Merge(ElementType A[], ElementType TmpArray[], int Lpos, int Rpos, int RightEnd)
{
	int i, LeftEnd, NumElements, TmpPos;

	LeftEnd = Rpos - 1;
	TmpPos = Lpos;
	NumElements = RightEnd - Lpos + 1;

	while(Lpos <= LeftEnd && Rpos <= RightEnd)
	{
		/* left <= Right copy left */
		if(A[Lpos] <= A[Rpos])
			TmpArray[TmpPos++] = A[Lpos++];
		/* left > Right copy right */
		else
			TmpArray[TmpPos++] = A[Rpos++];
	}
	/* Copy reset of first half */ 
	while(Lpos <= LeftEnd)
		TmpArray[TmpPos++] = A[Lpos++];
	/* Copy  reset of second half */
	while(Rpos <= RightEnd)
		TmpArray[TmpPos++] = A[Rpos++];

	/* Copy TmpArray Back */
	for(i = 0; i < NumElements; i ++, RightEnd--)
		A[RightEnd] = TmpArray[RightEnd];

}

void MSort(ElementType A[], ElementType TmpArray[], int Left, int Right)
{
	int Center;

	if(Left < Right)
	{
		Center = (Left + Right) / 2;
		MSort(A, TmpArray, Left, Center);			/* left to center */
		MSort(A, TmpArray, Center+1, Right);		/* center to right */
		Merge(A, TmpArray, Left, Center+1, Right);  /* Merge		   */

	}

}

/*
** 归并排序
*/
void MergeSort(ElementType A[], int N)
{
	ElementType* TmpArray;
	TmpArray = (ElementType*)malloc(sizeof(ElementType) * N);

	if(TmpArray != NULL)
	{
		MSort(A, TmpArray, 0, N-1);
		free(TmpArray);
	}
	else
		printf("No space for TmpArray...\n");

}


/*
** 返回枢纽元素
*/
ElementType Median3(ElementType A[], int Left, int Right)
{
	int Center = (Left + Right) / 2;

	if(A[Left] > A[Center])
		Swap(&A[Left], &A[Center]);
	if(A[Left] > A[Right])
		Swap(&A[Left], &A[Right]);
	if(A[Center] > A[Right])
		Swap(&A[Center], &A[Right]);

	/* A[Left] <= A[Center] <= A[Right] */
	Swap(&A[Center], &A[Right-1]);

	return (A[Right-1]);
}


#define Cutoff (3)

void QSort(ElementType A[], int Left, int Right)
{
	int i, j;
	ElementType Pivot;

	if(Left + Cutoff <= Right)
	{
		Pivot = Median3(A, Left, Right);
		i = Left; 
		j = Right - 1;
		for(;  ;)
		{
			while(A[++i] < Pivot) { }
			while(A[--j] > Pivot) { }
			if(i < j)
				Swap(&A[i], &A[j]);
			else
				break;

		}

		Swap(&A[i], &A[Right-1]);
		QSort(A, Left, i - 1);
		QSort(A, i + 1, Right);



	}
	else /*  do an insertion sort for subarray */
		InsertionSort(A+Left, Right - Left - 1);

}





#endif
/*-----------------------------------------------------------------------------------------------
 * Project: Main.cpp
 * Name: zwp
 * Date: 2014.3
 *------------------------------------------------------------------------------------------------*/



#include "Sort.h"


static void display(ElementType* A, int N)
{
	int i = 0;
	for(i = 0; i < N; ++ i)
		printf("  %d  ", A[i]);

	printf("\n");
}

int main(int argc, char* argv[])
{

	ElementType A[] = {
		2, 4, 9, 12, 1, 15, 100, 7
	};

	int N = sizeof(A)/sizeof(A[0]);

	display(A, N);
	//InsertionSort(A, N);
	QSort(A, 1, N-1);
	//MergeSort(A, N);
	display(A, N);

	
	system("pause");
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值