当前位置:网站首页>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
边栏推荐
- Neurotransmetteurs excitateurs - glutamate et santé cérébrale
- 本地浏览器打开远程服务器上的Jupyter Notebook/Lab以及常见问题&设置
- ceshi deces
- Understand what MySQL index push down (ICP) is in one article
- Rethink healthy diet based on intestinal microbiome
- 《ClickHouse原理解析与应用实践》读书笔记(1)
- PyTorch量化感知训练(QAT)步骤
- Arcmap|assign values to different categories of IDS with the field calculator
- Ssh server configuration file parameter permitrootlogin introduction
- 【无标题】第一次参加csdn活动
猜你喜欢

Bloom filter

【无标题】

1-2 install and configure MySQL related software

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

The Jenkins download Plug-in can't be downloaded. Solution

《ClickHouse原理解析与应用实践》读书笔记(3)

Open the jupyter notebook/lab and FAQ & settings on the remote server with the local browser

A comprehensive understanding of gout: symptoms, risk factors, pathogenesis and management
Understand what MySQL index push down (ICP) is in one article

《ClickHouse原理解析与应用实践》读书笔记(2)
随机推荐
1-11 create online file service
谈谈数字化转型的几个关键问题
Introduce an online platform for multi omics integration and network visual analysis
1-3 using SQL to manage databases
Arcmap|assign values to different categories of IDS with the field calculator
Analysis and proposal on the "sour Fox" vulnerability attack weapon platform of the US National Security Agency
FreeRTOS record (IX. an example of a bare metal project transferring to FreeRTOS)
A group of K inverted linked lists
Introduction to go web programming: a probe into the excellent test library gocovey
1-15 nodemon
“信任机器”为发展赋能
1-20 预检请求
ClickHouse distributed表引擎
《Dynamic Routing Between Capsules》论文学习总结
请问,启牛证券开户,可以开户吗?安全吗?你想要的答案全在这里
1-12 preliminary understanding of Express
周少剑,很少见
ssh 默认端口不是22时的一些问题
1-1 数据库的基本概念
【无标题】第一次参加csdn活动