Differences between qsort and sort

Source: Internet
Author: User

Next, let's explain qsort and sort in detail and try again! (Note the writing of comparison functions of qsort and sort, which is prone to errors)

First qsort
The basic quick sorting method divides an array into two points and one score in the middle each time. For arrays with multiple duplicate values, the basic sorting efficiency is low. The qsort function integrated in the C language library function uses a three-way division method to solve this problem. The so-called three-way division refers to dividing an array into three parts, which are less than or equal to the value, and greater than the value.
The function sorts the data that the Buf points to in ascending order.

Usage:
Void qsort (void * base, size_t num, size_t width, INT (_ cdecl * compare)
Int compare (const void * elem1, const void * elem2 ));
Qsort (quicksort) provides a quick sorting function based on the comparison conditions you have given. It mainly uses pointer movement to implement sorting. The sorted result is still in the original array.
The parameters have the following meanings:
Base: Start address of the target array to be sorted
Num: number of elements in the target array
Width: the length of each element in the target array.
Compare: function pointer, pointing to comparison Function
 
I. Sorting int-type Arrays
Int num [100];
Int CMP (const void * a, const void * B)
{
Return * (int *) A-* (int *) B;
}
Visible: The parameter list is two null pointers, and now it is going to point to your array element. So convert it to your current type and then set the value. In ascending order.
Because it is based on the value of the ACS code, the value of the preceding character must be smaller than the subsequent character.
Then, for a B, if the value of A is greater than the value of B, it means that in the alphabet, A is behind B, and the return value is 1, which indicates ture and performs exchange.

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

2. Sort char array (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 );
 
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;
The returned value is obviously an integer returned by CMP. Therefore, the double return decimal point is not lost.
}
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
Char STR [1, 100] [2, 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 N above is likely to be mistaken for 100. Here it should actually be the number you want to sort. For example, you actually only have STR [0], STR [1], STR [2] to sort the three strings, then N should be 3 rather than 100;
 
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 );
Note! CMP in qsort must be written by itself.

 

Second sort
When using sort, you must specify using namespace STD; or directly type STD: Sort () with # include <algorithm>
 
Example:
 
# Include <iostream>
# Include <algorithm>
Using namespace STD;
 
Int main (){
Int A [20];
For (INT I = 0; I <20; ++ I)
Cin> A [I];
 
Sort (A, A + 20); // range. Obviously, this is a + 20 note. This is necessary. If it is a + 19
For (I = 0; I <20; I ++) // The last value of a [19] will not be involved in sorting.
Cout <A [I] <Endl;
Return 0;
}
 
STD: sort is a simplified version of qsort. STD: The sort function is better than qsort in some features: taking nine samples for large arrays, a more complete three-way partitioning algorithm, and more detailed sorting of different array sizes using different methods.
 

 
Third differences:
Sort is an upgraded version of qsort. If you can use sort as much as possible, it is also relatively simple to use. Unlike qsort, you have to write CMP functions by yourself. You only need to specify the library functions you use, the parameter has only two (for common usage) header pointers and tail pointers;
By default, sort is sorted in ascending order. If you want it to be sorted in descending order, you can use your own CMP function.
Bool compare (int A, int B)
{
Return A> B; // in descending order. If it is changed to return a <B, it is in ascending order.
}
 

Sort (* a, * B, CMP );

Major differences between sort and qsort comparison functions: for example, to sort an array: # include <iostream> # include <algorithm> using namespace STD; // The Return Value of the following comparison function is 0 or 1 bool CMP1 (int m, int N) {return m> N;} bool cmp3 (int m, int N) {return M-N;} int cmp2 (const void * a, const void * B) {return * (int *) A-* (int *) B ;} int main () {int A [10] = {8, 6, 9, 4, 21, 1, 1, 3, 78,-5}; sort (, A + 10, CMP1); // sort (A, A + 10, cmp3); // qsort (A, 10, sizeof (INT), cmp2); fo R (INT I = 0; I <10; I ++) cout <A [I] <""; cout <Endl; System ("pause ");} we can see that the above results are unexpected! Why does an error occur when the comparison function in sort is changed ?! In the comparison function of CMP1, the returned values are 0 and 1. In the comparison function of cmp2, the returned values are 0,-1, and 1! Let's take a look at the example below: # include <iostream> using namespace STD; bool solve (int A, int B) {// return a-B; Return A> B ;} int main () {int A = 5, B = 10; If (solve (a, B) // if it is less than 0, it returns true! Cout <A <Endl; else cout <B <Endl; System ("pause ");}

If there is anything you don't understand: Look at the http://blog.solrex.org/articles/cygwin-gcc-qsort-error-ext.html

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.