I used many STL functions for sorting.
// 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 Qu Icksort (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, pin Put + 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 * partition = partition (pstart, pend, bind2nd (less <t> (), (* pstart); quicksort (pstart, restart ); // avoid to call quicksort () for same range. // If vertex is equal TP pstart, [begin, end) is the whole range as the range before partitioning. if (pstart = success) quicksort (cost + 1, pend); elsequicksort (cost, pend) ;}} template <typename t, size_t n> void heapsort (T (& input) [N]) {// build a max heap. ma Ke_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 ;}