各种排序GP版

51 篇文章 0 订阅

   几种排序,用了很多STL函数.贴了.

// sort.cpp
//	2011-10-09-21.41 -- 2011-10-09-21.55 -- insertionSort.
//	2011-10-13-06.25 -- 2011-10-13-06.46 -- bubbleSort.
//	2011-10-14-21.51 -- 2011-10-14-22.11 -- shellSort.
//	2011-10-15-10.13 -- 2011-10-15-11.40 -- mergeSort.
//	2011-10-15-12.09 -- 2011-10-15-12.18 -- selectionSort.
//	2011-10-15-15.20 -- 2011-10-15-22.57 -- quickSort.
//	2011-10-15-23.09 -- 2011-10-15-23.33 -- heapSort.
//	2011-10-16-08.21 -- 2011-10-16-09.37 -- countingSort.
#include "stdafx.h"
#include <iostream>
#include <algorithm>
#include <functional>

using std ::for_each ;
using std ::swap ;
using std ::merge ;
using std ::copy ;
using std ::partition ;
using std ::less ;
using std ::make_heap ;
using std ::greater ;
using std ::swap ;
using std ::greater ;

template<class T>
class Print
{
public:
	void operator () (const T & t) const
	{
		std ::cout << t << " " ;
	}
} ;

template<typename T, size_t N>
void insertionSort (T (&input)[N]) ;

template<typename T, size_t N>
void bubbleSort (T (&input)[N]) ;

template<typename T, size_t N>
void shellSort (T (&input)[N]) ;

template<typename T>
void mergeSort (T * const pInput, T * const pTemp, size_t start, size_t end) ;

template<typename T, size_t N>
void selectionSort (T (&input)[N]) ;

template<typename T>
void quickSort (T * const pStart, T * const pEnd) ;

template<typename T, size_t N>
void heapSort (T (&input)[N]) ;

template<typename T, size_t N>
void countingSort (T (&input)[N]) ;

int _tmain(int argc, _TCHAR* argv[])
{
	int input[] = {8, 8, 11, 2, 3, 4, 5, 7, 6, 5, 1, 4, 2, 6, 7, 1, 0, 9, 0, 0, 5, 11, 11, 8} ;
	int * pTemp = new int[sizeof input / sizeof (int)] ;

	insertionSort(input) ;
	bubbleSort(input) ;
	shellSort(input) ;
	mergeSort(input, pTemp, 0,  sizeof input / sizeof (int)) ;
	selectionSort(input) ;
	quickSort(input, input + sizeof input / sizeof (int)) ;
	heapSort(input) ;
	countingSort(input) ;

	for_each(input, input + sizeof input / sizeof (int), Print<int> ()) ;

	std ::cin.get() ;

	return 0 ;
}

template<typename T, size_t N>
void insertionSort (T (&input)[N])
{
	size_t i = 1 ;
	while (i != N)
	{
		size_t j = i ;
		T temp = input[i] ;
		while (j != 0 && temp < input[j - 1])
		{
			input[j] = input[j - 1] ;
			--j ;
		}
		input[j] = temp ;
		++i ;
	}
}

template<typename T, size_t N>
void bubbleSort (T (&input)[N])
{
	bool hasSwaped = true ;
	size_t endOfInput = N ;
	while (hasSwaped && endOfInput != 1)
	{
		hasSwaped = false ;
		for (size_t i = 1; i  != endOfInput; ++i)
		{
			if (input[i - 1] > input[i])
			{
				swap(input[i - 1], input[i]) ;
				hasSwaped = true ;
			}
		}
		--endOfInput ;
	}
}

template<typename T, size_t N>
void shellSort (T (&input)[N])
{
	size_t increment = N / 2 ;
	while (increment != 0)
	{
		size_t i = increment ;
		while (i != N)
		{
			size_t j = i ;
			T temp = input[j] ;
			while (j != 0 && temp < input[j - increment])
			{
				input[j] = input[j - increment] ;
				j -= increment ;
			}
			input[j] = temp ;
			++i ;
		}
		increment /= 2 ;
	}
}

template<typename T>
void mergeSort (T * const pInput, T * const pTemp, size_t start, size_t end)
{
	if (start < end - 1)
	{
		size_t middle = (start + end) / 2 ;
		mergeSort(pInput, pTemp, start, middle) ;
		mergeSort(pInput, pTemp, middle, end) ;
		merge(
			pInput + start, pInput + middle,
			pInput + middle, pInput + end,
			pTemp + start) ;
		//	Copy back.
		copy(pTemp + start, pTemp + end, pInput + start) ;
	}
}

template<typename T, size_t N>
void selectionSort (T (&input)[N])
{
	size_t indexOfMinItem ;
	size_t i = 0 ;
	while (i != N)
	{
		indexOfMinItem = i ;
		size_t j = i ;
		while (j != N)
		{
			if (input[j] < input[indexOfMinItem])
				indexOfMinItem = j ;
			++j ;
		}
		swap(*(input + i),*( input + indexOfMinItem)) ;
		++i ;
	}
}

template<typename T>
void quickSort (T * const pStart, T * const pEnd)
{
	if (pStart != pEnd)
	{
		T * pivot = partition(pStart, pEnd, bind2nd(less<T> (), (*pStart))) ;
		quickSort(pStart, pivot) ;
		//	Avoid to call quickSort() for same range.
		//	If pivot is equal tp pStart, [pivot, end) is the whole range as the range before partitioning.
		if (pStart == pivot)
			quickSort(pivot + 1, pEnd) ;
		else
			quickSort(pivot, pEnd) ;
	}
}

template<typename T, size_t N>
void heapSort (T (&input)[N])
{
	//	Build a max heap.
	make_heap(input, input + N) ;
	size_t end = N ;
	while (end != 0)
	{
		swap(*input, *(input + end - 1)) ;
		--end ;
		make_heap(input, input + end) ;
	}
}

template<typename T, size_t N>
void countingSort (T (&input)[N])
{
	//	Get the max value within input.
	T * maxValue = min_element(input, input + N, greater<T> ()) ;
	size_t k = *maxValue ;
	//	Allocate memory for run the algorithm.
	//	Index from 0 to k.
	T * arrayForCounting = new T[k + 1]() ;
	if (NULL == arrayForCounting)
	{
		return ;
	}
	T * pOutput = new T[N]() ;
	if (NULL == pOutput)
	{
		delete []arrayForCounting ;
		return ;
	}
	//	Counting the number of each value within input.
	for (size_t i = 0; i != N; ++i)
	{
		++arrayForCounting[input[i]] ;
	}
	//	Counting the number how many values less than or equal to itself. 
	for (size_t i = 1; i != k + 1; ++i)
	{
		arrayForCounting[i] += arrayForCounting[i - 1] ;
	}
	//	Place the result into pOutput.
	for (size_t i = N - 1; i != -1; --i)
	{
		*(pOutput + arrayForCounting[input[i]] - 1) = input[i] ;
		--arrayForCounting[input[i]] ;
	}
	//	Copy the result from pOutput back to input.
	for (size_t i = 0; i != N; ++i)
	{
		input[i] = *(pOutput + i) ;
	}
	//	Free the memory.
	delete[] arrayForCounting ;
	delete[] pOutput ;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值