First item: qsort:
( just a couple of concepts, just to get a sense of it )
The basic fast sort method, which divides the array into two points and a dividing value in the middle, is less efficient for arrays with multiple duplicate values. The Qsort function, which is integrated in the C language library function, solves this problem by using the three-way partitioning method. The so-called three-way division, refers to dividing the array into less than the divided value, equal to the dividing value and greater than the divided value of three parts.
The buf function sorts the data pointed to by an ascending order.
How to use:
void Qsort (void *base, size_t num, size_t width, int (__cdecl *compare)
int compare (const void *elem1, const void *elem2));
Qsort (Quicksort) is mainly based on the comparison conditions you give to a quick sort, mainly through pointer movement to achieve sorting function. The result of sorting is still placed in the original array.
The parameters have the following meanings: (be aware of the meanings of these parameters.) )
Base: Destination array start address to sort
Num: Number of target array elements
Width: The length of each element in the target array
Compare: function pointer, pointing to comparison function
(Key or specific use of the time how to use, actually encountered in these cases)
First, sort an array of type int
int num[100];
int cmp (const void *a, const void *b)
{
return * (int *) A-* (int *) b;
Visible: The argument list is a two null pointer, and now he's going to point to your array element. So transition to your current type, then take the value. in ascending order.
Because the values of the ACS code are followed, the values in the preceding characters must be less than the characters that follow.
So, for a B, if the value of a is >b, it means that in the alphabet A is after B, the return value is 1 for ture, and the interchange is performed.
}
Qsort (Num,100,sizeof (num[0]), CMP);
Second, sort the array of char types (same as int type)
Char word[100];
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;
Problem with the return value, it is obvious that CMP is returning an integral type, so avoid a double return decimal and be lost.
to a judgment.
}
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, refer to the above example to 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 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
Char str[100][100];
int cmp (const void* a,const void* B)
{
Return strcmp ((char *) A, (char*) b);
}
Qsort (Str,n,sizeof (str[0]), CMP);
It is worth noting that the above N, it is likely that you will be mistaken for 100, here is actually the number you want to sort, for example, you actually only str[0],str[1],str[2] these three strings to sort, then n should be 3, not the four;
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);
Attention! The CMP in Qsort has to be written by himself.
Second item: Sort:
The sort must be noted: using namespace std; or direct std::sort () plus #include <algorithm>
Cases:
#include <iostream>
#include <algorithm>
using namespace Std;
int main () {
int a[20];
for (int i=0;i<20;++i)
scanf ("%d", &a[]i);
Sort (a,a+20);
for (i=0;i<20;i++)
cout<<a[i]<<endl;
return 0;
}
Note: The general form of sort is sort (a,a+n,cmp), if the system default CMP can be used without the third parameter, where the first parameter A is sorted at the first address for a "0", a+n when the end address is a[n-1], such as sort (a+10,a+50) Represents the sort from a[10] to a[49].
Std::sort is an improved version of Qsort. The Std::sort function is superior to some features of Qsort: Taking 9 samples for large arrays, a more complete three-way partitioning algorithm, and more meticulous sorting of different array sizes by different methods.
Third difference:
Sort is an upgrade version of Qsort, if you can use sort as far as possible, use is also relatively simple, not like qsort also have to write the CMP function, as long as the use of the library function can be used, the parameters only two (if the general usage) head pointer and tail pointer;
The default sort is ascending, and if you want to sort it in descending order, you can use your own CMP function
Intl CMP (int a,int b)
{
Return a>b; Descending order, or ascending if return a<b is changed
}
Sort (a,a+n,cmp);
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
A summary and comparison of the usage of C language qsort and C++sort