当前位置:网站首页>qsort函数和模拟实现qsort函数
qsort函数和模拟实现qsort函数
2022-06-30 21:15:00 【珠江上上上】
目录:
1.qsort函数
2.模拟实现qsort函数
qsort函数
使用快速排序的思想来实现的一个排序函数。
qsort使用所需要的四个参数:
①要排序的数据的起始地址
②待排序的数据元素的个数
③待排序的数据元素的大小,单位字节
④函数指针——比较函数
int (*cmp)(const void* e1,const void* e2)
模拟演示qsort函数的使用:
排序数字
#include <stdio.h>
#include <stdlib.h>
int int_cmp(const void* p1, const void* p2)
{
return (*(int*)p1 - *(int*)p2);
}
int main()
{
int arr[] = { 1,3,5,7,9,2,4,6,8,0 };
int sz = sizeof(arr) / sizeof(arr[0]);
int i = 0;
qsort(arr, sz, sizeof(arr[0]), int_cmp);
for (i = 0; i < sz; i++)
{
printf("%d ", arr[i]);
}
return 0;
} 剖析代码:
函数:int_cmp的升降序

排序结构体
#include <stdio.h>
#include <stdlib.h>
struct stu
{
char name[20];
int age;
};
int int_cmp_name(const void* e1, const void* e2)
{
return strcmp(((struct stu*)e1)->name, ((struct stu*)e2)->name);
}
int int_cmp_age(const void* e1, const void* e2)
{
return strcmp(((struct stu*)e1)->age, ((struct stu*)e2)->age);
}
int main()
{
struct stu s[] = { {"zhang",15},{"li",30},{"wang",25} };
int sz = sizeof(s) / sizeof(s[0]);
qsort(s, sz, sizeof(s[0]), int_cmp_name);
qsort(s, sz, sizeof(s[0]), int_cmp_age);
return 0;
}特别注意:
例如:
int a=10;
char* pa=&a;报错
void* pb=&a;没问题
void*是无具体类型的指针,可以接收任意类型地址,不能解引用,不能+-整数
比较大小
①数字比较:30>25>15
②字母比较: abcd > abbe;b>c
模拟实现qsort函数
#include <stdio.h>
int int_cmp(const void* e1, const void* e2)
{
return (*(int*)e1 - *(int*)e2);
}
void swap(char* e1, char* e2,int width)
{
int i = 0;
for (i = 0; i < width; i++)
{
char tmp = *e1;
*e1 = *e2;
*e2 = tmp;
e1++;
e2++;
}
}
void bubble(void* base, int count, int width, int(*cmp)(void*, void*))
{
int i = 0;
int j = 0;
for (i = 0; i < count-1; i++)
{
int flag = 1;
for (j = 0; j < count - 1 - i; j++)
{
if (cmp((char*)base + j * width, (char*)base + (j + 1) * width) > 0)
{
swap((char*)base + j * width, (char*)base + (j + 1) * width, width);
flag = 0;
}
}
if (flag == 0)
break;
}
}
int main()
{
int arr[] = { 1,3,5,7,9,2,4,6,8,0 };
int sz = sizeof(arr) / sizeof(arr[0]);
int i = 0;
bubble(arr, sz, sizeof(arr[0]), int_cmp);
for (i = 0; i < sz; i++)
{
printf("%d ", arr[i]);
}
return 0;
}结语:
如果对您有帮助的话,
不要忘记点赞+关注哦,蟹蟹
如果对您有帮助的话,
不要忘记点赞+关注哦,蟹蟹
如果对您有帮助的话,
不要忘记点赞+关注哦,蟹蟹
边栏推荐
- SQL Server 提取字符串中的纯数字
- 三个火枪手
- Oprator-1 first acquaintance with oprator
- How to run jenkins build, in multiple servers with ssh-key
- Testing media cache
- 阿里kube-eventer mysql sink简单使用记录
- 1.微信小程序页面跳转方法总结;2. navigateTo堆栈到十层不跳转问题
- 雷达数据处理技术
- A small step in code change and a big leap in thinking
- Markdown notes concise tutorial
猜你喜欢

How to move forward when facing confusion in scientific research? How to give full play to women's advantages in scientific research?

凤凰架构——架构师的视角

Apply for vector bus protocol color picture wallpaper hanging picture, very good!

Software engineering UML drawing

uniapp-路由uni-simple-router

修改已经上线的小程序名称

Open source internship experience sharing: openeuler software package reinforcement test

Adobe-Photoshop(PS)-脚本开发-去除文件臃肿脚本

MySQL introduction, detailed installation steps and usage | dark horse programmer

文本生成模型退化怎么办?SimCTG 告诉你答案
随机推荐
sqlserver 字符串类型转换成小数或者整数类型
Electronic scheme development - Intelligent rope skipping scheme
ClickHouse distributed表引擎
修改已经上线的小程序名称
Text recognition svtr paper interpretation
给苏丹国安德森苏丹的撒过 d s g
. NETCORE redis geo type
What about degradation of text generation model? Simctg tells you the answer
go搭建服务器基础
coredns 修改upstream
asp. Net core JWT delivery
Why have the intelligent investment advisory products collectively taken off the shelves of banks become "chicken ribs"?
防范未授权访问攻击的十项安全措施
MFC界面库BCGControlBar v33.0 - 桌面警报窗口、网格控件升级等
SQL Server 提取字符串中的纯数字
MySQL batch update
ArcGIS construction and release of simple road network data service and rest call test
时空数据挖掘:综述
WebRTC系列-网络传输之本地scoket端口
CentOS - enable / disable Oracle