9种排序算法——堆排序,归并排序,插入排序,选择排序

共实现了9中排序算法,包括直接插入排序,简单插入排序,折半排序,二路排序算法,快速排序,简单选择排序,归并排序,堆排序,还有冒泡排序法。

其中所有的排序算法都经过了测试,可以保证准确性。

头文件如下

/*********************************************************************************
*FileName:SortCollection
*Author:  fmx
*Version:  1.0
*Date:  2014-06-18
*Description: 实现了直接插入排序,简单插入排序,折半排序,二路排序算法,快速排序,简单选择排序,归并排序,堆排序,还有冒泡排序法共九种排序法。
**********************************************************************************/

#pragma once

#define SWAP(X,Y) int tmp=X;X=Y;Y=tmp;


#define LT(X,Y) X<Y

class SortCollection
{
private:
	void heap(int*src, int n);
	void headAdjust(int a[], int i, int m);
	void mergeProcess(int a[], int p, int r);
	void merge(int *num, int start, int middle, int end);
	int partion(int *Src, int low, int high);
	void QSort(int *Src, int low, int high);
public:
	SortCollection();
	~SortCollection();

	int* directSort(int* src, int n);

	int* BInsertSort(int* Src, int n);
	int* twowaySort(int* Src, int n);
	int* shellSort(int* Src, int n);
	int* quickSort(int* Src, int n);
	int* simchoSort(int* Src, int n);
	int* heapSort(int* Src, int n);
	int* mergeSort(int* Src, int n);
	int* stdSort(int* Src, int n);
	int* bubble(int* src, int n);
};

实现文件如下

#include "SortCollection.h"
SortCollection::SortCollection()
{
}


SortCollection::~SortCollection()
{
}


int* SortCollection::directSort(int* src, int n)
{
	int* pS = new int[n];
	memcpy(pS, src, n*sizeof(int));
	int i, j;
	int temp;
	for (i = 1; i < n; i++)
	{
		temp = pS[i];
		j = i - 1;
		while (j >= 0 && temp < pS[j])
		{
			pS[j + 1] = pS[j--];
		}
		pS[j + 1] = temp;
	}
	return pS;
}


int* SortCollection::BInsertSort(int* Src, int n)
{
	int* pS = new int[n];
	memcpy(pS, Src, n*sizeof(int));

	int low, high, i, j, t;
	int m;
	for (i = 2; i <= n; ++i)
	{
		t = pS[i];//临时存放
		low = 1; high = i - 1;
		while (low <= high)
		{
			m = (low + high) / 2;
			if (t < pS[m])
				high = m - 1;
			else
				low = m + 1;
		}
		for (j = i - 1; j >= high + 1; --j)

		{
			pS[j + 1] = pS[j];
		}

		pS[high + 1] = t;
	}
	return pS;
}
int* SortCollection::twowaySort(int* Src, int n)
{
	int *ps = new int[5000];
	memcpy(ps, Src, 5000 * sizeof(int));
	int temp[5000];
	memcpy(temp, Src, 5000 * sizeof(int));
	int i, first, final = first = 0;

	temp[0] = ps[0];
	for (i = 1; i < n; ++i) {
		if (temp[final] <= ps[i]) {
			temp[++final] = ps[i];
		}
		else if (ps[i] <= temp[first]) {
			first = (first - 1 + n) % n;
			temp[first] = ps[i];
		}
		else {
			size_t index;

			for (index = (final - 1 + n) % n;;
				index = (index - 1 + n) % n) {
				if (temp[index] <= ps[i]) {
					size_t mid = final++;
					while (mid != index) {
						temp[(mid + 1) % n] = temp[mid];
						mid = (mid - 1 + n) % n;
					}
					temp[(index + 1) % n] = ps[i];

					break;
				}
			}
		}
	}

	for (i = 0; i < n; ++i) {
		ps[i] = temp[first];
		first = (first + 1) % n;
	}
	return ps;
}

int* SortCollection::shellSort(int* Src, int n)
{
	int* pS = new int[n];
	memcpy(pS, Src, n*sizeof(int));
	int i, j, k, temp;
	for (i = n / 2; i > 0; i /= 2)
	{
		for (j = i; j < n; j++)
		{
			temp = pS[j];
			for (k = j - i; k >= 0 && temp < pS[k]; k -= i)
			{
				pS[k + i] = pS[k];
			}
			pS[k + i] = temp;
		}
	}

	return pS;
}
int* SortCollection::bubble(int*Src, int n)
{
	int* pS = new int[n];
	memcpy(pS, Src, n*sizeof(int));
	int tmp = 0;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			if (pS[j]>pS[j + 1])
			{
				tmp = pS[j];
				pS[j] = pS[j + 1];
				pS[j + 1] = tmp;
			}
		}
	}

	return pS;
}

