七大排序算法初步实现

Sorts.h

#ifndef __SORTS_H__
#define __SORTS_H__

//数据交换
void Swap(int& a, int& b);

//冒泡排序
void BubbleSort(int a[], int n);

//快速排序
void QuickSort(int a[], int n);
void QuickSortPartition1(int a[], int beg, int end);
void QuickSortPartition2(int a[], int beg, int end);

//插入排序
void InsertSort(int a[], int n);

//shell排序
void ShellSort(int a[], int n);

//归并排序
void MergeSort(int a[], int n);
void MergeSortStep(int a[], int beg, int end, int temp[]);
void MergeArray(int a[], int beg, int middle, int end, int temp[]);

//基数排序
void RadixSort(int a[], int n);
int PreRadixSort(int a[], int n, int weight, int bits[]);
void RadixSortStep(int a[], int n, int bits[], int temp[]);

//堆排序
enum HeapType
{
	HeapType_SMALL = 0,
	HeapType_BIG = 1
};

typedef int HeapElement;
typedef struct _tagHeap
{
	HeapType type;
	int cursize;
	int maxsize;
	HeapElement a[];
} Heap;

void HeapSort(HeapElement a[], int n);
Heap* CreatHeap(HeapType type, int n);
void InsertEle2Heap(Heap* heap, HeapElement ele);
void DeleteHeapTop(Heap* heap);
void PopHeap(Heap* heap);
void KeepBalanceForInsert(Heap* heap, int index);
void KeepBalanceForDelete(Heap* heap, int index);

#endif

Sorts.cpp

#include "Sorts.h"
#include <stdio.h>
#include <malloc.h>
#include <string.h>

//数据交换
void Swap(int& a, int& b)
{
	if (a != b)
	{
		a ^= b;
		b ^= a;
		a ^= b;
	}
}

//冒泡排序
void BubbleSort(int a[], int n)
{
	for (int i = 0; i < n; i++)
	{
		int k = 0;
		for (int j = 0; j < n - i - 1; j++)
		{
			if (a[j] > a[j + 1])
			{
				Swap(a[j], a[j + 1]);
				k = 0;
			}
			else
			{
				k++;
			}
		}
		i += k;
	}
}

//快速排序
void QuickSort(int a[], int n)
{
	QuickSortPartition2(a, 0, n - 1);
}

void QuickSortPartition1(int a[], int beg, int end)
{
	if (beg < end)
	{
		int i = beg - 1;
		int j = beg;
		int x = a[end];
		for (; j < end; j++)
		{
			if (a[j] < x)
			{
				i += 1;
				if (i != j)
				{
					Swap(a[i], a[j]);
				}
			}
		}
		Swap(a[++i], a[end]);
		QuickSortPartition1(a, beg, i - 1);
		QuickSortPartition1(a, i + 1, end);
	}
}

void QuickSortPartition2(int a[], int beg, int end)
{
	if (beg < end)
	{
		int x = a[beg];
		int i = beg;
		int j = end;
		while (i < j)
		{
			while (i < j)
			{
				if (a[j] <= x)
				{
					a[i++] = a[j];
					break;
				}
				j--;
			}

			while (i < j)
			{
				if (a[i] > x)
				{
					a[j--] = a[i];
					break;
				}
				i++;
			}
		}
		a[i] = x;

		QuickSortPartition2(a, beg, i - 1);
		QuickSortPartition2(a, i + 1, end);
	}
}

//插入排序
void InsertSort(int a[], int n)
{
	for (int i = 1; i < n; i++)
	{
		int x = a[i];
		int j = i - 1;
		for (; j >= 0; j--)
		{
			if (a[j] > x)
			{
				a[j + 1] = a[j];
			}
			else
				break;
		}
		a[j + 1] = x;
	}
}

//shell排序
void ShellSort(int a[], int n)
{
	for (int g = n / 2; g >= 1; g /= 2)
	{
		for (int i = 0; i < g; i++)
		{
			for (int j = i + g; j < n; j += g)
			{
				int x = a[j];
				int k = j - g;
				for (; k >= 0; k -= g)
				{
					if (a[k] > x)
					{
						a[k + g] = a[k];
					}
					else
					{
						break;
					}
				}
				a[k + g] = x;
			}
		}
	}
}

//归并排序
void MergeSort(int a[], int n)
{
	int *pTemp = new int[n];
	if (!pTemp)
		return;
	MergeSortStep(a, 0, n - 1, pTemp);
	delete(pTemp);
}

void MergeSortStep(int a[], int beg, int end, int temp[])
{
	if (beg < end)
	{
		int middle = (beg + end) / 2;
		MergeSortStep(a, beg, middle, temp);
		MergeSortStep(a, middle + 1, end, temp);
		MergeArray(a, beg, middle, end, temp);
	}
}

void MergeArray(int a[], int beg, int middle, int end, int temp[])
{
	int i = beg;
	int j = middle + 1;
	int k = 0;
	while (i <= middle && j <= end)
	{
		if (a[i] <= a[j])
			temp[k++] = a[i++];
		else
			temp[k++] = a[j++];
	}

	while (i <= middle)
		temp[k++] = a[i++];
	while (j <= end)
		temp[k++] = a[j++];

	for (int i = 0; i < k; i++)
		a[beg + i] = temp[i];
}

