当前位置:网站首页>C语言中的基本库函数(qsort)
C语言中的基本库函数(qsort)
2022-07-30 05:07:00 【录大大i】
qsort排序函数:
基于sort快速排序算法实现的一个排序函数。
基本使用:
void qsort(void* base, //待排序数据的起始位置
size_t num,//数组的元素个数
size_t width,//一个元素的字节大小
int(*cmp)(const void* elem1,sonst void* elem2)//cmp是一个函数指针,指向一个比较函数,需要自己编写,
//elem1与elem2是两个待比较元素的地址
return 返回值:
<0 elem1<elem2
0 elem1=elem2
>0 elem1>elem2
注:整型数据可以用><,结构体数据用><不太方便
举例:
题目说明:在对数字从小到大排序之后,数组的重复元素一定出现在相邻位置中。因此,我们可以扫描已排序的数组,每次判断相邻的两个元素是否相等,如果相等则说明存在重复的元素。
//该函数为构造qsort排序规则
int cmp(const void* _a, const void* _b) {
int a = *(int*)_a, b = *(int*)_b;
return a - b;
}
bool containsDuplicate(int* nums, int numsSize) {
qsort(nums, numsSize, sizeof(int), cmp);
for (int i = 0; i < numsSize - 1; i++) {
if (nums[i] == nums[i + 1]) {
return true;
}
}
return false;
}
作者:LeetCode-Solution
链接:https://leetcode.cn/problems/contains-duplicate/solution/cun-zai-zhong-fu-yuan-su-by-leetcode-sol-iedd/
来源:力扣(LeetCode)
边栏推荐
猜你喜欢

Discourse 自定义头部链接(Custom Header Links)

1315_Use the LOOPBACK simulation mode to test whether the pyserial installation is successful

5. View parsing and template engine

mysql无法远程连接 Can‘t connect to MySQL server on ‘xxx.xxx.xxx.xxx‘ (10060 “Unknown error“)

2.6 Radix sort (bucket sort)

Seven, custom configuration

ms project2010项目管理软件使用技巧总结

pyinstaller打包程序所遇问题记录

The Complete Go Books - Beginner to Advanced and Web Development

05 Detailed explanation of the global configuration file application.properties
随机推荐
解决go环境编译不了exe
error: The following untracked working tree files would be overwritten by
handler+message [message mechanism]
IGBT wafers used in photovoltaic inverters
Whole process scheduling - Azkaban entry and advanced
WPF recursively obtains the list of specified control types in the form
Simulation problem (below)
Summary of skills in using ms project2010 project management software
C. Qualification Rounds
mysql isolation level
(RCE) Remote Code/Command Execution Vulnerability Vulnerability Exercise
LeetCode Algorithm 328. 奇偶链表
Record of problems encountered by the pyinstaller packager
需求设计文档和产品经理的角色改变
Go study notes (84) - Go project directory structure
mysql隔离级别
Solve the go environment can not compile exe
Detailed explanation of REUSE_ALV_GRID_DISPLAY
pyinstaller打包程序所遇问题记录
go语言学习笔记三