Use of the C/C++ qsort function

Time:2023-10-30

catalogs

I. Preface

II. Specific use of functions

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

2. Analysis

3. Integer array sorting

4. Floating-point array sorting

5. Character array sorting

6. Structure array sorting

III. Unfinished business.


I. Preface

Every time you encounter the need to sort the problem need to write a custom function, this is more trouble, and the time complexity may not pass, that specific how to solve it?The C library function provides a qsort function that has a smaller time complexity than the sort function you write yourself, used without having to write their own function body, let’s learn the next function!

II. Specific use of functions

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

Use of the C/C++ qsort function If you want to know the library functions in C like I do, you can search the CPLUSPLUS website. Website:cplusplus.com Use of the C/C++ qsort function Then you will be taken to this screen, and then follow the arrows in the picture below: Use of the C/C++ qsort function That would take you to the old cplusplus site: Use of the C/C++ qsort function Then search in the search box pointed to by the arrow to search for the syntax information of the library function you want.

2. Analysis

Use of the C/C++ qsort function As you can tell from the above, someInformation about qsort: 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 in the array pointed to by the base. size_t is an unsigned integer type. 6, the third parameter is size_t size: the size of each element of the array (in bytes) size_t is an unsigned integer type. 7. The fourth argument is int (*compar)(const void*p1,const void*p2)): a pointer to the function that compares the two elements. Based on the description of the parameters above, that is, the fourth parameter is a bit overwhelming, but that’s okay, I’ll do the explaining. The fourth argument is a function pointer to a function whose return value is of type int. When the return value is <0, the element pointed to by p1 precedes the element pointed to by p2; the return value is =0, the element pointed to by p1 is equivalent to the element pointed to by p2; the return value is >0, the element pointed to by p1 is after the element pointed to by p2.

3. Integer array sorting

The last argument in the qsort function is passed a function (The function name is the address of the function), the parameter type of this function is of type viod*, here void isIt’s not empty.The meaning of the word is now, ratherarbitrarilyThe meaning of the But data of type void*Direct dereferencing and direct pointer arithmetic are not possible.You need to force it to convert to the data type you need before you can do the various operations. return *(int*)p1 – *(int*)p2;//*(int*)p1, this is the ascending order of the writing, to descending order if you want to exchange the position of the p1 and p2 on the line, move the smart little brain can figure out.
#include<stdio.h>
#include<stdlib.h>
int cmp(const void* p1, const void* p2)
{
	return *(int*)p1 - *(int*)p2;//*(int*)p1 --- force convert p1 to int* type, and then dereference the forced-converted pointer
}
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;
}
Run results: Use of the C/C++ qsort function

4. Floating-point array sorting

This and the above is not much difference, that is, the type of the array, the type of strong transfer, format output symbols are replaced with floating-point type on the line, nothing 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 slightly more tricky than the one above, but not by much. The only thing to note is that after p1 has performed a strong transfer it should be bracketed as a whole (*(Stu*)p1) like this, and then the pointing operation should be performed.
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] = { { "Zhang San",50 }, { "Wang Wu",70 }, { "Li Si",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;
}

III. Unfinished business.

Thanks for your support, my next post will explain how to use theBubble Sort O(n^2)to implement the qsort function. The qsort function of the library function is the one that uses theRapid Sort O(nlogn)

Recommended Today

uniapp and applet set tabBar and show and hide tabBar

(1) Set the tabBar: uni.setTabberItem({}); wx.setTabberItem({}); indexnumberisWhich item of the tabBar, counting from the left, is indexed from 0.textstringnoButton text on tabiconPathstringnoImage PathselectedIconPathstringnoImage path when selectedpagePathstringnoPage absolute pathvisiblebooleannotab Whether to display uni.setTabBarItem({ index: 0, text: ‘text’, iconPath: ‘/path/to/iconPath’, selectedIconPath: ‘/path/to/selectedIconPath’, pagePath: ‘pages/home/home’ }) wx.setTabBarItem({ index: 0, text: ‘text’, iconPath: ‘/path/to/iconPath’, selectedIconPath: ‘/path/to/selectedIconPath’, pagePath: ‘pages/home/home’ }) […]