Quick Sort function Template

Source: Internet
Author: User
Tags benchmark

During this time, I was obsessed with STL and made some research. Today I wrote a new quick sorting algorithm using the template function and posted the code to share it.

There are two versions. Version 2 can pass in the comparator and define the sorting rules by yourself.


Quick Sorting Algorithm ideas:
1) Select an element from the sequence as the benchmark;
2) Rearranging the sequence. All the elements smaller than the benchmark are on the left of the benchmark. The elements larger than the benchmark are on the right of the benchmark, and the elements equal to the benchmark are on any side. This process is called grouping;
3) recursively sort groups smaller than the benchmark and those larger than the benchmark respectively.

# Include <vector> # include <list> // print the function template <typename iterator> void print (iterator begin, iterator end) {While (begin! = END) cout <* begin ++ <''; cout <Endl;} // exchange function template <typename type> void my_swap (type &, type & B) {Type C = A; A = B; B = C;} // template of the quick sort function, Version 1 template <typename iterator> void my_sort (iterator begin, iterator end) {iterator P = begin; iterator last = end; // point to the end position -- last; For (iterator I = begin, j = last; I! = J;) {While (! (I = p | * P <* I) ++ I; if (I! = P) {my_swap (* P, * I); // The switch location, which cannot be assigned here, because the data type P = I;} while (! (J = p | * j <* p) -- J; If (J! = P) {my_swap (* P, * j); P = J ;}} iterator it = begin; ++ it; if (P! = Begin & P! = It) // recursive my_sort (begin, P); It = P; ++ it; if (it! = End & it! = Last) // recursive my_sort (it, end);} // the template of the quick sort function that is greater than the benchmark. Version 2 (the comparator is higher than the version, and the rest is the same) template <typename iterator, typename comparator> void my_sort (iterator begin, iterator end, comparator CMP) {iterator P = begin; iterator last = end; -- last; for (iterator I = begin, j = last; I! = J;) {While (! (I = p | CMP (* P, * I) ++ I; if (I! = P) {my_swap (* P, * I); P = I;} while (! (J = p | CMP (* j, * p) -- J; If (J! = P) {my_swap (* P, * j); P = J ;}} iterator it = begin; ++ it; if (P! = Begin & P! = It) my_sort (begin, P, CMP); It = P; ++ it; if (it! = End & it! = Last) my_sort (it, end, CMP) ;}// comparator class cmpint {public: bool operator () (int A, int B) const {return A> B ;}}; int main (void) {int na [] = {13, 24, 22, 19, 44, 56, 88, 22 }; vector <int> VI (Na, na + 8); // vector list <int> Li (Na, na + 8); // list my_sort (Na, na + 8); // test the array print (Na, na + 8); my_sort (VI. begin (), Vi. end (); // test vector print (VI. begin (), Vi. end (); my_sort (Li. begin (), Li. end (), cmpint (); // test list print (Li. begin (), Li. end (); Return 0 ;}


Quick Sort function Template

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.