Use of qsort in C/C ++

Source: Internet
Author: User
C/C ++ has a standard library function named qsort for fast sorting, which is declared in stdlib. H. Its prototype is:

Void qsort (void * base, int nelem, unsigned int width, INT (* pfcompare) (const void *, const void *));

This function can be used to sort all types of one-dimensional arrays. In this function parameter, base is the starting address of the array to be sorted, nelem is the number of elements in the array to be sorted, and width is the size of each element in the array to be sorted (in bytes ), the last parameter pfcompare is a function pointer pointing to a "comparison function ". Sorting is a process of constantly comparing and exchanging locations. How does qsort compare two elements and determine which element should be in the front when the element type is unknown? The answer is that during execution of the qsort function, a "comparison function" is called through the pfcompare pointer to determine which of the two elements should be at the top. This "comparison function" is not a C/C ++ library function, butProgramPrepared by members. When calling qsort, pass the name of the comparison function as a real parameter to pfcompare. The programmer knows what rules should be used to determine which element should come first and which element should be later. This rule is embodied in "comparison functions.

The usage of the qsort function stipulates that the prototype of the "comparison function" should be: int function name (const void * elem1, const void * elem2); two parameters of the function, elem1 and elem2, points to the two elements to be compared. In other words, * elem1 and * elem2 are the two elements to be compared. This function must have the following actions:

1) if * elem1 should be placed before * elem2, the return value of the function is a negative integer (any negative integer ).

2) If * elem1 and * elem2 are both at the top, the function returns 0.

3) If * elem1 should be placed behind * elem2, the return value of the function is a positive integer (any positive integer ).

For example:

# Include <iostream>
# Include <stdio. h>
# Include <stdlib. h>

Using namespace STD;

Int compare (const void * a, const void * B)
{
Int * pA = (int *);
Int * pb = (int *) B;
Return (* pA)-(* pb); // sort data in ascending order.
}

Void main ()
{
Int A [10] = {5, 6, 4, 3, 7, 0, 8, 9, 2, 1 };
Qsort (A, 10, sizeof (INT), compare );
For (INT I = 0; I <10; I ++)
Cout <A [I] <"" <Endl;
}

Another example is:

The following program calls the qsort library function to sort an unsigned int array in ascending order of individual digits. For example, the numbers 8, 23, and 15 should be, 15, and 8 in ascending order of single digits:

# Include <stdio. h>
# Include <stdlib. h>
Int mycompare (const void * elem1, const void * elem2
{
Unsigned int * P1, * P2;
P1 = (unsigned int *) elem1; // Statement 6
P2 = (unsigned int *) elem2; // Statement 7
Return (* P1 % 10)-(* P2 % 10); // Statement 8
}
# Define num 5
Int main ()
{
Unsigned int an [num] = {8,123, 4 };
Qsort (an, num, sizeof (unsigned INT), mycompare );
For (INT I = 0; I <num; I ++)
Printf ("% d", an [I]);
Return 0;
}

The output result of the above program is:
10 11 123 4 8

During the execution of the qsort function, the address of the two elements is used as the parameter to compare the two elements and call the mycompare function. If the returned value is less than 0, qsort will know that the first element should be in front. If the returned value is greater than 0, the first element should be in the back. If the return value is equal to 0, the system will always be the first.
Explain Statement 6 as follows: Because elem1 is of the const void * type and is a void pointer, the expression "* elem1" is meaningless. Elem1 should point to the element to be compared, that is, a variable of the unsigned int type. Therefore, to forcibly convert the type, assign the address stored in elem1 to P1. In this way, * P1 is
An element. Statement 7 is the same. Statement 8 shows the sorting rules. If * P1 is less than * P2, a negative value is returned. The other two cases are not described here.

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.