forked from wangzheng0822/algo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[notes][13_sorts] bucket sort, done.
- Loading branch information
Showing
1 changed file
with
26 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# 线性排序 | ||
|
||
## 开篇问题 | ||
|
||
如何按年龄给 100 万用户排序? | ||
|
||
## 桶排序(Bucket Sort) | ||
|
||
算法思想: | ||
|
||
* 按待排序数据的 key 分有序桶 | ||
* 桶内排序 | ||
* 有序桶依次输出 | ||
|
||
![桶排序示例](https://static001.geekbang.org/resource/image/98/ae/987564607b864255f81686829503abae.jpg) | ||
|
||
### 算法分析 | ||
|
||
* 时间复杂度 $O(n)$ | ||
* $n$ 个元素,分 $m$ 个有序桶,每个桶里平均 $k = n / m$ 个元素 | ||
* 桶内快排,复杂度 $O(k \log k)$,$m$ 个桶一共 $O(n \log k)$ | ||
* 当 $m$ 接近 $n$,例如当 $k = 4$ 时,这个复杂度近似 $O(n)$ | ||
* 使用条件 | ||
* 数据易于分如有序桶 | ||
* 数据在各个有序桶之间分布均匀 | ||
* 适合外部排序——数据不全部载入磁盘 |