Repeat Wheel Series-insert sort and merge sort

Source: Internet
Author: User

Embarrassed, the truth is very simple, practice is not easy.

Because programming languages and algorithms describe the data structure is not exactly the same, so the theory to practice there are some discrepancies.

The following example is an implementation without a Sentinel position:

1#include <iostream>2#include <vector>3#include <algorithm>4#include <cassert>5 6 using namespacestd;7 8Template<typename _init, TypeName _func>9 voidInsert_sort (_init First, _init last, _func&Func) {Tentypedef TypeName Iterator_traits<_init>:: Value_type _valuetype; One  A      for(_init it = first +1; It! = Last; ++it) { -_valuetype key = *it; -_init ite = IT-1; the          for(; (Ite-first) >=0&& Func (*ite, key); --ite) -* (ITE +1) = *ite; -* (ITE +1) =key; -     } + } -  +Template<typename _init, TypeName _func> A void__merge (_init First, _init last, _func&Func) { attypedef TypeName Iterator_traits<_init>:: Value_type _valuetype; -Vector<_valuetype>lv, RV; -  -     //if ((last-first) = = 1) return; //This line of code tortured me for a long time, re-wrote a C language version to regain confidence, and then went to a bath back found, hurriedly remove -size_t r = last-First ; -size_t q = Floor (r)/2); in  -Copy (First, first+q+1, Back_inserter (LV)); toCopy (first+q+1, last+1, Back_inserter (RV)); +  -TypeName Vector<_valuetype>::iterator lit =Lv.begin (); theTypeName Vector<_valuetype>::iterator rit =Rv.begin (); *  $      for(_init It=first; it!=last+1; ++it) {Panax Notoginseng         if(Func (*lit, *RIT)) { -             if(Lit! =Lv.end ()) { the*it = *lit; +lit++; A             } the}Else { +             if(RIT! =Rv.end ()) { -*it = *RIT; $rit++; $             } -         } -     } the } - WuyiTemplate<typename _init, TypeName _func> the void__merge_sort (_init First, _init last, _func&Func) { -     if(Last-first >0) { Wusize_t q = Floor ((last-first)/2); -__merge_sort (First, first+Q, Func); About__merge_sort (first+q+1, last, Func); $ __merge (First, last, Func); -     } - } -  ATemplate<typename _init, TypeName _func> + voidMerge_sort (_init First, _init last, _func&Func) { the__merge_sort (First, last-1, Func); - } $  theTemplate<typename t> the voidPrintConstvector<t>&v) { the      for(vector<int>::const_iterator It=v.begin (); It! = V.end (); it++) thecout << *it <<" "; -cout <<Endl; in } the  the intMain () { About     intLst[] = {1,5,2,6,3,7,4,8}; thevector<int> V (LST, lst+8); the  theless<int>l; + Insert_sort (V.begin (), V.end (), l); - print (v); the Bayi     intLst2[] = {2,4,5,7,1,2,3,6}; thevector<int> V2 (lst2, lst2+8); theless_equal<int>LQ; -  - Merge_sort (V2.begin (), V2.end (), LQ); the Print (v2); the  the     return 0; the}

Repeating wheel series-insert sort and merge sort

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.