int* SortCollection::quickSort(int* Src, int n)
{
	int* pS = new int[n];
	memcpy(pS, Src, n*sizeof(int));

	QSort(pS, 0, 5000);

	return pS;
}

int SortCollection::partion(int *Src, int low, int high)
{
	int prvotkey = Src[low];
	Src[0] = Src[low];
	while (low < high)
	{
		while (low < high&&Src[high] >= prvotkey)
			--high;
		Src[low] = Src[high];
		while (low < high&&Src[low] <= prvotkey)
			++low;
		Src[high] = Src[low];
	}

	Src[low] = Src[0];
	return low;
}

void SortCollection::QSort(int *Src, int low, int high)
{
	int pivot = 0;
	if (low < high)
	{
		pivot = partion(Src, low, high);
		QSort(Src, low, pivot - 1);
		QSort(Src, pivot + 1, high);
	}

}

int* SortCollection::simchoSort(int* Src, int n)
{
	int* pS = new int[n];
	memcpy(pS, Src, n*sizeof(int));

	int min = 0;
	int pos = 0;
	for (int i = 0; i < n; i++)
	{
		min = pS[i];
		for (int j = i; j < n; j++)
		{
			min = min < pS[j] ? min : pS[j];
		}
		for (int m = i; m < n; m++)
		{
			if (min == pS[m])
			{
				pos = m;
				break;
			}
		}
		if (pS[i] != min)
		{
			SWAP(pS[i], min);
		}
	}

	return pS;
}



int* SortCollection::heapSort(int* Src, int n)
{
	int* pS = new int[n];
	memcpy(pS, Src, n*sizeof(int));

	heap(pS, n);

	return pS;
}

void SortCollection::heap(int*src, int n)
{
	int i, k;

	for (i = n / 2 - 1; i >= 0; i--) headAdjust(src, i, n);
	for (i = n - 1; i >= 1; i--)
	{
		k = src[0]; src[0] = src[i]; src[i] = k;
		headAdjust(src, 0, i);
	}
}

void SortCollection::headAdjust(int a[], int i, int m)
{
	int k, t;

	t = a[i]; k = 2 * i + 1;
	while (k < m)
	{
		if ((k < m - 1) && (a[k] < a[k + 1])) k++;
		if (t < a[k])
		{
			a[i] = a[k]; i = k; k = 2 * i + 1;
		}
		else break;
	}
	a[i] = t;
}

int* SortCollection::mergeSort(int* Src, int n)
{
	int* pS = new int[n];
	memcpy(pS, Src, n*sizeof(int));
	mergeProcess(pS, 0, n - 1);
	return pS;
}

void SortCollection::mergeProcess(int *num, int start, int end)
{
	int middle;
	if (start < end)
	{
		middle = (start + end) / 2;
		mergeProcess(num, start, middle);
		mergeProcess(num, middle + 1, end);
		merge(num, start, middle, end);
	}
}
void SortCollection::merge(int *num, int start, int middle, int end)
{
	int n1 = middle - start + 1;
	int n2 = end - middle;
	int *L = new int[n1 + 1];
	int *R = new int[n2 + 1];
	int i, j = 0, k;

	for (i = 0; i < n1; i++)
	{
		*(L + i) = *(num + start + i);
	}
	*(L + n1) = 1000000;
	for (i = 0; i < n2; i++)
	{
		*(R + i) = *(num + middle + i + 1);
	}
	*(R + n2) = 1000000;
	i = 0;
	for (k = start; k <= end; k++)
	{
		if (L[i] <= R[j])
		{
			num[k] = L[i];
			i++;
		}
		else
		{
			num[k] = R[j];
			j++;
		}
	}
	delete[] L;
	delete[] R;
}


int* SortCollection::stdSort(int* Src, int n)
{
	int* pS = new int[n];
	memcpy(pS, Src, n*sizeof(int));
	std::sort(pS, pS + n);
	return pS;
}


不过有点很遗憾,性能方面没有优化,改天用OpenMP给这个算法来一次并行化,估计可以加快些。运行测试如下



柱状图的高度是算法运行时间。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值