Table of contents
1. Introduction to qsort function
2. Declaration of qsort function
1. Introduction to qsort function
The qsort function can sort arrays of any data type , such as integers, floating point types, strings, and structure types.
The qsort function is a library function, and the corresponding header file should be included when used ( #include<stdlib.h> )
2. Declaration of qsort function
void qsort(void *base, size_t num, size_t width, int ( *compare)(const void *elem1,const void *elem2));
void *base: the address of the first element of the array to be sorted
size_t num: the number of elements in the array
size_t width: the size of the array element (number of bytes occupied)
int (*compare)(const void* elem1, const void* elem2): This parameter is a function pointer , pointing to a comparison function, which is used to compare the size of two elements when sorting. The two parameters (elem1, elem2) are the addresses of the elements to be compared, and the parameter type is void*, so any type of parameter can be received.
The return type is Int. When elem1 < elem2, return a value <0; when elem1 = elem2, return 0 ; It needs to return a value >0 when elem1 < elem2, and return a value <0 when elem1 > elem2)
3. Use of qsort function
1. Plastic surgery
First implement the comparison function (cmp_int)
int cmp_int(const void* p1, const void* p2)
{
return (*((int*)p1)) - (*(int*)p2);
}
First cast the pointer to an integer pointer, then dereference to obtain the value of the element, and finally return the difference between the two elements.
Then call the qsort function
#include <stdio.h>
#include <stdlib.h>
int cmp_int(const void* p1, const void* p2)
{
return (*((int*)p1)) - (*(int*)p2);
}
void test1()
{
int arr[] = { 1,2,10,4,11,6,7,9,0 };
int sz = sizeof(arr) / sizeof(arr[0]);
printf("排序前: ");
for (int i = 0; i < sz; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
qsort(arr, sz, sizeof(arr[0]), cmp_int);
printf("排序后: ");
for (int i = 0; i < sz; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
int main()
{
test1();
return 0;
}
Sort results
2. Floating point
First implement the comparison function cmp_double
Double is 8 bytes. If the difference between two elements is returned directly, the forced type conversion is int at this time, and the floating point type is incomplete, and an error may occur
like
Therefore, use the if else statement to judge when processing
int cmp_double(const void* p1, const void* p2)
{
double ret = *(double*)p1 - *(double*)p2;
if (ret > 0)
return 1;
else if (ret < 0)
return -1;
else
return 0;
}
Then call the qsort function
#include <stdio.h>
#include <stdlib.h>
int cmp_double(const void* p1, const void* p2)
{
double ret = *(double*)p1 - *(double*)p2;
if (ret > 0)
return 1;
else if (ret < 0)
return -1;
else
return 0;
}
void test2()
{
double arr[] = { 1.3,3,2.1,0.9,1.7,10.3,9.1 };
int sz = sizeof(arr) / sizeof(arr[0]);
printf("排序前:");
for (int i = 0; i < sz; i++)
{
printf("%.2lf ", arr[i]);
}
printf("\n");
qsort(arr, sz, sizeof(arr[0]), cmp_double);
printf("排序后:");
for (int i = 0; i < sz; i++)
{
printf("%.2lf ", arr[i]);
}
printf("\n");
}
int main()
{
test2();
return 0;
}
Sort results
3. Structure type
When sorting structure types, first determine the basis for sorting
such as structure
struct stu
{
int age;
char name[10];
double score;
};
First determine whether to sort by age, name, or score, or perform multi-level sorting, that is, sort by score first, and then sort by name if the scores are the same.
(1) First-level sorting
sort by name
Comparison function cmp_by_name
Use the strcmp function to compare the size of two strings
int cmp_stu_by_name(const void* p1,const void* p2)
{
return (strcmp(((struct stu*)p1)->name , ((struct stu*)p2)->name ));
}
Call the qsort function
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct stu
{
int age;
char name[10];
double score;
};
int cmp_stu_by_name(const void* p1,const void* p2)
{
return (strcmp(((struct stu*)p1)->name , ((struct stu*)p2)->name ));
}
void test3()
{
struct stu students[3] = { {12,"li",90.5},{11,"zhang",80.3},{10,"wang",97.6} };
int sz = sizeof(students) / sizeof(students[0]);
printf("排序前:\n");
for (int i = 0; i < sz; i++)
{
printf("%d %s %.2lf", students[i].age, students[i].name, students[i].score);
printf("\n");
}
printf("\n");
qsort(students, sz, sizeof(students[0]), cmp_stu_by_name);//按名字排序
printf("排序后:\n");
for (int i = 0; i < sz; i++)
{
printf("%d %s %.2lf", students[i].age, students[i].name, students[i].score);
printf("\n");
}
printf("\n");
}
int main()
{
test3();
return 0;
}
Sort results
(2) Multi-level sorting
Sort by grade first, if the grades are the same, sort by name
Comparison function cmp_stu
int cmp_stu(const void* p1, const void* p2)
{
double ret = ((struct stu*)p2)->score - ((struct stu*)p1)->score;
if (ret > 0)
return 1;
else if (ret < 0)
return -1;
else
return (strcmp(((struct stu*)p1)->name, ((struct stu*)p2)->name));
}
When the scores are the same, sort by name. If you want to sort by age, if the names are the same, sort by age.
Call the qsort function
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct stu
{
int age;
char name[10];
double score;
};
int cmp_stu(const void* p1, const void* p2)
{
double ret = ((struct stu*)p2)->score - ((struct stu*)p1)->score;
if (ret > 0)
return 1;
else if (ret < 0)
return -1;
else
return (strcmp(((struct stu*)p1)->name, ((struct stu*)p2)->name));
}
void test3()
{
struct stu students[] = { {12,"li",90.5},{11,"zhang",80.3},{10,"wang",97.6},{12,"zhao",80.3} };
int sz = sizeof(students) / sizeof(students[0]);
printf("排序前:\n");
for (int i = 0; i < sz; i++)
{
printf("%d %s %.2lf", students[i].age, students[i].name, students[i].score);
printf("\n");
}
printf("\n");
qsort(students, sz, sizeof(students[0]), cmp_stu);
printf("排序后:\n");
for (int i = 0; i < sz; i++)
{
printf("%d %s %.2lf", students[i].age, students[i].name, students[i].score);
printf("\n");
}
printf("\n");
}
int main()
{
test3();
return 0;
}
Sort results