@@ -60,15 +60,15 @@ $$\sum _{i=0} ^{} \frac {n}{2^i} = 2n$$
6060
6161$$ \sum_{i=0}^n (n-i+1) = O(n^2) $$
6262
63- ![ Quicksort Recursive] ( https://raw.githubusercontent.com/billryan/algorithm-exercise/master /shared-files/images/qsort1.png)
63+ ![ Quicksort Recursive] ( ../.. /shared-files/images/qsort1.png)
6464
6565## in-place - 原地快排
6666
6767### one index for partition
6868
6969先来看一种简单的 in-place 实现,仍然以` [6, 5, 3, 1, 8, 7, 2, 4] ` 为例,结合下图进行分析。以下标 $$ l $$ 和 $$ u $$ 表示数组待排序部分的下界(lower bound)和上界(upper bound),下标 $$ m $$ 表示遍历到数组第 $$ i $$ 个元素时当前 partition 的索引,基准元素为 $$ t $$ , 即图中的 target.
7070
71- ![ Quick Sort one index for partition] ( https://raw.githubusercontent.com/billryan/algorithm-exercise/master /shared-files/images/qsort2.png)
71+ ![ Quick Sort one index for partition] ( ../.. /shared-files/images/qsort2.png)
7272
7373在遍历到第 $$ i $$ 个元素时,$$ x[i] $$ 有两种可能,第一种是 $$ x[i] \geq t $$ , $$ i $$ 自增往后遍历;第二种是 $$ x[i] < t $$ , 此时需要将 $$ x[i] $$ 置于前半部分,比较简单的实现为 ` swap(x[++m], x[i]) ` . 直至 ` i == u ` 时划分阶段结束,分两截递归进行快排。既然说到递归,就不得不提递归的终止条件,容易想到递归的终止步为 ` l >= u ` , 即索引相等或者交叉时退出。使用 Python 的实现如下所示:
7474
@@ -164,7 +164,7 @@ public class Sort {
164164
165165先来一张动图看看使用两个索引进行 partition 的过程。
166166
167- ![ Quick Sort two index for partition] ( https://raw.githubusercontent.com/billryan/algorithm-exercise/master /shared-files/images/qsort3.gif)
167+ ![ Quick Sort two index for partition] ( ../.. /shared-files/images/qsort3.gif)
168168
1691691 . 选中` 3 ` 作为基准
1701702 . ` lo ` 指针指向元素` 6 ` , ` hi ` 指针指向` 4 ` , 移动` lo ` 直至其指向的元素大于等于` 3 ` , 移动` hi ` 直至其指向的元素小于` 3 ` 。找到后交换` lo ` 和` hi ` 指向的元素——交换元素` 6 ` 和` 2 ` 。
@@ -293,5 +293,5 @@ Robert Sedgewick 在其网站上对 [Quicksort](http://algs4.cs.princeton.edu/23
293293- [ 快速排序 - 维基百科,自由的百科全书] ( http://zh.wikipedia.org/wiki/%E5%BF%AB%E9%80%9F%E6%8E%92%E5%BA%8F )
294294- [ Quicksort | Robert Sedgewick] ( http://algs4.cs.princeton.edu/23quicksort/ )
295295- Programming Pearls Column 11 Sorting - 深入探讨了插入排序和快速排序
296- - [ Quicksort Analysis] ( https://raw.githubusercontent.com/billryan/algorithm-exercise/master /shared-files/docs/quicksort_analysis.pdf)
296+ - [ Quicksort Analysis] ( ../.. /shared-files/docs/quicksort_analysis.pdf)
297297- [ ^ programming_pearls ] : Programming Pearls(第二版修订版) 一书中第11章排序中注明需要 $$ n\log2n $$ 次比较,翻译有误?
0 commit comments