[C language] Usage of important function qsort function

Table of contents

          1. Introduction of qsort function

                1. Integer array

                2. Character array

                3. String

                4. Structure

          Second, the use of qsort function


1. Introduction of qsort function

The qsort function is a low-level quick sort function, and its characteristic is that it can sort any type of data, such as: integer, string, floating point, and structure type.

The specific usage is as follows:

1. void* base: Indicates the starting address of the array to be sorted;

2. size_t num: indicates the number of array elements;

3. size_t size: Indicates the size of an array element (in bytes);

4. int (*compar)(const void*, const void*): Indicates a function pointer that compares the size of two elements. The function pointed to is a sorting function. The two parameters contained are the address of the compared element, and the type of the last parameter It is void*, which can receive parameters of any form. 

1. Integer array

int cmp_int(const void* e1, const void* e2)
{
	return *(int*)e1 - *(int*)e2;
}

The above function is a self-defined comparison function, the return type of the function is int type, if <0, e1 is smaller than e2, if >0, e1 is greater than e2, if =0, e1=e2. Mandatory conversion of two element pointers into integer pointers, dereferencing to obtain the value of the compared element, and finally returning the difference between the two elements.

2. Character array

int cmp_char(const void* e1, const void* e2)
{
	return *(char*)e1 - *(char*)e2;
}

3. String

 (1) Sort by the first letter of the string

int cmp_string(const void* e1, const void* e2)
{
	return *(char*)e1 - *(char*)e2;
}

 (2) Sort by string length

int cmp_string(const void* e1, const void* e2)
{
    return strlen((char*)e1) > strlen((char*)e2) ? 1 : -1;
}

4. Structure

If the structure is as follows:

struct Stu
{
   char name[20];//姓名
   int age;//年龄
}s;

 (1) Sort by name

int cmp_stu_by_name(const void* e1, const void* e2)
{
	return strcmp(((struct Stu*)e1)->name, ((struct Stu*)e2)->name);
}

 (2) If the names are the same, sort by age

int cmp_stu_by_age(const void* e1, const void* e2)
{
	if ((strcmp(((struct Stu*)e1)->name, ((struct Stu*)e2)->name)) != 0)
		return strcmp(((struct Stu*)e1)->name, ((struct Stu*)e2)->name);
	else
		return ((struct Stu*)e1)->age - ((struct Stu*)e2)->age;
}

Second, the use of qsort function

1. Use for sorting integer array elements

int cmp_int(const void* e1, const void* e2)
{
	return *(int*)e1 - *(int*)e2;
}
int main()
{
	int arr[10] = { 5,7,9,3,1,6,2,4,0,8 };
	int sz = sizeof(arr) / sizeof(arr[0]);
	qsort(arr, sz, sizeof(arr[0]), cmp_int);
	for (int i = 0; i < sz; i++)
	{
		printf("%d ", arr[i]);
	}
	printf("\n");
	return 0;
}

operation result:

2. The use of structure comparison

struct Stu
{
	char name[20];
	int age;
};
int cmp_stu_by_name(const void* e1, const void* e2)
{
	if ((strcmp(((struct Stu*)e1)->name, ((struct Stu*)e2)->name)) != 0)
		return strcmp(((struct Stu*)e1)->name, ((struct Stu*)e2)->name);
	else
		return ((struct Stu*)e1)->age - ((struct Stu*)e2)->age;
}
int main()
{
	struct Stu s[] = { { "zhangsan", 20 }, { "wangwu", 18 }, { "zhaohu", 50 }, { "lisi", 44 }, { "mahan", 33 }, {"zhaohu",67} };
	int sz = sizeof(s) / sizeof(s[0]);
	qsort(s, sz, sizeof(s[0]), cmp_stu_by_name);
	for (int i = 0; i < sz; i++)
	{
		printf("%s %d\n", s[i].name, s[i].age);
	}
	return 0;
}

Run as follows:


If there are deficiencies in this article, you are welcome to comment below, and I will correct it as soon as possible.

 Old irons, remember to like and pay attention!!!  

Guess you like

Origin blog.csdn.net/m0_63198468/article/details/128400798