当前位置:网站首页>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;
}结语:
如果对您有帮助的话,
不要忘记点赞+关注哦,蟹蟹
如果对您有帮助的话,
不要忘记点赞+关注哦,蟹蟹
如果对您有帮助的话,
不要忘记点赞+关注哦,蟹蟹
边栏推荐
- Radar data processing technology
- 开发技术-使用easyexcel导入文件(简单示例)
- How to run jenkins build, in multiple servers with ssh-key
- The 16th Heilongjiang Provincial Collegiate Programming Contest
- 转:用实际行动赢得别人追随
- 学习总结
- 对多态的理解
- 针对美国国家安全局“酸狐狸”漏洞攻击武器平台的分析与应对方案建议
- A small step in code change and a big leap in thinking
- ArcGIS构建发布简单路网Network数据服务及Rest调用测试
猜你喜欢

CentOS - enable / disable Oracle

RP原型资源分享-购物类App

雷达数据处理技术

Et la dégradation du modèle de génération de texte? Simctg vous donne la réponse

大学生研究生毕业找工作,该选择哪个方向?

文本识别-SVTR论文解读

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

Why have the intelligent investment advisory products collectively taken off the shelves of banks become "chicken ribs"?

毕业五年,想当初若没有入行测试,我是否还会如这般焦虑

关于,奇安信检测代码漏洞,XSS系列解决
随机推荐
uniapp-第三方网络请求
阿里kube-eventer mysql sink简单使用记录
报错:Internal error XFS_WANT_CORRUPTED_GOTO at line 1635 of file fs/xfs/libxfs/xfs_alloc.c.
How can I get the stock account opening discount link? In addition, is it safe to open a mobile account?
vncserver: Failed command ‘/etc/X11/Xvnc-session‘: 256!
FreeRTOS记录(九、一个裸机工程转FreeRTOS的实例)
clickhouse原生监控项,系统表描述
[untitled]
侧睡哈哈哈哈
Fletter nested hell? No, constraintlayout to save!
Text recognition svtr paper interpretation
Double solid histogram / double y-axis
关于,奇安信检测代码漏洞,XSS系列解决
Peking University ACM problems 1002:487-3279
Test medal 1234
Spatiotemporal data mining: an overview
CentOS - enable / disable Oracle
测试勋章1234
A group of K inverted linked lists
网络营销之四大误解