count, the base of Chinese pronunciation are the same, the translation of the people still think our computer is not messy, really want to spit Groove.
regardless, The code is not the same after All.
1. Sort by Count (counter sort):
By using an upper limit to count the values in the set (or other values mapped by Non-numeric types) and accumulating the number of statistics that are smaller than the number of their own (including), the index of the sort is formed (that is, How many of these are smaller than mine, and my position is ok).
Normal count sort code: (still need to optimize, valuetype default is integer Type)
1Template<typename _init>2 voidCounter_sort (_init first, _init last,intk, _init Result) {3typedef TypeName Iterator_traits<_init>:: Value_type _valuetype;4 5Vector<_valuetype> v (k, _valuetype (0));6 for(_init it=first; it!=last; ++it) v[*it]++;7 8TypeName Vector<_valuetype>::iterator ITX = V.begin () +1;9 for(; itx!=v.end (); ++itx) (*itx) + = * (itx-1);Ten one for(inti = last-first-1; i>=0; i--) { a_valuetype val = * (first+i); -* (result + v[val]-1) =val; -v[val]--; the } -}
2. Base sort (radix sort)
Base sorting is easy to understand, and there are illustrations in the introduction to Algorithms. But see an article about ACM that mentions the sort of MSD (most significant dight) and LSD (Least significant dight) ,
That is, starting from There. high efficiency, as an example of an integer, 3 digits, if each number of the highest bit is not repeated, it must be from the highest position of the value of the row in Order. But in fact, too many may not be the case, so this can only be said with the big end of the same side, from there began to look at the specific Situation.
Here is the code that modifies the count order to match the number of digits in the value Change.
1Template<typename t>2 voidPrintConstvector<t>&V) {3 for(vector<int>::const_iterator It=v.begin (); it! = V.end (); it++)4cout << *it <<" ";5cout <<endl;6 }7 8 intDecimal_num (intNumintP) {9Assert (num >0&& p >0);Ten return(num% (int) Pow (Ten, p))/(int) Pow (Ten, p-1); one } a -Template<typename _init> - voidCounter_sort (_init first, _init last, _init result,intKintP) { thetypedef TypeName Iterator_traits<_init>:: Value_type _valuetype; - -Vector<_valuetype> v (k, _valuetype (0)); - for(_init it=first; it!=last; ++it) v[decimal_num (*it, p)]++; + -TypeName Vector<_valuetype>::iterator ITX = V.begin () +1; + for(; itx!=v.end (); ++itx) (*itx) + = * (itx-1); a at for(inti = last-first-1; i>=0; i--) { -_valuetype val = * (first+i); -_valuetype idx =decimal_num (val, p); -* (result + v[idx]-1) =val; -v[idx]--; - } in } - toTemplate<typename _init> + voidRadix_sort (_init first, _init last, _init result,intP) { - for(intI=1; i<=p; i++) theCounter_sort (first, last, result,Ten, i); * } $ Panax Notoginseng intmain () { - intlst[] = {2,5,0,3,2,3,0,3}; thevector<int> v (lst, lst+8); +vector<int> V2 (v.size (),0); a theCounter_sort (v.begin (), v.end (),6, V2.begin ()); + Print (v2); - $ intlst2[] = {329,457,657,839,436,720,355}; $vector<int> V3 (lst2, lst2+7); -vector<int> v4 (v3.size (),0); - theRadix_sort (v3.begin (), v3.end (), v4.begin (),3); - Print (v4);Wuyi return 0; the}
Reference:
Http://www.acmerblog.com/radix-sorting-5601.html
Repeating wheel series--counting, base sorting