C Language standard library function qsort specific explanation

Source: Internet
Author: User
Tags strcmp

1Simple Introduction to Functions

Function: Sort by using high-speed sort routines

Header file: stdlib.h
Usage: void qsort (void *base,int nelem,int Width,int (*fcmp) (const void *,const void *));
Number of references: 1 The first address of the array to be sorted
2 number of elements to be sorted in the array
3 space size of each element

4 A pointer to a function that determines the order of the sort

2Basic Use Method

Use qsort () Sorting and bsearch () search is a relatively common combination, convenient and quick to use.
Qsort'sFunction prototypesis void __cdecl qsort (void *base,size_tnum,size_t width,int (__cdecl *comp) (const void *,const void*))
Where base is a collection of sorted arrays, NUM is the number of elements of this array, width is the size of an element, comp is a comparative function.
For example: when sorting an array of length 1000, int a[1000]; then base should be a,num should be 1000,width should be sizeof (int), and the comp function should be named with its own.
qsort (a,1000,sizeof (int), comp);
the comp function should be written as:
int Comp (const void *a,const void *b)
{
return * (int *) a-* (int *) b;
}qsortabove is sorted by small to large, return * (int *) b-* (int *) A;
is to aTwo-dimensional arraysto sort by:
int a[1000][2], in which a general sort is performed according to the size of a[0], in which a[1] must move the interchange with A[0.
qsort (a,1000,sizeof (int) *2,comp);
int Comp (const void *a,const void *b)
{
return ((int *) a) [0]-((int *) b) [0];
}

The Qsort function is provided in the ANSI C standard, and its declaration in the Stdlib.h file is written according to the two distributions, with a time complexity of N*log (n), which is structured as:

void Qsort (void *base,size_t nelem,size_t Width,int (*comp) (const void *,const void *));

Of

*base is the array to sort

Nelem the length of the array to sort

Width the size of the array element (in one-byte units)

The default is from small to large sort!

(* Comp) (const void *p1,const void *P2) is a pointer to the inferred size function, this function needs to be customized, assuming P1>P2, the function returns -1;A<B, the function returns the 1;a==b function returns 0

There are many kinds of sorting methods: Select Sort, bubble sort, merge sort, high speed sort, etc.
Look at the name all know high-speed sorting is now recognized as a better sorting algorithm (I did not hear that 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. The

Qsort requires a comparison function, which is a little better for versatility. For example, you're not just going to sort a number, you might want to sort a few numbers, like 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 be done by defining a comparison function.
The function of the qsort is to indicate how the size of the element is compared.
Like this comparative function inline int mycmp (const void* A, const void* b)

There are two elements as the parameters, return an int value, assuming that the comparison function returns greater than 0,qsort feel a>b, assuming that the return of the function is equal to 0 qsort think A and b two elements equal, return less than 0 qsort feel a<b.

Qsort know the size of the element, you can put the big to the front.

Assuming that your comparison function is supposed to be 1 (a>b), you compare the function returned a 1 (less than 0) then qsort think A<b, put B to the front, but in fact is a greater than B, so it caused the difference between ascending and descending order.

3 Related Examples

Seven kinds of qsort sorting methods

< sorting in this article is used from 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; Cast type
}

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-* (char *) 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 important data types can be very many, reference the above example write

int cmp (const void *a, const void *b)
{
Return (* (in *) a). Data > (* (*) 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 equals 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, sorting all points except 1 points, rotation angle
{
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-&GT;X,D-&GT;Y,P[1].X,P[1].Y))// Assuming that you are in a straight line, put the distance in front
return 1;
else return-1;
}

Ps:

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

C Language standard library function qsort specific explanation

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.