Use of C/C++qsort function

Homepage:

There are still unknowns waiting to be explored_Data structure, small projects, Luogu question brushing-CSDN Blogz

Topic columns:

C Language Difficulties_There are still unknown blogs waiting to be explored-CSDN Blog

Table of contents

I. Introduction        

2. Specific use of functions 

1. How to search for specific syntax usage of library functions 

 2.Analysis

3. Sorting of integer arrays 

4. Sorting of floating point arrays 

5.Character array sorting  

6. Structure array sorting 

 3. To be continued. . .


I. Introduction        

        Every time you encounter a problem that requires sorting, you need to write a custom function. This is more troublesome, and the time complexity may not be enough. So how to solve it? The C language library function provides a qsort function, which has less time complexity than the sorting function written by yourself . You don’t have to write the function body yourself to use it. Let’s learn this function next!

2. Specific use of functions 

1. How to search for specific syntax usage of library functions 

 If you want to know the library functions in C language like me, you can search on the CPLUSPLUS official website.

Website: cplusplus.com

 You will then enter this interface and follow the arrows in the picture below:

In this case, you will enter the old version of cplusplus website:

Then search in the search box pointed by the arrow to search for the syntax information of the library function you want to know. 

 2.Analysis

From the above, you can know some qsort information :

1. The header file is stdlib.h

2. qsort has no return value type.

3. qosrt has 4 parameters.

4. The first parameter is void*base: a pointer to the first object of the array to be sorted, converted to void*.

5. The second parameter is size_t num: the number of elements pointed to by the base in the array. size_t is an unsigned integer type.

6. The third parameter is size_t size: the size of each element in the array (in bytes). size_t is an unsigned integer type.

7. The fourth parameter is int (*compar)(const void*p1, const void*p2)): a pointer to a function that compares two elements.

According to the description of the parameters above, the fourth parameter is a bit confusing, but it’s okay, let me explain it.

The fourth parameter is a function pointer. This function pointer points to a function. The return value of this function is of type int. When the return value is <0, the element pointed by p1 precedes the element pointed by p2; when the return value is =0, the element pointed by p1 is equivalent to the element pointed by p2; when the return value is >0, the element pointed by p1 is after the element pointed by p2.

3. Sorting of integer arrays 

The last parameter in the qsort function needs to be passed to a function ( the function name is the address of the function ). The parameter type of this function is the viod* type. The void here does not mean empty , but means arbitrary . However, void* type data cannot be directly dereferenced and directly subjected to pointer operations . It needs to be forcibly converted into the data type you need before various operations can be performed.

 return *(int*)p1 - *(int*)p2;//*(int*)p1, this is written in ascending order. If you want to order in descending order, just swap p1 and p2. Just use your clever little brain. I can figure it out.

#include<stdio.h>
#include<stdlib.h>
int cmp(const void* p1, const void* p2)
{
	return *(int*)p1 - *(int*)p2;//*(int*)p1---把p1强制转化为int*类型,然后再把强制转化的指针进行解引用
}
int main()
{
	int arr[10] = { 10,9,8,7,6,5,4,3,2,1 };
	int sz = sizeof(arr) / sizeof(arr[0]);
	qsort(arr,sz,sizeof(arr[0]),cmp);
	for (int i = 0; i < sz; i++)
		printf("%d ", arr[i]);
	return 0;
}

operation result:

4. Sorting of floating point arrays 

 There is not much difference between this and the above. It is just that the array type, forced conversion type, and format output character are all changed to floating point type. It is not too difficult.

#include<stdio.h>
#include<stdlib.h>
int cmp(const void* p1, const void* p2)
{
	return *(float*)p1 - *(float*)p2;
}
int main()
{
	float arr[10] = { 10.0,9.0,8.0,7.0,6.0,5.0,4.0,3.0,2.0,1.0 };
	int sz = sizeof(arr) / sizeof(arr[0]);
	qsort(arr,sz,sizeof(arr[0]),cmp);
	for (int i = 0; i < sz; i++)
		printf("%lf ", arr[i]);
	return 0;
}

5.Character array sorting  

int cmp(const void* p1, const void* p2)
{
	return *(char*)p1 - *(char*)p2;
}

6. Structure array sorting 

This one is a little more troublesome than the one above, but it’s not too troublesome.

The only thing to note is that after the forced transfer of p1, you need to add brackets (* (Stu*) p1) as a whole, like this, and then perform the pointing operation.

typedef struct Stu
{
	char name[20];
	int score;
}Stu;
int cmp(const void* p1, const void* p2)
{
	return ((Stu*)p1)->score - ((Stu*)p2)->score;
}
int main()
{
	Stu s[3] = { { "张三",50 }, { "王五",70 }, { "李四",60 } };
	int sz = sizeof(s) / sizeof(s[0]);
	qsort(s, sz, sizeof(s[0]), cmp);
	for (int i = 0; i < sz; i++)
		printf("%s %d\n", s[i].name,s[i].score);
	return 0;
}

 3. To be continued. . .

Thank you for your support. My next article will explain how to use bubble sort O(n^2) to implement the qsort function.

The qsort function of the library function uses quick sort O(nlogn) .

Guess you like

Origin blog.csdn.net/qq_73435980/article/details/133027820