当前位置:网站首页>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
边栏推荐
- CA I ah, how many times Oh, ah sentence IU home Oh 11111
- 介绍一款|用于多组学整合和网络可视化分析的在线平台
- 你我他是谁
- Document Layout Analysis: A Comprehensive Survey 2019论文学习总结
- SQL server extracts pure numbers from strings
- 1-18 create the most basic express server & API module for creating routes
- Prediction and regression of stacking integrated model
- 【回溯】全排列 II leetcode47
- twelve thousand three hundred and forty-five
- Ml & DL: introduction to hyperparametric optimization in machine learning and deep learning, evaluation index, over fitting phenomenon, and detailed introduction to commonly used parameter adjustment
猜你喜欢
The Jenkins download Plug-in can't be downloaded. Solution
USBCAN分析仪的配套CAN和CANFD综合测试软件LKMaster软件解决工程师CAN总线测试难题
漫谈Clickhouse Join
1-2 安装并配置MySQL相关的软件
Excitatory neurotransmitter glutamate and brain health
jupyter notebook/lab 切换conda环境
A comprehensive understanding of gout: symptoms, risk factors, pathogenesis and management
Introduction and example of template method mode
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
测试媒资缓存问题
随机推荐
1-13 express listens to get and post requests & processes requests
Side sleep ha ha ha
Study summary of dynamic routing between capsules
vim 常用快捷键
Neurotransmetteurs excitateurs - glutamate et santé cérébrale
Dm8: generate DM AWR Report
Introduction and example of template method mode
jupyterbook 清空控制台输出
“信任机器”为发展赋能
Usbcan analyzer's supporting can and canfd comprehensive test software lkmaster software solves engineers' can bus test problems
开发属于自己的包
一文读懂什么是MySQL索引下推(ICP)
1-1 数据库的基本概念
5g demand in smart medicine
攻防演练中的防泄露全家福
ceshi deces
Akk bacteria - the next generation of beneficial bacteria
qsort函数和模拟实现qsort函数
1-3 使用SQL管理数据库
PyTorch量化实践(2)