<Sort used herein are sorted in ascending>
a, sort the array of type int
int NUM [100];
the Sample:
int CMP (A const void *, const void * B)
{
return * (int *) A - * (int *) B;
}
qsort (NUM, 100, the sizeof (NUM [0]), CMP);
Second, the char type sort the array (the same type int)
char Word [100];
the Sample:
int CMP (const void * A, const void * B)
{
return * (char *) A - B * (int *);
}
qsort (Word, 100, the sizeof (Word [0]), CMP);
three, sort the array of double type ( particular attention)
Double in [100];
int CMP (A const void *, const void * B)
{
return * (* Double) A> * (Double *). 1 B:? -1;
}
qsort (in, 100, sizeof ( in [0]), cmp);
four, a sort of structure
struct the In
{
Double data;
int OTHER;
} S [100]
// the value of the data according to the general structure of the small , the sort about structures in the body of the key data data types can be a variety of examples with reference to the above write
int CMP (const void * a, const void * B)
{
return (* (the in *) a) -> data> (* (the In *) B) -> Data. 1: -1;?
}
qsort (S, 100, the sizeof (S [0]), CMP);
five, two sort of structure
struct the In
{
int X;
int Y;
S} [100];
// x accordance with small to large, when x is equal descending order according to y
int CMP (const void * A, * B const void)
{
struct the In * C = (the In *) A ;
struct the In * D = (the In *) B;
IF (! the C-> X = D-> X) the C-return> X - D-> X;
the else return D-> Y - the C-> Y;
}
qsort (S, 100, the sizeof (S [0]) , CMP);
six, sort strings
struct the in
{
int Data;
char str [100];
} S [100];
// lexicographically ordered structure in the string str
int cmp (const void * a, const void * B)
{
return strcmp ((* (the In *) A) -> STR, (* (the In *) B) -> STR);
}
qsort (S, 100, the sizeof (S [0]), CMP );
seven, calculating the convex hull geometry seeking cmp
int cmp (const void * a, * B const void) // cmp function key, all the points in addition to one point outside the rotational angle of the sort
{
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 in a straight line,
put away in front of the
return. 1;
the else return -1;
}
the PS:
wherein qsort function contained in <stdlib.h> header file, strcmp contained in the <string.h> header file
a, sort the array of type int
int NUM [100];
the Sample:
int CMP (A const void *, const void * B)
{
return * (int *) A - * (int *) B;
}
qsort (NUM, 100, the sizeof (NUM [0]), CMP);
Second, the char type sort the array (the same type int)
char Word [100];
the Sample:
int CMP (const void * A, const void * B)
{
return * (char *) A - B * (int *);
}
qsort (Word, 100, the sizeof (Word [0]), CMP);
three, sort the array of double type ( particular attention)
Double in [100];
int CMP (A const void *, const void * B)
{
return * (* Double) A> * (Double *). 1 B:? -1;
}
qsort (in, 100, sizeof ( in [0]), cmp);
four, a sort of structure
struct the In
{
Double data;
int OTHER;
} S [100]
// the value of the data according to the general structure of the small , the sort about structures in the body of the key data data types can be a variety of examples with reference to the above write
int CMP (const void * a, const void * B)
{
return (* (the in *) a) -> data> (* (the In *) B) -> Data. 1: -1;?
}
qsort (S, 100, the sizeof (S [0]), CMP);
five, two sort of structure
struct the In
{
int X;
int Y;
S} [100];
// x accordance with small to large, when x is equal descending order according to y
int CMP (const void * A, * B const void)
{
struct the In * C = (the In *) A ;
struct the In * D = (the In *) B;
IF (! the C-> X = D-> X) the C-return> X - D-> X;
the else return D-> Y - the C-> Y;
}
qsort (S, 100, the sizeof (S [0]) , CMP);
six, sort strings
struct the in
{
int Data;
char str [100];
} S [100];
// lexicographically ordered structure in the string str
int cmp (const void * a, const void * B)
{
return strcmp ((* (the In *) A) -> STR, (* (the In *) B) -> STR);
}
qsort (S, 100, the sizeof (S [0]), CMP );
seven, calculating the convex hull geometry seeking cmp
int cmp (const void * a, * B const void) // cmp function key, all the points in addition to one point outside the rotational angle of the sort
{
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 in a straight line,
put away in front of the
return. 1;
the else return -1;
}
the PS:
wherein qsort function contained in <stdlib.h> header file, strcmp contained in the <string.h> header file
Reproduced in: https: //my.oschina.net/garyun/blog/602801