C + + sort () and qsort () (Incomplete introduction)

Source: Internet
Author: User

In the usual brush algorithm and OJ, sorting algorithm is one of the most commonly used algorithms, and in the directory of the various algorithms are usually placed in various sorting algorithms in the first place, the importance of the visible sorting algorithm. Probably many people in the algorithm book have learned bubbles, fast sorting method, also have a general understanding of its principle; In practical applications, the bubble sort is the simplest, of course, the complexity is the highest .... (as Gartner said: "The bubble sort does not seem to be recommended in addition to its fascinating name and the fact that it has led to some interesting theoretical problems"); fast-line is ideally the most efficient (NLOGN). But fast sorting is not as good as the bubble sort of understanding and memory, each time you need to remember and the template code to knock up ... It is a very troublesome thing to think about. However, fortunately in C + + has a fast-line function ready-to-use ... It's a wonderful thing to think about.

Now some of the uses of sort () and qsort () are described, but they are not complete, just a list of some of the things used in the game.

Sort function: In fact, the sort () is not called a quick sort, but rather a smart sort; it normally uses a fast row, but when it finds that the fast line worsens, it automatically adjusts to other sorts to assist. Is the most efficient sort. In the algorithm library of C + +.

/**/

eg. :

int _main () {    int a[20],i;    for (i=0;i<20;i++)         cin>>a[i];    Sort (a,a+20);  Sort in ascending order for    (i=0;i<20;i++)    cout<<a[i]<<endl;    return 0;}

The sort here is ascending, so what if you want to do it in descending order? Here is the code that you write the comparison function to implement:

int cmp (int a,int b) {      return a<b;   in ascending order; return a>b, or descending} int main () {     int a[20],i;     for (i=0;i<20;i++)       cin>>a[i];     Sort (a,a+20,cmp);     for (i=0;i<20;i++)       cout<<a[i]<<endl;     return 0;}

In fact, just to achieve the integer and real array of ascending, descending order, you can have a more simple method, do not need to re-edit the function, because the standard library has a ready-made, it provides a number of template-based comparison function objects, can be used directly; here's the greater<data_type. > and less<data_type>.

    • Ascending: Sort (begin,end,less<data-type> ());
    • Descending: Sort (begin,end,greater<data-type> ());
int main () {      int a[20],i;      for (i=0;i<20;i++)          cin>>a[i];      Sort (a,a+20,greater<int> ());//If you want to press descending, change to less<int> () for      (i=0;i<20;i++)          Cout<<a[i] <<endl;      return 0;}

Below is Qsort ():

Qsort (quicksort) gives an array a quick sort based on the comparison function you give, which is the sort function by pointer movement. The result of sorting is still placed in the original array.

The usage of qsort and compare is as follows:

void void *baseint  Compare); int Compare (constvoidconstvoid *elem2);

Compare function:

1 intCompareConst void*a,Const void*b)2 3 {4   return*(int*) A-* (int*) b;//Sort Ascending5 6 //return * (int *) b-* (int *) A;//Sort Descending7 8}

The above is a sort of int, and if it is a character type, the int is changed to char. And the double type is the following:

1 int cmp (constvoid*a,constvoid*b)2{3      return * (double*) a>* (double*) b?  1:-1; // Ascending 4 5   // return * (double*) b>* (double*) a?1:-1; descending 6 }

C + + sort () and qsort () (not fully described)

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.