惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

腾讯CDC
The Cloudflare Blog
IT之家
IT之家
V
V2EX
雷峰网
雷峰网
MyScale Blog
MyScale Blog
P
Proofpoint News Feed
Stack Overflow Blog
Stack Overflow Blog
博客园 - Franky
Engineering at Meta
Engineering at Meta
S
SegmentFault 最新的问题
GbyAI
GbyAI
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - 司徒正美
云风的 BLOG
云风的 BLOG
小众软件
小众软件
博客园 - 叶小钗
Blog — PlanetScale
Blog — PlanetScale
C
Check Point Blog
A
About on SuperTechFans
B
Blog
月光博客
月光博客
宝玉的分享
宝玉的分享
Last Week in AI
Last Week in AI

Victrid's Personal Site

斯普拉遁3打工模式联机网络过程分析 DHCP无类型静态路由──一个摆设 配置透明代理,实现无感上网 Generating Unlimited Grammar: A Messenger Perspective LaTeX插入其他PDF中的矢量图 光速不变原理和迈克尔孙──莫雷实验 交大葡萄演义 调试微信内置浏览器 欧悌甫戎篇 苏格拉底的申辩篇 LCA 最近公共祖先 弥罗斯人的辩论 埃斯库罗斯作品:波斯人、阿伽门农 电路理论资料 Coronavirus: A Humanitarian Crisis 雨夜 linux配置SSR 二叉堆的实现 懒政 Placement New Flipped: A Love Story 我宁可呆在这样的疯人院里 谈新发本地病例 勇敢与智慧 Powerful but Limited Drafted VLC Libva Error Troubleshooting Ring-Fit新上手 政治艺术化与艺术政治化 动态规划——从分割等和子集入手
鸽巢排序与桶排序
Victrid · 2020-02-13 · via Victrid's Personal Site

引入

在一个长度为 n 的数组 nums 里的所有数字都在 0~n-1 的范围内。数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次。请找出数组中任意一个重复的数字。(Cf. Leetcode,2020)。

有一种做法是这样的:

1
2
3
4
5
6
7
8
int* Count = new int[n]();
for (int i = 0; i < n; i++) {
Count[nums[i]]++;
}
for (int i = -; i < n; i++) {
if (Count[i] >= 2)
return i;
}

这样的做法,我们同样可以用在排序中。对于上下界确定的欲排序数组,我们建立一个计数数组,遍历整个数组,进行计数,就可以直接输出对应的有序数组。我们称这种排序方法为鸽巢排序。这种排序方法在特殊的情况下可以实现时间复杂度 $O(n)$ 。

有些时候计数数组显得过于庞大,而且,例如对于一个双精度数组,就无法建立这样的对应数组。这个时候我们设立一系列“容量”相等的集合,将数组进行有序分割,再对被分割的子数组实施排序。这样的集合称为,这样的排序方法称为桶排序

桶排序继承了鸽巢排序的优点,但是鸽巢排序占用空间大的问题仍然解决得不好。而且桶排序仍然有先决条件,可以说是一种特殊情况下的排序。

桶排序是一种分配排序。分配排序不需要进行关键码的比较,但需要知道待排序列的一些具体情况。(Cf.Jin-nuo,2016)

Author: Victrid

Permanent Link: https://victrid.dev/2020/ge-chao-pai-xu-yu-tong-pai-xu/

License: Copyright (c) 2025 victrid Terms of Use

Ads by Google

Read our privacy policy on how these personalized advertisements are delivered to you.

For your reading experience, we provide full-text RSS feeds. Although math formulas cannot be displayed well, the interface can be adjusted as you like and there are no ads.