Qsort usage in C-language STL

Source: Internet
Author: User

The Qsort function is included in the <stdlib.h> header file.

The Qsort function is declared as follows:
void Qsort (void *base, size_t nmemb, size_t size, int (*compar) (const void *, const void *));

The parameters are described as follows:
Base: the array to sort
NMEMB: Number of elements in an array
Size: Each array element occupies memory space and can be obtained using sizeof
Compar: Compares the comparison function of two elements of an array. When the first parameter value of this comparison function is less than, equal to, or greater than the second parameter value, the return value of this comparison function shall be less than, equal to, or greater than 0 respectively.

Just say you want to implement a function like this (ascending):
int cmp (const void *a, const void *b)
If a > B, return to >0
If a = = B, return 0
If a < B, return to <0
The relationship between A and B here is only logical, not a value comparison, so the sort can be more than just a number, it can also be a character.

< The following sort is used from small to large sort >

First, sort an array of type int

int num[]; int Const void Const void *b) {        return * (int *) A-* (int *) b;} Qsort (num,max,sizeof(num[0]), CMP);

To sort a two-dimensional array:
int a[1000][2]; Where the size of a[0] is a whole sort, where a[1] must move the interchange with A[0.

Qsort (A,N,sizeof(int) *2, comp); int Comp (constvoid *a,constvoid *b) {     return (int *) a) [0] ((int *) b) [0];

Second, sort the array of char types (same as int type)

Char word[]; int Const void Const void *b) {        return * (Char *) A-* (char *) b;} Qsort (Word,andsizeof(word[0]), CMP);

Third, sort the array of double types (especially note)

Double inch [+]; int Const void Const void *b) {        return * (double *) a > * (double1 :- 1 ;} Qsort (in, N,sizeof(in [0]), CMP);

Iv. sequencing of structural bodies

structin{Doubledata;intOther ;} s[ -]//according to the value of data from small to large structure of the order, about the structure of the key data types can be many, refer to the above example to writeintcmpConst void*a,Const void*b) {        return(* (in *) a). Data > (* (*) b). Data?1: -1;} Qsort (s), -,sizeof(s[0]), CMP);

Five, the structure of the two-level ranking

structin{intx;inty;} s[ -];//Sort by x from small to large, when x is equal by y from largest to smallestintcmpConst void*a,Const void*b) {structIn *c = (structIn *) A;structIn *d = (structIn *) b;if(c->x! = d->x)returnC->x-d->x;Else returnD->y-c->y;} Qsort (s), -,sizeof(s[0]), CMP);

Six, sort the string

structin{intdata;Charstr[ -];} s[ -];//Sort by the dictionary order of the string str in the structintCMP (Const void*a,Const void*b) {        returnstrcmp ((* (in *) a). STR, (* (in *)b). str);} Qsort (s), -,sizeof(s[0]), CMP);

CMP for convex hull in computational geometry

intcmpConst void*a,Const void*B)//The key CMP function, which puts all points except 1 points, sorts the rotation angle {structPoint *c= (Point *) A;structPoint *d= (Point *) b;if(Calc (*c,*d,p[1]) <0)return 1;Else if(!calc (*c,*d,p[1]) && dis (c->x,c->y,p[1].x,p[1].Y) < Dis (d->x,d->y,p[1].x,p[1].Y))//If you're in a straight line, put it far in front .return 1;Else return-1;}

Qsort usage in C-language STL

Related Article

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.