The use of qsort () and sort () in C + +

Source: Internet
Author: User
Tags strcmp

Recently learned younger brother asked the quick sort of more, today I do a summary, fast sorting in the library functions have ready-made, without their own implementation, call can achieve their desired results, master can completely abandon the bubble and choice, and time complexity from O (n*n) to O (N*log (n )),

First say the Qsort () in C:

Header file: #include <stdlib.h>

Call Parameters: Qsort (sort the first address, need to sort the number of elements, order the size of the elements, define the function name of the sorting method);

1, sort int array A[n]:

int cmp (const void *a, const void *b)
{
return * (int *) a - * (int *) b;
}//Ascending

int cmp (const void *a, const void *b)
{
return * (int *) b - * (int *) a;
}//Descending

2, sort the string array A[n]:

int cmp (const void *a, const void *b)
{
Return strcmp ((char *) a, (char *) b);
}//Ascending

int cmp (const void *a, const void *b)
{
Return strcmp ((char *) b, (char *) a);
}//Descending

3, sort the struct array a[n]

struct node
{
int x, y;
};

int cmp (const void *a, const void *b)
{
Node *c = (node *) A;
Node *d = (node *) b;
Return c->x - d->x;
}//in ascending order of a[i].x

int cmp (const void *a, const void *b)
{
Node *c = (node *) A;
Node *d = (node *) b;
Return d->x - c->x;
}//in descending order of a[i].x

PS: The structure of the two-level ordering only need to add an IF condition statement.

All of the above sorting functions, when used, are called qsort (A, N, sizeof (a[0)), CMP);

C + + in the sort function: in fact, sometimes the sort function is slightly faster than the Qsort function, and it is more convenient to use, so here to do the ads, there are sort functions, can not be used to create opportunities to use!!!

Header files: #include <algothrim>

Call Parameters: Sort (sort the first address, sort the end address, define the sort function name (optional)), if not sorted by default in ascending order.

Now let's work out how to arrange in descending order.

1, for array a[n] descending order of type int:

BOOL CMP (int a, int b)
{
Return a > B;
}//Descending

2, sort the array a[n] of string type:

BOOL CMP (string A, string b)
{
Return a > B;
}//Descending

3, sort the array a[n] for the structure size:

BOOL CMP (Node A, Node B)
{
return a.x > b.x;
}//Descending

PS: If you sort the struct by a level two, simply add the IF statement to the sort function.

All of the above sorting function calls are used: sort (A, a+n, CMP);

In fact, the powerful STL library provides us with spicy and many functions, some of which are still useful here, such as:

Sort (A, a+n, less< data type > ());//For the specified data type ascending row

Sort (A, a+n, greater< data type > ());//For the specified data type in descending order

Because C + + gives us too many data types, the above is just sort of some common data types.

    

    

The use of qsort () and sort () in C + +

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.