//基数排序
void RadixSort(int a[], int n)
{
	int* pBits = new int[1];
	if (!pBits)
		return;
	int* pTemp = new int[n];
	if (!pTemp)
	{
		delete pBits;
		return;
	}
	int weight = 1;
	
	while (1)
	{
		int count = PreRadixSort(a, n, weight, pBits);
		if (0 == count)
		{
			delete pBits;
			delete pTemp;
			return;
		}
		RadixSortStep(a, n, pBits, pTemp);
		weight++;
	}
}

int PreRadixSort(int a[], int n, int weight, int bits[])
{
	int value = 1;
	for (int i = 0; i < weight; i++)
		value *= 10;

	int ret = 0;
	for (int i = 0; i < n; i++)
	{
		if (a[i] / (value / 10) != 0 && !ret)
			ret = 1;
		bits[i] = a[i] % value / (value / 10);
	}
	return ret;
}

void RadixSortStep(int a[], int n, int bits[], int temp[])
{
	int k = 0;
	for (int i = -9; i < 10; i++)
	{
		for (int j = 0; j < n; j++)
		{
			if (bits[j] == i)
				temp[k++] = a[j];
		}
	}

	for (int i = 0; i < n; i++)
		a[i] = temp[i];
}

//堆排序
void HeapSort(HeapElement a[], int n)
{
	Heap *heap = CreatHeap(HeapType_SMALL, 1);
	if (heap == NULL)
	{
		return;
	}
	for (int i = 0; i < n; i++)
	{
		InsertEle2Heap(heap, a[i]);
	}
	//for (int i = 0; i < heap->cursize; i++)
	//	printf("%d ", heap->a[i]);
	//printf("\n");
	PopHeap(heap);
	free(heap);
}

Heap* CreatHeap(HeapType type, int n)
{
	Heap *heap = (Heap*)malloc(sizeof(Heap)+ n * sizeof(HeapElement));
	if (!heap)
		return NULL;
	heap->type = type;
	heap->cursize = 0;
	heap->maxsize = n;
	memset(heap->a, 0, n * sizeof(HeapElement));
	return heap;
}

void InsertEle2Heap(Heap* heap, HeapElement ele)
{
	if (!heap)
		return;
	if (heap->cursize == heap->maxsize)
	{
		heap = (Heap*)realloc(heap, sizeof(Heap)+ heap->maxsize * 2 * sizeof(HeapElement));
		heap->maxsize = heap->maxsize * 2;
	}
	heap->a[heap->cursize++] = ele;
	KeepBalanceForInsert(heap, heap->cursize - 1);
}

void DeleteHeapTop(Heap* heap)
{
	if (heap->cursize > 0)
	{
		heap->a[0] = heap->a[heap->cursize - 1];
		heap->cursize--;
		KeepBalanceForDelete(heap, 0);
	}
}

void PopHeap(Heap* heap)
{
	while (heap->cursize > 0)
	{
		printf("%d ", heap->a[0]);
		DeleteHeapTop(heap);
	}
	printf("\n");
}

void KeepBalanceForInsert(Heap* heap, int index)
{
	if (!heap)
		return;
	int parentIndex = (index - 1) / 2;
	if (parentIndex >= 0)
	{
		if (HeapType_SMALL == heap->type)
		{

			if (heap->a[index] < heap->a[parentIndex])
			{
				Swap(heap->a[index], heap->a[parentIndex]);
				KeepBalanceForInsert(heap, parentIndex);
			}
		}
		else
		{
			if (heap->a[index] > heap->a[parentIndex])
			{
				Swap(heap->a[index], heap->a[parentIndex]);
				KeepBalanceForInsert(heap, parentIndex);
			}
		}
	}
}

void KeepBalanceForDelete(Heap* heap, int index)
{
	int lindex = index * 2 + 1;
	int rindex = index * 2 + 2;
	if (lindex > heap->cursize - 1)
		return;
	if (rindex > heap->cursize - 1)
	{
		if (heap->type == HeapType_SMALL && heap->a[index] > heap->a[lindex])
			Swap(heap->a[index], heap->a[lindex]);
		else if (heap->type == HeapType_BIG && heap->a[index] < heap->a[lindex])
			Swap(heap->a[index], heap->a[lindex]);
		return;
	}
	else
	{
		if (heap->type == HeapType_SMALL)
		{
			int exchangeIndex = lindex;
			if (heap->a[rindex] < heap->a[lindex])
			{
				exchangeIndex = rindex;
			}
			if (heap->a[index] > heap->a[exchangeIndex])
			{
				Swap(heap->a[index], heap->a[exchangeIndex]);
				KeepBalanceForDelete(heap, exchangeIndex);
			}
		}
		else
		{
			int exchangeIndex = lindex;
			if (heap->a[rindex] > heap->a[lindex])
			{
				exchangeIndex = rindex;
			}
			if (heap->a[index] < heap->a[exchangeIndex])
			{
				Swap(heap->a[index], heap->a[exchangeIndex]);
				KeepBalanceForDelete(heap, exchangeIndex);
			}
		}
	}
}


你可能感兴趣的:(七大排序算法初步实现)