Skip to content

Commit

Permalink
[notes][13_sorts] bucket sort, done.
Browse files Browse the repository at this point in the history
  • Loading branch information
Liam0205 committed Oct 26, 2018
1 parent 04e3d8e commit 1197723
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions notes/13_sorts/readme.md
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)$
* 使用条件
* 数据易于分如有序桶
* 数据在各个有序桶之间分布均匀
* 适合外部排序——数据不全部载入磁盘

0 comments on commit 1197723

Please sign in to comment.