qsort / bsearch
qsort bsearch
include <stdlib.h>
void qsort (void* base, size_t num, size_t size,
int (*compar)(const void*,const void*));
//第一个参数 数组
//第二个参数 数组大小
//第三个参数 元素大小
//第四个参数 函数
#include <stdlib.h>
#include <stdio.h>
typedef struct {
char name[30];
int Chinese;
int Math;
int English;
} Student;
Student students[7];
void readData() {
FILE* file = fopen("mark.txt", "r");
int i;
for (i = 0; i < 7; i++) {
fscanf(file, "%s", students[i].name);
fscanf(file, "%d", &students[i].Chinese);
fscanf(file, "%d", &students[i].Math);
fscanf(file, "%d", &students[i].English);
}
fclose(file);
}
void displayData() {
int i;
for (i = 0; i < 7; i++) {
printf("%s\t", students[i].name);
printf("%d\t", students[i].Chinese);
printf("%d\t", students[i].Math);
printf("%d\n", students[i].English);
}
}
int compare(const void* a, const void* b) {
Student* pa = (Student *)a;
Student* pb = (Student *)b;
int num1 = pa->Chinese;
int num2 = pb->Chinese;
return num2 - num1;
}
int main(void) {
readData();
qsort(students, 7, sizeof(Student), compare);
displayData();
return 0;
}
//mark.txt
Tom 97 68 45
Jerry 100 32 88
Harry 78 88 78
Lucy 87 90 89
Mickey 88 77 66
Peter 59 68 98
Alice 62 73 89
/* !< output */
//按语文成绩排序
Jerry 100 32 88
Tom 97 68 45
Mickey 88 77 66
Lucy 87 90 89
Harry 78 88 78
Alice 62 73 89
Peter 59 68 98
Process returned 0 (0x0) execution time : 0.006 s
Press any key to continue.
/* qsort example */
#include <stdio.h> /* printf */
#include <stdlib.h> /* qsort */
int values[] = { 40, 10, 100, 90, 20, 25 };
int compare (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}
int main ()
{
int n;
qsort (values, 6, sizeof(int), compare);
for (n=0; n<6; n++)
printf ("%d ",values[n]);
return 0;
}
bsearch
void* bsearch (const void* key, const void* base,
size_t num, size_t size,
int (*compar)(const void*,const void*));
#include <stdlib.h>
#include <stdio.h>
typedef struct {
char name[30];
int Chinese;
int Math;
int English;
} Student;
Student students[7];
void readData() {
FILE* file = fopen("mark.txt", "r");
int i;
for (i = 0; i < 7; i++) {
fscanf(file, "%s", students[i].name);
fscanf(file, "%d", &students[i].Chinese);
fscanf(file, "%d", &students[i].Math);
fscanf(file, "%d", &students[i].English);
}
fclose(file);
}
void displayData() {
int i;
for (i = 0; i < 7; i++) {
printf("%s\t", students[i].name);
printf("%d\t", students[i].Chinese);
printf("%d\t", students[i].Math);
printf("%d\t", students[i].English);
printf("%d\n", students[i].Chinese + students[i].Math + students[i].English);
}
}
int compare(const void* a, const void* b) {
Student* pa = (Student *)a;
Student* pb = (Student *)b;
int num1 = pa->Chinese + pa->Math + pa->English;
int num2 = pb->Chinese + pb->Math + pb->English;
return num2 - num1;
}
int compare2(const void* key, const void* e) {
int* pNum1 = (int *)key;
Student* pS = (Student *)e;
int num1 = *pNum1;
int num2 = pS->Math + pS->Chinese + pS->English;
return num2 - num1;
}
int main(void) {
readData();
qsort(students, 7, sizeof(Student), compare);
int key = 224;
Student* s = bsearch(&key, students, 7, sizeof(Student), compare2);
printf("%s\n", s->name);
displayData();
return 0;
}
/* output */
/* !< 寻找总分为224的人 */
Alice
Lucy 87 90 89 266
Harry 78 88 78 244
Mickey 88 77 66 231
Peter 59 68 98 225
Alice 62 73 89 224
Jerry 100 32 88 220
Tom 97 68 45 210
Process returned 0 (0x0) execution time : 0.005 s
Press any key to continue.
/* bsearch example */
#include <stdio.h> /* printf */
#include <stdlib.h> /* qsort, bsearch, NULL */
int compareints (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}
int values[] = { 50, 20, 60, 40, 10, 30 };
int main ()
{
int * pItem;
int key = 40;
qsort (values, 6, sizeof (int), compareints);
pItem = (int*) bsearch (&key, values, 6, sizeof (int), compareints);
if (pItem!=NULL)
printf ("%d is in the array.\n",*pItem);
else
printf ("%d is not in the array.\n",key);
return 0;
}
/* bsearch example with strings */
#include <stdio.h> /* printf */
#include <stdlib.h> /* qsort, bsearch, NULL */
#include <string.h> /* strcmp */
char strvalues[][20] = {"some","example","strings","here"};
int main ()
{
char * pItem;
char key[20] = "example";
/* sort elements in array: */
qsort (strvalues, 4, 20, (int(*)(const void*,const void*)) strcmp);
/* search for the key: */
pItem = (char*) bsearch (key, strvalues, 4, 20, (int(*)(const void*,const void*)) strcmp);
if (pItem!=NULL)
printf ("%s is in the array.\n",pItem);
else
printf ("%s is not in the array.\n",key);
return 0;
}
reference
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!