各种排序GP版

来源:互联网 发布:java案例精粹150例 编辑:程序博客网 时间:2024/05/15 20:51

   几种排序,用了很多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) ;elsequickSort(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 ;}
原创粉丝点击