1. The efficiency of handwriting fast-line is 10 times times of sort in debug
2. Under Releasesort a little faster than a handwritten quick row(significantly improved compared to debug)3, Stable_sort in the debug than sort faster than the sort slow reason unknown. (The release is twice times faster than the debug)
1 #pragmaOnce2#include"stdafx.h"3#include <vector>4Template<typename comparable>5 voidInsertsort (std::vector<comparable>& V,intBeginintend)6 {7 for(inti=begin+1; I! = end +1; i++)8 {9 intJ;TenComparable TMP =V[i]; One for(j=i; J >0&& tmp < v[j-1]; j--) A { - //move the larger number back one -V[J] = v[j-1]; the } - //Unlike the last J in the Loop body -V[J] =tmp; - } + } - + //three-digit sort, and place the median at the end A //used to select pivot values atTemplate<typename comparable> - Constcomparable& Median3 (std::vector<comparable>& V,intLeftintRight ) - { - intCenter = (left + right)/2; - if(V[center] <V[left]) - swap (V[center], v[left]); in if(V[right] <V[left]) - swap (V[right], v[left]); to if(V[right] <V[center]) + swap (V[right], v[center]); -Swap (V[center], v[right-1]); the returnv[right-1]; * } $Template<typename comparable>Panax Notoginseng voidQuickSort (std::vector<comparable>& V,intLeftintRight ) - { the if(Left +Ten<=Right ) + { AComparable pivot =median3 (V, left, right); the inti = Left,j = right-1; + while(true) - { $ while(V[++i] <pivot) $ ; - while(V[--j] >pivot) - ; the if(I <j) - {Wuyi swap (V[i], v[j]); the } - Else Wu { - Break; About } $ } -Swap (v[i],v[right-1]); -QuickSort (V, left, I-1); -QuickSort (V, i+1, right); A } + Else the { - Insertsort (V, left, right); $ } the}
Fast Line Implementation
1 //QuickSort.cpp:Defines the entry point for the console application.2 //3#include"stdafx.h"4#include"QuickSort.h"5#include <vector>6#include <iostream>7#include <time.h>8#include <algorithm>9 using namespacestd;Ten #defineMax_rand 829346 One int_tmain (intARGC, _tchar*argv[]) A { -vector<Long>v; - clock_t c_s,c_e; the for(inti =0; I <2000000; i++) - { -V.push_back (Long((Double) rand ()/rand_max *Max_rand)); - } +vector<Long>v1 (v), V2 (v), V3 (v); -c_s =clock (); +QuickSort (v1,0, V1.size ()-1); AC_e =clock (); atcout<<"QuickSort:"<<c_e-c_s<<Endl; -c_s =clock (); - sort (V3.begin (), V3.end ()); -C_e =clock (); -cout<<"Sort:"<<c_e-c_s<<Endl; - return 0; in}
Run Time Test
The results of the operation are as follows:Debug under: quicksort:6562sort:67766 Release: quicksort:218sort:157 code download
Http://pan.baidu.com/s/1jHqqxnS
1. Quick Sort