当前位置:网站首页>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
边栏推荐
- Zaah Sultan looks at the old driver
- 12345
- Understand what MySQL index push down (ICP) is in one article
- 1-19 利用CORS解决接口跨域问题
- 5G 在智慧医疗中的需求
- Arcmap|assign values to different categories of IDS with the field calculator
- Analyse des risques liés aux liaisons de microservices
- [backtracking] full arrangement leetcode46
- 全面认识痛风:症状、风险因素、发病机理及管理
- 1-3 使用SQL管理数据库
猜你喜欢

【无标题】

Summary of errors reported when using YML file to migrate CONDA environment

Random talk about Clickhouse join

A comprehensive understanding of gout: symptoms, risk factors, pathogenesis and management

Neurotransmetteurs excitateurs - glutamate et santé cérébrale

Go Web 编程入门: 一探优秀测试库 GoConvey
Testing media cache

周少剑,很少见

1-2 安装并配置MySQL相关的软件

程序员女友给我做了一个疲劳驾驶检测
随机推荐
Troubleshooting the problem of pytorch geometric torch scatter and torch spark installation errors
Multi table operation - foreign key constraint
Ssh server configuration file parameter permitrootlogin introduction
你我他是谁
微服務鏈路風險分析
介绍一款|用于多组学整合和网络可视化分析的在线平台
京东与腾讯续签三年战略合作协议;起薪涨至26万元,韩国三星SK争相加薪留住半导体人才;Firefox 102 发布|极客头条
[backtracking] full arrangement II leetcode47
Rethink healthy diet based on intestinal microbiome
Pytorch quantitative practice (1)
Clickhouse distributed table engine
[grade evaluator] how to register a grade evaluator? How many passes?
1-18 创建最基本的express服务器&创建路由的API模块
1-13 express监听GET和POST请求&处理请求
ceshi deces
微服务链路风险分析
Analysis and proposal on the "sour Fox" vulnerability attack weapon platform of the US National Security Agency
1-15 nodemon
The programmer's girlfriend gave me a fatigue driving test
将el-table原样导出为excel表格