Seven qsort sorting methods

Source: Internet
Author: User
<In this article, sorting is performed in ascending order.>

I. Sorting int-type Arrays

Int num [100];

Sample:

Int cmp (const void * a, const void * B)
{
Return * (int *) a-* (int *) B;
}

Qsort (num, 100, sizeof (num [0]), cmp );

2. Sort char array (same as int type)

Char word [100];

Sample:

Int cmp (const void * a, const void * B)
{
Return * (char *) a-* (int *) B;
}

Qsort (word, 100, sizeof (word [0]), cmp );

3. Sort arrays of the double type (special note)

Double in [100];

Int cmp (const void * a, const void * B)
{
Return * (double *) a> * (double *) B? 1:-1;
}

Qsort (in, 100, sizeof (in [0]), cmp );

4. sorting the struct at a level

Struct In
{
Double data;
Int other;
} S [100]

// Sort by data values from small to large struct. There are many types of sorting key data in the Structure Body. refer to the example above to write

Int cmp (const void * a, const void * B)
{
Return (* (In *) a). data> (* (In *) B). data? 1:-1;
}

Qsort (s, 100, sizeof (s [0]), cmp );

5. List struct

Struct In
{
Int x;
Int y;
} S [100];

// Sort by x from small to large, and sort by y from large to small when x is equal

Int cmp (const void * a, const void * B)
{
Struct In * c = (In *);
Struct In * d = (In *) B;
If (c-> x! = D-> x) return c-> x-d-> x;
Else return d-> y-c-> y;
}

Qsort (s, 100, sizeof (s [0]), cmp );

6. Sort strings

Struct In
{
Int data;
Char str [100];
} S [100];

// Sort the string 'str' in alphabetical order.

Int cmp (const void * a, const void * B)
{
Return strcmp (* (In *) a)-> str, (* (In *) B)-> str );
}

Qsort (s, 100, sizeof (s [0]), cmp );

VII. cmp for convex hull Calculation

Int cmp (const void * a, const void * B) // The Key cmp function sorts all vertices and rotation angles except 1.
{
Struct point * c = (point *);
Struct point * 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 it is in a straight line, place the far one in front of it.
Return 1;
Else return-1;
}

PS:

The qsort function is included in the header file of <stdlib. h>, and strcmp is included in the header file of <string. h>.

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.