在C语言中,qsort
函数用于对数组进行快速排序。以下是详细的使用方法及示例:
一、函数原型
#include <stdlib.h>void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)
);
二、参数说明
参数 | 说明 |
---|---|
base | 指向数组首元素的指针(即数组名) |
nmemb | 数组的元素个数 |
size | 每个元素的大小(字节数,可用 sizeof 获取) |
compar | 比较函数的指针,用于定义排序规则(返回负数、0、正数表示第一个参数应排在前面、相等、后面) |
三、比较函数模板
比较函数的参数是 const void*
,需在内部转换为实际类型后再比较:
int compare(const void *a, const void *b) {// 将 void* 转为实际类型(如 int*)const Type *ptrA = (const Type*)a;const Type *ptrB = (const Type*)b;// 定义比较规则:// 若 a < b,返回负数;a > b,返回正数;相等返回0return (*ptrA - *ptrB); // 示例:升序排列
}
四、使用示例
1. 对整数数组升序排序
#include <stdio.h>
#include <stdlib.h>int compare_ints(const void *a, const void *b) {int arg1 = *(const int*)a;int arg2 = *(const int*)b;return (arg1 > arg2) - (arg1 < arg2); // 避免整数溢出
}int main() {int arr[] = {5, 2, 8, 1, 3};size_t len = sizeof(arr) / sizeof(arr[0]);qsort(arr, len, sizeof(int), compare_ints);for (size_t i = 0; i < len; i++) {printf("%d ", arr[i]); // 输出:1 2 3 5 8}return 0;
}
2. 对字符串数组排序(按字典序)
#include <string.h>int compare_strings(const void *a, const void *b) {const char **str1 = (const char **)a; // 注意双指针转换const char **str2 = (const char **)b;return strcmp(*str1, *str2);
}int main() {char *strs[] = {"apple", "banana", "cherry"};size_t len = sizeof(strs) / sizeof(strs[0]);qsort(strs, len, sizeof(char*), compare_strings);for (size_t i = 0; i < len; i++) {printf("%s ", strs[i]); // 输出:apple banana cherry}return 0;
}
3. 对结构体按字段排序
typedef struct {char name[50];int age;
} Person;int compare_person_age(const void *a, const void *b) {const Person *p1 = (const Person*)a;const Person *p2 = (const Person*)b;return p1->age - p2->age; // 按年龄升序
}int main() {Person people[] = {{"Alice", 30}, {"Bob", 25}};size_t len = sizeof(people) / sizeof(people[0]);qsort(people, len, sizeof(Person), compare_person_age);for (size_t i = 0; i < len; i++) {printf("%s: %d\n", people[i].name, people[i].age); // 输出:Bob: 25 → Alice: 30}return 0;
}
五、常见问题
1. 如何实现降序排序?
在比较函数中反转返回值:
int compare_ints_desc(const void *a, const void *b) {int arg1 = *(const int*)a;int arg2 = *(const int*)b;return (arg1 < arg2) - (arg1 > arg2); // 降序排列
}
2. 如何避免整数溢出?
使用显式比较代替减法:
int compare_ints_safe(const void *a, const void *b) {int arg1 = *(const int*)a;int arg2 = *(const int*)b;if (arg1 < arg2) return -1;if (arg1 > arg2) return 1;return 0;
}
3. 为什么排序结果不正确?
- 错误原因:比较函数未正确处理边界(如未考虑负数)。
- 解决方案:确保比较函数对任意输入都能正确返回负/0/正值。
通过灵活定义比较函数,qsort
可排序任意类型的数据,是C语言中最通用的排序工具。