qsort function Usage

Source: Internet
Author: User

Transferred from: http://www.cnblogs.com/syxchina/archive/2010/07/29/2197382.html

Qsort Features: Sorting using the quick sort routines 

Usage: void qsort (void *base, int nelem, int width, int (*fcmp) (const void *,const void *));

Parameters: 1 The number of elements to be sorted in the first address of the array 2 array 3 The space size of each element 4 pointers to functions

There are many kinds of sequential sorting methods used to determine sorting, sort by, bubble sort, merge sort, quick sort, etc. Look at the name. Fast sorting is now recognized as a good sorting algorithm (I did not shuwang faster than this, special occasions exception), than the choice of sorting, bubble sort is faster. This is because he is very fast, so the system also in the library to implement the algorithm, easy for us to use. This is qsort.

Qsort requires a comparison function, which is a little more versatile . For example, you're not just going to sort a number, you might want to sort several numbers, such as having a struct struct num {int A; int b;}; Then I have an array of type num, num dddd[100]; I want to sort dddd this array, what do I do? I want to get the largest num element of a +b in the front of the array, so what? This can all be done by defining a comparison function. The function of the comparison function is to show qsort how the size of the element is compared. A comparison function like this is an inline int mycmp (const void* A, const void* B) that has two elements as arguments, returns an int value if the comparison function returns greater than 0,qsort, and if the comparison function returns equals 0 Qsort think A and b these two elements equal, return less than 0 qsort think AB), you compare the function returns a 1 (less than 0) then qsort think a< This article is the sort of small to large sort >

First, sort an array of type int

int num[100];

Sample:int cmp (const void *a, const void *b)

{return * (int *) A-* (int *) b; }

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

Second, sort the array of char types (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);

Third, sort the array of double types (especially 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);

Iv. sequencing of structural bodies

struct in {double data; int other;} S[100]

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 kinds,

Refer to the example above to write

int cmp (const void *a, const void *b)

{return (* (*) a). Data > (* (in *) b). Data 1:-1;}

Qsort (s,100,sizeof (S[0]),CMP);

Five, the structure of the two-level ranking

struct in {int x; int y;} S[100];

Sort by x from small to large, when x is equal by y from largest to smallest

int cmp (const void *a, const void *b)

{

struct in *c = (in *) A; 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);

Six, sort the string

struct in {int data; char str[100];} S[100];

Sort by the dictionary order of the string str in the struct

int cmp (const void *a, const void *b)

{

Return strcmp ((* (in *) a)->str, (* (in *) b)->str);

}

Qsort (S,100,sizeof (s[0]), CMP);

CMP for convex hull in computational geometry

int cmp (const void *a,const void *b)

Key CMP function to sort all points except 1 points and rotate angles

{

struct Point *c= (point *) A;

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 you're in a straight line, put it far in front.

return 1;

else return-1;

}

qsort function Usage

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.