当前位置:网站首页>Qsort function and Simulation Implementation of qsort function
Qsort function and Simulation Implementation of qsort function
2022-06-30 21:48:00 【On the Pearl River】
Catalog :
qsort Function and Simulation Implementation qsort function
1.qsort function
2. Simulation Implementation qsort function
qsort function
Using the idea of quick sort to achieve a sort function .
qsort Use the four parameters you need :
① The starting address of the data to be sorted
② The number of data elements to be sorted
③ The size of the data elements to be sorted , Unit byte
④ A function pointer —— Comparison function
int (*cmp)(const void* e1,const void* e2)
Simulation demonstration qsort Use of functions :
Sort the numbers
#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;
} Dissect the code :
function :int_cmp The ascending and descending order of

Sort structure
#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;
}Particular attention :
for example :
int a=10;
char* pa=&a; Report errors
void* pb=&a; That's all right.
void* Is a pointer without a specific type , Can receive any type of address , Can't dereference , You can't +- Integers
Compare the size
① Number comparison :30>25>15
② Letter comparison : abcd > abbe;b>c
Simulation Implementation qsort function
#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;
}Conclusion :
If it helps you ,
Don't forget it give the thumbs-up + Focus on Oh , Crab
If it helps you ,
Don't forget it give the thumbs-up + Focus on Oh , Crab
If it helps you ,
Don't forget it give the thumbs-up + Focus on Oh , Crab
边栏推荐
猜你喜欢

jupyter notebook/lab 切换conda环境

Four Misunderstandings of Internet Marketing

Excitatory neurotransmitter glutamate and brain health

Random talk about Clickhouse join

根据肠道微生物组重新思考健康饮食

Markdown notes concise tutorial

【无标题】

Usbcan analyzer's supporting can and canfd comprehensive test software lkmaster software solves engineers' can bus test problems

JD and Tencent renewed the three-year strategic cooperation agreement; The starting salary rose to 260000 yuan, and Samsung sk of South Korea scrambled for a raise to retain semiconductor talents; Fir

5G 在智慧医疗中的需求
随机推荐
谈谈数字化转型的几个关键问题
ca i啊几次哦啊句iu家哦
【无标题】
ML&DL:机器学习和深度学习中超参数优化的简介、评估指标、过拟合现象、常用的调参优化方法之详细攻略
To the Sultanate of Anderson
sdfsdf
1-7 path module
模板方法模式介绍与示例
京东与腾讯续签三年战略合作协议;起薪涨至26万元,韩国三星SK争相加薪留住半导体人才;Firefox 102 发布|极客头条
微服務鏈路風險分析
1-1 basic concepts of database
周少剑,很少见
Pytorch quantitative practice (1)
Coefficient of variation method matlab code [easy to understand]
clickhouse原生监控项,系统表描述
布隆过滤器
A comprehensive understanding of gout: symptoms, risk factors, pathogenesis and management
1-10 根据不同的url响应客户端的内容
Sqlserver string type converted to decimal or integer type
Analyse des risques liés aux liaisons de microservices