Simple and practical C ++ quick sorting template class

Source: Internet
Author: User

(1) Objective
in solving the actual problem, we found that many problems can be attributed to sorting and querying data. The query efficiency depends largely on the sorting efficiency, especially when the data volume reaches the sea level. Therefore, designing an effective sort algorithm is crucial. This article designs a General C ++ quicksort template class. By simply providing a data class, you can implement a fast Sorting Algorithm for any data, improving the development efficiency.
(2) idea of the fast Sorting Algorithm
the most basic idea of fast sorting is based on the grouping policy:
for the input subsequence L [p .. r]. If the scale is small enough, sort the data directly. Otherwise, the data is processed in three steps:
1 decomposition (divide): returns the input sequence L [p .. r] divided into two non-empty subsequences L [p .. q] and L [q + 1 .. r], so that l [p .. q] the value of any element is not greater than l [q + 1 .. r] the value of any element.
2. Recursive solution (conquer): uses recursive call to sort l [P. q] and L [q + 1. R] respectively.
3 Merge (merge): Because the sorting of the two subsequences obtained by splitting is performed in the local place .. q] and L [q + 1 .. r] After sorting, no computing is required. L [p .. r] the order has been sorted.
(3) preparations and Source Code
1. Create a console project using vc6
2. Add the following template class:

Template <typename datatype> // datatype is a template parameter, representing the data type class quicksorttemp {public: quicksorttemp (){}~ Quicksorttemp () {} public: // implementation of quick sorting. array is the array for sorting data. The nlower and nupper ranges from 0 ~ Total Data Count-1 static void quicksort (ype * array, int nlower, int nupper) {// test whether the sorting is complete if (nlower <nupper) {// separate and sort int nsplit = partition (array, nlower, nupper); // data is split into two parts: quicksort (array, nlower, nsplit-1 ); // recursive sorting of the left half (array, nsplit + 1, nupper); // recursive sorting of the right half }}


// Split the data into the left and right parts and return the number of the intermediate element x.
// The main process is to select an element x as the demarcation point and place the elements larger than X to the right of X, and the rest to the left of X.

 static int partition (datatype * array, int nlower, int nupper) {int nleft = nlower + 1; datatype lower = array [nlower]; int nright = nupper; datatype swap; while (nleft <= nright) {While (nleft <= nright & array [nleft]. compareto (bytes) <= 0) nleft = nleft + 1; while (nleft <= nright & array [nright]. compareto (bytes)> 0) nright = nright-1; if (nleft 
  

The template class for fast sorting is implemented.
3. Implementation of Data interfaces
From the implementation of the template class above, we can see that in order to use this template class to sort a certain type of data array datatype * data, we must implement the datatype interface compareto (compare two datatype elements, the size of B, A> B returns 1, A = B returns 0, otherwise-1 is returned ).
For example, to sort the two-dimensional coordinate, define the size relationship: first compare the size of the X axis coordinate value. If X is the same, the size of the Y value determines the size relationship. That is, (1, 1) = (1, 1), (2, 1)> (1, 10), (3, 5) <(4, 1 ).
In addition, the default constructor without parameters of the datatype type must be implemented (because it is used in the template class ).
Define the Data Type mypoint as follows:

 
Struct mypoint {mypoint () {} mypoint (int x, int y) {This-> X = x; this-> Y = y;} int compareto (mypoint & B) {If (this-> x <B. x) Return-1; else if (this-> x> B. x) return 1; else {If (this-> Y> B. y) return 1; else if (this-> Y <B. y) Return-1; else return 0 ;}} int X; int y ;};

(4) test
The following are the main functions used for testing:

 int _ tmain (INT argc, tchar * argv [], tchar * envp []) {int nretcode = 0; // point array mypoint points [10] = {mypoint (), mypoint (1, 7 ), mypoint (), mypoint (7, 1), mypoint ()}; int COUNT = 10; // printf ("before quicksort:/N") Before sorting; For (INT I = 0; I 
  
    (% d, % d)/n", I, points [I]. x, points [I]. y); // call the template class to sort quicksorttemp 
   
    : quicksort (points, 0, Count-1); // printf ("after quicksort:/N") after sorting "); for (I = 0; I 
    
      (% d, % d)/n", I, points [I]. x, points [I]. y); System ("pause"); Return nretcode ;}
    
   
  

result output:
before quicksort:
0 <-------> ()
1 <------->)
2 <-------> ()
3 <-------> ()
4 <-------> ()
5 <------->)
6 <-------> ()
7 <-------> ()
8 <-------> ()
9 <------->)
after quicksort:
0 <-------> (1, 1)
1 <-------> (1, 1)
2 <-------> (1, 7)
3 <-------> (2, 2)
4 <-------> (2, 5)
5 <----- --> ()
6 <-------> ()
7 <-------> ()
8 <------->)
9 <-------> ()
press any key to continue...
(5) Description
Based on the quick sorting algorithm, this article implements a C ++ quick sorting template class. You can use this template class and follow the interface definitions that must be implemented to sort data types to quickly sort any data types. Of course, the example in this article is just a basic guide.

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.