diff --git a/C04-Recurrences/4.2.md b/C04-Recurrences/4.2.md
index a753da45..a9c442da 100644
--- a/C04-Recurrences/4.2.md
+++ b/C04-Recurrences/4.2.md
@@ -1,88 +1,89 @@
-### Exercises 4.2-1
-***
-Use a recursion tree to determine a good asymptotic upper bound on the recurrence
+### Exercises 4.2-1
+***
+Use a recursion tree to determine a good asymptotic upper bound on the recurrence
 = 3T\(\\lceil n/2 \\rceil\) + n). Use the substitution method to verify your answer.
-### `Answer`
-
-
-树的高度是lgn,有3^lgn个叶子节点.
-
- = n\\sum_{i = 0}^{lg\(n\)-1}\(\\frac{3}{2}\)^i + \\Theta\(3^{\\lg{n}}\) \\\\ ~
-\\hspace{15 mm} = \\Theta\(n^{\\lg{3}}\) + \\Theta\(3^{\\lg{n}}\) \\\\ ~
-\\hspace{15 mm} = \\Theta\(n^{\\lg{3}}\) + \\Theta\(n^{\\lg{3}}\) \\\\ ~
-\\hspace{15 mm} = \\Theta\(n^{\\lg{3}}\)
-)
-
-我们猜想  \\le cn^{\\lg{3}}+2n )
-
- \\le 3c\(n/2\)^{\\lg{3}} + 2n \\\\ ~
-\\hspace{15 mm} \\le cn^{\\lg{3}}+2n \\\\ ~
-\\hspace{15 mm} = \\Theta\(n^{\\lg{3}}\)
-)
-
-
-### Exercises 4.2-2
-***
-Argue that the solution to the recurrence
- = T\(n/3\) + T\(2n/3\) + cn )
-, where c is a constant, is Ω(nlgn) by appealing to the recurrsion tree.
-
-### `Answer`
-最短的叶子高度是lg3n,每一层都要cn.也就是说,只考虑最短叶子的那一层(忽略其他层)已经有cnlg3n.
-
-
-### Exercises 4.2-3
-***
-Draw the recursion tree for
- = 4T\(\\lfloor n/2 \\rfloor\) + cn)
-,where c is a constant, and provide a tight asymptotic bound on its solution. Verify your bound by the substitution method.
-### `Answer`
-
-
-很明显是n^2的级别
-
-我们假设  \\le n^2+2cn)
-
- \\le 4c\(n/2\)^2 + 2cn/2+cn \\le cn^2+2cn)
-
-我们假设  \\ge n^2+2cn)
-
- \\ge 4c\(n/2\)^2 + 2cn/2+cn \\ge cn^2+2cn)
-
-### Exercises 4.2-4
-***
-Use a recursion tree to give an asymptotically tight solution to the recurrence T(n) = T(n - a) + T(a) + cn, where a ≥ 1 and c > 0 are constants.
-
-### `Answer`
-
-file:///Users/ganzhenchao/Workspaces/CLRS/C04-Recurrences/repo/s2/4.png
-  = \\sum_{i=0}^{n/a}c\(n-ia\) + \(n/a\)ca
-= \\Theta\(n^2\))
-
-我们假设  \\le cn^2)
-
- \\le c\(n-a\)^2 + ca + cn \\\\ ~
-\\hspace{15 mm} \\le cn^2-2acn+ca+cn \\\\ ~
-\\hspace{15 mm} \\le cn^2-c\(2an-a-n\) \\\\ ~
-\\hspace{15 mm}\\le cn^2 - cn ~~~~ if ~~ a > 1/2,n > 2a \\\\ ~
-\\hspace{15 mm}\\le cn^2 \\\\ ~
-\\hspace{15 mm} = \\Theta\(n^2\)
-)
-
-另外一个方向的证明和这个基本一样.
-
-### Exercises 4.2-5
-***
-Use a recursion tree to give an asymptotically tight solution to the recurrence T(n) = T(αn) +
T((1 - α)n) + cn, where α is a constant in the range 0 <α < 1 and c > 0 is also a constant.
-
-### `Answer`
-
-
-可以假设α < 1/2,因此树的高度有
-
- = \\sum_{i = 0}^{\\log_{1/ \\alpha}{n}}cn + \\Theta\(n\) = cn\\log_{1/ \\alpha}{n} + \\Theta\(n\) = \\Theta\(n\\lg{n}\) )
-
-***
-Follow [@louis1992](https://github.com/gzc) on github to help finish this task.
-
+### `Answer`
+
+
+树的高度是lgn,有3^lgn个叶子节点.
+
+ = n\\sum_{i = 0}^{lg\(n\)-1}\(\\frac{3}{2}\)^i + \\Theta\(3^{\\lg{n}}\) \\\\ ~
+\\hspace{15 mm} = \\Theta\(n^{\\lg{3}}\) + \\Theta\(3^{\\lg{n}}\) \\\\ ~
+\\hspace{15 mm} = \\Theta\(n^{\\lg{3}}\) + \\Theta\(n^{\\lg{3}}\) \\\\ ~
+\\hspace{15 mm} = \\Theta\(n^{\\lg{3}}\)
+)
+
+我们猜想  \\le cn^{\\lg{3}}+2n )
+
+ \\le 3c\(n/2\)^{\\lg{3}} + 2n \\\\ ~
+\\hspace{15 mm} \\le cn^{\\lg{3}}+2n \\\\ ~
+\\hspace{15 mm} = \\Theta\(n^{\\lg{3}}\)
+)
+
+
+### Exercises 4.2-2
+***
+Argue that the solution to the recurrence
+ = T\(n/3\) + T\(2n/3\) + cn )
+, where c is a constant, is Ω(nlgn) by appealing to the recurrsion tree.
+
+### `Answer`
+最短的叶子高度是lg3n,每一层都要cn.也就是说,只考虑最短叶子的那一层(忽略其他层)已经有cnlg3n.
+
+
+### Exercises 4.2-3
+***
+Draw the recursion tree for
+ = 4T\(\\lfloor n/2 \\rfloor\) + cn)
+,where c is a constant, and provide a tight asymptotic bound on its solution. Verify your bound by the substitution method.
+### `Answer`
+
+
+很明显是n^2的级别
+
+我们假设  \\le n^2+2cn)
+
+ \\le 4c\(n/2\)^2 + 2cn/2+cn \\le cn^2+2cn)
+
+我们假设  \\ge n^2+2cn)
+
+ \\ge 4c\(n/2\)^2 + 2cn/2+cn \\ge cn^2+2cn)
+
+### Exercises 4.2-4
+***
+Use a recursion tree to give an asymptotically tight solution to the recurrence T(n) = T(n - a) + T(a) + cn, where a ≥ 1 and c > 0 are constants.
+
+### `Answer`
+
+file:///Users/ganzhenchao/Workspaces/CLRS/C04-Recurrences/repo/s2/4.png
+  = \\sum_{i=0}^{n/a}c\(n-ia\) + \(n/a\)ca
+= \\Theta\(n^2\))
+
+我们假设  \\le cn^2)
+
+ \\le c\(n-a\)^2 + ca + cn \\\\ ~
+\\hspace{15 mm} \\le cn^2-2acn+ca+cn \\\\ ~
+\\hspace{15 mm} \\le cn^2-c\(2an-a-n\) \\\\ ~
+\\hspace{15 mm}\\le cn^2 - cn ~~~~ if ~~ a > 1/2,n > 2a \\\\ ~
+\\hspace{15 mm}\\le cn^2 \\\\ ~
+\\hspace{15 mm} = \\Theta\(n^2\)
+)
+
+另外一个方向的证明和这个基本一样.
+
+### Exercises 4.2-5
+***
+Use a recursion tree to give an asymptotically tight solution to the recurrence T(n) = T(αn) +
+T((1 - α)n) + cn, where α is a constant in the range 0 <α < 1 and c > 0 is also a constant.
+
+### `Answer`
+
+
+可以假设α < 1/2,因此树的高度有
+
+ = \\sum_{i = 0}^{\\log_{1/ \\alpha}{n}}cn + \\Theta\(n\) = cn\\log_{1/ \\alpha}{n} + \\Theta\(n\) = \\Theta\(n\\lg{n}\) )
+
+***
+Follow [@louis1992](https://github.com/gzc) on github to help finish this task.
+
diff --git a/C04-Recurrences/4.3.md b/C04-Recurrences/4.3.md
index c574affd..ddf0fd75 100644
--- a/C04-Recurrences/4.3.md
+++ b/C04-Recurrences/4.3.md
@@ -1,79 +1,80 @@
-### Exercises 4.3-1
-***
-Use the master method to give tight asymptotic bounds for the following recurrences.
-
-a.  = 4T\(n/2\)+n )
-
-b.  = 4T\(n/2\)+n^2 )
-
+### Exercises 4.3-1
+***
+Use the master method to give tight asymptotic bounds for the following recurrences.
+
+a.  = 4T\(n/2\)+n )
+
+b.  = 4T\(n/2\)+n^2 )
+
c.  = 4T\(n/2\)+n^3 )
-### `Answer`
-
-
-a.  )
-
-b.  )
-
-c.  )
-
-
-### Exercises 4.3-2
-***
-The recurrence T(n) = 7T (n/2)+n2 describes the running time of an algorithm A. A competing algorithm A′ has a running time of T′(n) = aT′(n/4) + n2. What is the largest integer value for a such that A′ is asymptotically faster than A?
-
-### `Answer`
-根据主定理,算法A的运行时间为 = \\Theta\(\\lg{7}\)\ \\approx n^{2.8} )
-
-A'的运行时间在a > 16时超过n^2,此时
-
- = \\Theta\(n^{\\log_{4}{a}}\) < \\lg{7} = \\log_{4}{49})
-
-所以最大值为48
-
-
-
-### Exercises 4.3-3
-***
-Use the master method to show that the solution to the binary-search recurrence T(n) = T (n/2)
+ Θ(1) is T(n) = Θ(lg n). (See Exercise 2.3-5 for a description of binary search.)
-### `Answer`
-
-
-so the solution is Θ(lgn).
-
-
-### Exercises 4.3-4
-***
-Can the master method be applied to the recurrence
- = 4T\(n/2\) + n^2 \\lg{n} )
+### `Answer`
+
+
+a.  )
+
+b.  )
+
+c.  )
+
+
+### Exercises 4.3-2
+***
+The recurrence T(n) = 7T (n/2)+n2 describes the running time of an algorithm A. A competing algorithm A′ has a running time of T′(n) = aT′(n/4) + n2. What is the largest integer value for a such that A′ is asymptotically faster than A?
+
+### `Answer`
+根据主定理,算法A的运行时间为 = \\Theta\(\\lg{7}\)\ \\approx n^{2.8} )
+
+A'的运行时间在a > 16时超过n^2,此时
+
+ = \\Theta\(n^{\\log_{4}{a}}\) < \\lg{7} = \\log_{4}{49})
+
+所以最大值为48
+
+
+
+### Exercises 4.3-3
+***
+Use the master method to show that the solution to the binary-search recurrence T(n) = T (n/2)
++ Θ(1) is T(n) = Θ(lg n). (See Exercise 2.3-5 for a description of binary search.)
+### `Answer`
+
+
+so the solution is Θ(lgn).
+
+
+### Exercises 4.3-4
+***
+Can the master method be applied to the recurrence
+ = 4T\(n/2\) + n^2 \\lg{n} )
Why or why not? Give an asymptotic upper bound for this recurrence.
-
-### `Answer`
-
-
-The problem is that it is not polynomially larger. The ratio 
- / n^{\\log_{b}{a}} = \\lg{n})
-is asymptotically less than
- for any positive constant
-
-
-### Exercises 4.3-5
-***
-Consider the regularity condition af (n/b) ≤ cf(n) for some constant c < 1, which is part of case 3 of the master theorem. Give an example of constants a ≥ 1 and b > 1 and a function f (n) that satisfies all the conditions in case 3 of the master theorem except the regularity condition.
-
-### `Answer`
-let
-
- a = 1
- b = 2
- f(n) = 2 - cos(n)
-
-我们需要证明
-
-}\) < cn \\\\ ~ \\Rightarrow c > \\frac{2- \\cos\(n/2\)}{2} \\\\ ~
-\\Rightarrow c > \\frac{3}{2}
-)
-
-***
-Follow [@louis1992](https://github.com/gzc) on github to help finish this task.
-
+
+### `Answer`
+
+
+The problem is that it is not polynomially larger. The ratio 
+ / n^{\\log_{b}{a}} = \\lg{n})
+is asymptotically less than
+ for any positive constant
+
+
+### Exercises 4.3-5
+***
+Consider the regularity condition af (n/b) ≤ cf(n) for some constant c < 1, which is part of case 3 of the master theorem. Give an example of constants a ≥ 1 and b > 1 and a function f (n) that satisfies all the conditions in case 3 of the master theorem except the regularity condition.
+
+### `Answer`
+let
+
+ a = 1
+ b = 2
+ f(n) = 2 - cos(n)
+
+我们需要证明
+
+}\) < cn \\\\ ~ \\Rightarrow c > \\frac{2- \\cos\(n/2\)}{2} \\\\ ~
+\\Rightarrow c > \\frac{3}{2}
+)
+
+***
+Follow [@louis1992](https://github.com/gzc) on github to help finish this task.
+
diff --git a/C04-Recurrences/4.4.md b/C04-Recurrences/4.4.md
index 7fabc0ad..2ed9d3b7 100644
--- a/C04-Recurrences/4.4.md
+++ b/C04-Recurrences/4.4.md
@@ -1,47 +1,47 @@
-### Exercises 4.4-1
-***
+### Exercises 4.4-1
+***
Give a simple and exact expression for nj in equation (4.12) for the case in which b is a positive integer instead of an arbitrary real number.
-### `Answer`
-
-
-
-### Exercises 4.4-2
-***
-Show that if  = \\Theta\(n^{\\log_{b}{a}}\) \\lg^k{n} ) , where k ≥ 0, then the master recurrence has solution
-Show that if  = \\Theta\(n^{\\log_{b}{a}}\) \\lg^{k+1}{n} ).
-
-### `Answer`
- = \\sum_{j = 0}^{\\log_{b}{n-1}} a^jf\(n/b^j\) \\\\ ~ f\(n/b^j\) = \\Theta\\Big\(\(n/b^j\)^{\\log_b{a}}\\lg^k\(n/b^j\)\\Big\) \\\\
-g\(n\) = \\Theta\\Big\(\\sum_{j=0}^{\\log_b{n}-1}a^j\\big\(\\frac{n}{b^j}\\big\)^{\\log_b{a}}\\lg^k\\big\(\\frac{n}{b^j}\\big\)\\Big\) = \\Theta\(A\) \\\\
-A = \\sum_{j=0}^{\\log_b{n}-1}a^j\\big\(\\frac{n}{b^j}\\big\)^{\\log_b{a}}\\lg^k\\frac{n}{b^j}
- = n^{\\log_b{a}}\\sum_{j=0}^{\\log_b{n}-1}\\Big\(\\frac{a}{b^{\\log_b{a}}}\\Big\)^j\\lg^k\\frac{n}{b^j}
- = n^{\\log_b{a}}\\sum_{j=0}^{\\log_b{n}-1}\\lg^k\\frac{n}{b^j}
- = n^{\\log_b{a}}B \\\\
-\\lg^k\\frac{n}{d} = \(\\lg{n} - \\lg{d}\)^k = \\lg^k{n} + o\(\\lg^k{n}\) \\\\
-B = \\sum_{j=0}^{\\log_b{n}-1}\\lg^k\\frac{n}{b^j}
- = \\sum_{j=0}^{\\log_b{n}-1}\\Big\(\\lg^k{n} - o\(\\lg^k{n}\)\\Big\)
- = \\log_b{n}\\lg^k{n} + \\log_b{n} \\cdot o\(\\lg^k{n}\)
- = \\Theta\(\\log_b{n}\\lg^k{n}\)
- = \\Theta\(\\lg^{k+1}{n}\) \\\\
-g\(n\) = \\Theta\(A\) = \\Theta\(n^{\\log_b{a}}B\) = \\Theta\(n^{\\log_b{a}}\\lg^{k+1}{n}\)
-)
-
-
-### Exercises 4.3-3
-***
+### `Answer`
+
+
+
+### Exercises 4.4-2
+***
+Show that if  = \\Theta\(n^{\\log_{b}{a}}\) \\lg^k{n} ) , where k ≥ 0, then the master recurrence has solution
+Show that if  = \\Theta\(n^{\\log_{b}{a}}\) \\lg^{k+1}{n} ).
+
+### `Answer`
+ = \\sum_{j = 0}^{\\log_{b}{n-1}} a^jf\(n/b^j\) \\\\ ~ f\(n/b^j\) = \\Theta\\Big\(\(n/b^j\)^{\\log_b{a}}\\lg^k\(n/b^j\)\\Big\) \\\\
+g\(n\) = \\Theta\\Big\(\\sum_{j=0}^{\\log_b{n}-1}a^j\\big\(\\frac{n}{b^j}\\big\)^{\\log_b{a}}\\lg^k\\big\(\\frac{n}{b^j}\\big\)\\Big\) = \\Theta\(A\) \\\\
+A = \\sum_{j=0}^{\\log_b{n}-1}a^j\\big\(\\frac{n}{b^j}\\big\)^{\\log_b{a}}\\lg^k\\frac{n}{b^j}
+ = n^{\\log_b{a}}\\sum_{j=0}^{\\log_b{n}-1}\\Big\(\\frac{a}{b^{\\log_b{a}}}\\Big\)^j\\lg^k\\frac{n}{b^j}
+ = n^{\\log_b{a}}\\sum_{j=0}^{\\log_b{n}-1}\\lg^k\\frac{n}{b^j}
+ = n^{\\log_b{a}}B \\\\
+\\lg^k\\frac{n}{d} = \(\\lg{n} - \\lg{d}\)^k = \\lg^k{n} + o\(\\lg^k{n}\) \\\\
+B = \\sum_{j=0}^{\\log_b{n}-1}\\lg^k\\frac{n}{b^j}
+ = \\sum_{j=0}^{\\log_b{n}-1}\\Big\(\\lg^k{n} - o\(\\lg^k{n}\)\\Big\)
+ = \\log_b{n}\\lg^k{n} + \\log_b{n} \\cdot o\(\\lg^k{n}\)
+ = \\Theta\(\\log_b{n}\\lg^k{n}\)
+ = \\Theta\(\\lg^{k+1}{n}\) \\\\
+g\(n\) = \\Theta\(A\) = \\Theta\(n^{\\log_b{a}}B\) = \\Theta\(n^{\\log_b{a}}\\lg^{k+1}{n}\)
+)
+
+
+### Exercises 4.3-3
+***
Show that case 3 of the master theorem is overstated, in the sense that the regularity condition af(n/b) ≤ cf(n) for some constant c < 1 implies that there exists a constant  = \\Omega\(n^{\\log_b{a+\\epsilon}}\)).
-### `Answer`
-根据case3,我们有 \\le cf\(n\) ~~~~ a \\ge 1,b \\ge 1 c < 1 )
-
-变形一下
-
- \\ge kf\(n\) ~~~where ~ k = a/c > a \\\\ \\Rightarrow f\(b^i\) \\ge k^i f\(1\) \\\\
-if~~ n = b^i, i = \\log_b{n}, then f\(n\) \\ge k^{\\log_b{n}}f\(1\) \\\\
-k^{\\log_b{n}} = n^{\\log_b{k}} ~ and ~ \\log_b{k} \\ge \\log_b{a} ~so~ \\log_b{k} = \\log_b{a} + \\epsilon
-)
-
-***
-Follow [@louis1992](https://github.com/gzc) on github to help finish this task.
-
+### `Answer`
+根据case3,我们有 \\le cf\(n\) ~~~~ a \\ge 1,b \\ge 1 c < 1 )
+
+变形一下
+
+ \\ge kf\(n\) ~~~where ~ k = a/c > a \\\\ \\Rightarrow f\(b^i\) \\ge k^i f\(1\) \\\\
+if~~ n = b^i, i = \\log_b{n}, then f\(n\) \\ge k^{\\log_b{n}}f\(1\) \\\\
+k^{\\log_b{n}} = n^{\\log_b{k}} ~ and ~ \\log_b{k} \\ge \\log_b{a} ~so~ \\log_b{k} = \\log_b{a} + \\epsilon
+)
+
+***
+Follow [@louis1992](https://github.com/gzc) on github to help finish this task.
+
diff --git a/C04-Recurrences/problem.md b/C04-Recurrences/problem.md
index 2f693345..c49eb793 100644
--- a/C04-Recurrences/problem.md
+++ b/C04-Recurrences/problem.md
@@ -1,231 +1,317 @@
-### Problems 1 : Recurrence examples
-***
-Throughout this book, we assume that parameter passing during procedure calls takes constant time, even if an N-element array is being passed. This assumption is valid in most systems because a pointer to the array is passed, not the array itself. This problem examines the implications of three parameter-passing strategies:
-
-1. An array is passed by pointer. Time = Θ(1).
-
2. An array is passed by copying. Time = Θ(N), where N is the size of the array.
-
3. An array is passed by copying only the subrange that might be accessed by the called
procedure. Time = Θ(q - p + 1) if the subarray A[p...q] is passed.
-
**a.** Consider the recursive binary search algorithm for finding a number in a sorted array (see Exercise 2.3-5). Give recurrences for the worst-case running times of binary search when arrays are passed using each of the three methods above, and give good upper bounds on the solutions of the recurrences. Let N be the size of the original problem and n be the size of a subproblem.
**b.** Redo part (a) for the MERGE-SORT algorithm from Section 2.3.1.
-
-### `Answer`
-1. Θ(n^4)
-
-2. Θ(n)
-
-3. Θ(n^2lgn)
-
-4. Θ(n^2)
-
-5.  )
-
-6. 
-
-7. Θ(n^3)
-
-
-
-### Problems 2 : Finding the missing integer
-***
-An array A[1...n] contains all the integers from 0 to n except one. It would be easy to determine the missing integer in O(n) time by using an auxiliary array B[0...n] to record which numbers appear in A. In this problem, however, we cannot access an entire integer in A with a single operation. The elements of A are represented in binary, and the only operation we can use to access them is "fetch the jth bit of A[i]," which takes constant time.
-
Show that if we use only this operation, we can still determine the missing integer in O(n) time.
-
-### `Answer`
-* 00000
-* 00001
-* 00010
-* 00011
-* 00101
-* 00110
-* 00111
-* 01000
-
-[code](./exercise_code/findMissing.py)
-
-我们用上面的8个数字当作例子,[0,8]缺4.
-
- 1.第一次迭代发现最末位1出现4次0出现3次,所以missnum的最后一位是4,排除掉末位为1的数字
- 2.然后一次次迭代
-
-### Problems 3 : Parameter-passing costs
Throughout this book, we assume that parameter passing during procedure calls takes constant time, even if an N-element array is being passed. This assumption is valid in most systems because a pointer to the array is passed, not the array itself. This problem examines the implications of three parameter-passing strategies:
-
1. An array is passed by pointer. Time = Θ(1).
-
2. An array is passed by copying. Time = Θ(N), where N is the size of the array.
-
3. An array is passed by copying only the subrange that might be accessed by the calledprocedure. Time = Θ(q - p + 1) if the subarray A[p...q] is passed.
-
**a.** Consider the recursive binary search algorithm for finding a number in a sorted array (see Exercise 2.3-5). Give recurrences for the worst-case running times of binary search when arrays are passed using each of the three methods above, and give good upper bounds on the solutions of the recurrences. Let N be the size of the original problem and n be the size of a subproblem.
-
**b.** Redo part (a) for the MERGE-SORT algorithm from Section 2.3.1.
-
### `Answer`
-
**a.**
-
1. T(n) = T(n/2) + 2, O(lgN)
-
2. T(n) = T(n/2) + N, O(NlgN)
-
3. T(n) = T(n/2) + n, O(N)
-
**b.**
-
-1. T(n) = 2T(n/2) + n + 2, O(NlgN)
-
2. T(n) = 2T(n/2) + n + 2N, O(2^{lgN})
-
3. T(N) = 2T(n/2) + n + 2n, O(Nlg^**2**N)
-
### Problems 4 : More recurrence examples
-
**a.**
 = 3T\(n/2\) + \\lg{n} \\\\
by~applying~master~method~\\Theta\(n^{\\log_3{4}}\))
-***
**b.**
 = 5T\(n/5\) + n/\\lg{n} \\\\
T\(n\) = 5T\(n/5\) + \\frac{n}{\\lg{n}} = 25T\(n/25\) + 5\\frac{n/5}{\\lg\(n/5\)} + \\frac{n}{\\lg{n}} = 25T\(n/25\) + \\frac{n}{\\lg{n}-\\lg{5}} + \\frac{n}{\\lg{n}} = nT\(1\)+\\sum_{i = 0}^{\\lg{n}-1}\\frac{n}{\\lg{n}-i\\lg{5}} = nT\(1\)+n\\sum_{i = 1}^{\\lg{n}}\\frac{1}{\\lg{n}} = \\Theta\(n\\lg{\\lg{n}}\))
-***
**c.**
 = 4T\(n/2\)+n^2\\sqrt{n} \\\\
-by ~applying~master~method~ \\Theta\(n^2\\sqrt{n}\) )
-***
-**d.**
 = 3T\(n/3+5\) + n/2 \\\\
by ~applying~master~method~ \\Theta\(n\\lg{n}\)
)
-***
**e.**
The same as b
-***
**f.**
 = T\(n/2\) + T\(n/4\) + T\(n/8\) + n\\\\
Let's ~ guess ~ \\Theta\(n\) \\\\
-T\(n\) = cn/2 + cn/4 + cn/8 \\le cn = O\(n\) \\\\
-T\(n\) = cn/2 + cn/4 + cn/8 \\ge cn = \\Omega\(n\)
)
-***
**g.**
 = T\(n-1\) + 1/n \\\\
-T\(n\) = \\sum_{i = 1}^{n}\\frac{1}{i} = \\Theta\(lg{n}\)
-)
-***
-**h.**
 = T\(n-1\) + \\lg{n} \\\\
-T\(n\) = \\sum_{i=1}^{n}\\lg{i} = \\lg{n!} = \\Theta\(n\\lg{n}\) ~ remember ~we ~prove ~it ~in ~section~1
)
-***
**i.**
 = T\(n-2\) + 2\\lg{n} ~ The ~same
-)
-***
-**j.**
 = \\sqrt{n}T\(\\sqrt{n}\)+n \\\\
-Let's ~ guess ~ \\Theta\(cn\\lg{\\lg{n}}\) \\\\
-T\(n\) \\le \\sqrt{n}c\\sqrt{n}\\lg{\\lg{\\sqrt{n}}}+n \\\\
-= cn\\lg{\\lg{\\sqrt{n}}}+n \\\\
-= cn\\lg{\\frac{\\lg{n}}{2}}+n \\\\
-= cn\\lg{\\lg{n}} - cn\\lg{2}+n \\\\
-= cn\\lg{\\lg{n}} + \(1-c\)n \\\\
-\\le cn\\lg{\\lg{n}} ~~~~~~~~~ if ~ c > 1 \\\\
-= \\Theta\(cn\\lg{\\lg{n}}\)
)
-
-
-### Problems 5 : Fibonacci numbers
-***
-This problem develops properties of the Fibonacci numbers, which are defined by recurrence (3.21). We shall use the technique of generating functions to solve the Fibonacci recurrence. Define the generating function (or formal power series) F as
-
- = \\sum_{i=0}^{\\infty}F\_iz^i \\\\
-= 0 + z + z^2 + 2z^3 + 3z^4 + 5z^5 + 8z^6 + 13z^7 + 21z^8 + \\ldots \\\\
-where~ F_i ~is ~the~ ith ~Fibonacci~ number.)
-
-a. Show that  = z + z\mathcal{F}\(z\) + z^2\mathcal{F}\(z\))
-
-b. Show that  = \\frac{z}{1-z-z^2} = \\frac{z}{\(1-\\phi{z}\)\(1-\\widehat\\phi{z}\)} = \\frac{1}{\\sqrt{5}}\(\\frac{1}{1-\\phi{z}}-\\frac{1}{1-\\widehat\\phi{z}}\)
-)
-
-c. Show that Show that  = \\sum_{i=0}^{\\infty}\\frac{1}{\\sqrt{5}}\(\\phi^i - \\widehat\\phi^i\)z^i )
-
-d. Prove that  = \\phi^i/\\sqrt{5}) for i > 0 , rounded to the nearest integer.
-
-e. Prove that  for i ≥ 0.
-
-### `Answer`
-**a.**
-
- + z^2\mathcal{F}\(z\) \\\\
-= z + z\\sum_{i=0}^{\\infty}F\_iz^i + z^2\\sum_{i=0}^{\\infty}F\_iz^i \\\\
-= z + z\\sum_{i=1}^{\\infty}F\_{i-1}z^i + z^2\\sum_{i=2}^{\\infty}F\_{i-2}z^i \\\\
-= z + F_1z + \\sum_{i=2}^{\\infty}\(F_{i-1}+F_{i-2}\)z^i \\\\
-= z + F_1z + \\sum_{i=2}^{\\infty}F_iz^i \\\\
-= \\mathcal{F}\(z\))
-
-**b.**
-这个结论的证明还是很straight-forward的,就不写公式啦.
-
-**c.**
-
- = \\frac{1}{\\sqrt5}\\Big\(\\frac{1}{1 - \\phi z} - \\frac{1}{1 - \\hat\\phi z}\\Big\) = \\frac{1}{\\sqrt5}\\Big\(\\sum_{i=0}^{\\infty}\\phi^i z^i - \\sum_{i=0}^{\\infty}\\hat{\\phi}^i z^i\\Big\)= \\sum_{i=0}^{\\infty}\\frac{1}{\\sqrt5}\(\\phi^i - \\hat{\\phi}^i\) z^i
-)
-
-**d.**
-
- = \\sum_{i=0}^{\\infty}\\alpha_iz^i \\quad\\text{ where } \\alpha_i = \\frac{\\phi^i - \\hat{\\phi}^i}{\\sqrt5} \\\\
-\\quad\\text{ so we have } \\alpha_i = F_i \\\\
-F_i = \\frac{\\phi^i - \\hat{\\phi}^i}{\\sqrt5} = \\frac{\\phi^i}{\\sqrt5} - \\frac{\\hat{\\phi}^i}{\\sqrt5} \\\\
-\\quad\\text{because } |\\hat\\phi| < 1 \\quad\\text{, so } \\frac{|\\hat\\phi^i|}{\\sqrt{5}} < 0.5
-)
-
-**e.**
-[we had prove it previously](https://github.com/gzc/CLRS/blob/master/C03-Growth-of-Functions/3.2.md#exercises-32-7)
-
-### Problems 6 : VLSI chip testing
-***
-Professor Diogenes has n supposedly identical VLSI[1] chips that in principle are capable of testing each other. The professor's test jig accommodates two chips at a time. When the jig is loaded, each chip tests the other and reports whether it is good or bad. A good chip always reports accurately whether the other chip is good or bad, but the answer of a bad chip cannot be trusted. Thus, the four possible outcomes of a test are as follows:
-
Chip A says | Chip B says | Conclusion
:----:|:----:|:----:
B is good | A is good | both are good, or both are bad
B is good | A is bad | at least one is bad
B is bad | A is good | at least one is bad
+### Problems 1 : Recurrence examples
+***
+Throughout this book, we assume that parameter passing during procedure calls takes constant time, even if an N-element array is being passed. This assumption is valid in most systems because a pointer to the array is passed, not the array itself. This problem examines the implications of three parameter-passing strategies:
+
+1. An array is passed by pointer. Time = Θ(1).
+
+2. An array is passed by copying. Time = Θ(N), where N is the size of the array.
+
+3. An array is passed by copying only the subrange that might be accessed by the called
+procedure. Time = Θ(q - p + 1) if the subarray A[p...q] is passed.
+
+
+**a.** Consider the recursive binary search algorithm for finding a number in a sorted array (see Exercise 2.3-5). Give recurrences for the worst-case running times of binary search when arrays are passed using each of the three methods above, and give good upper bounds on the solutions of the recurrences. Let N be the size of the original problem and n be the size of a subproblem.
+
+**b.** Redo part (a) for the MERGE-SORT algorithm from Section 2.3.1.
+
+### `Answer`
+1. Θ(n^4)
+
+2. Θ(n)
+
+3. Θ(n^2lgn)
+
+4. Θ(n^2)
+
+5.  )
+
+6. 
+
+7. Θ(n^3)
+
+
+
+### Problems 2 : Finding the missing integer
+***
+An array A[1...n] contains all the integers from 0 to n except one. It would be easy to determine the missing integer in O(n) time by using an auxiliary array B[0...n] to record which numbers appear in A. In this problem, however, we cannot access an entire integer in A with a single operation. The elements of A are represented in binary, and the only operation we can use to access them is "fetch the jth bit of A[i]," which takes constant time.
+
+Show that if we use only this operation, we can still determine the missing integer in O(n) time.
+
+### `Answer`
+* 00000
+* 00001
+* 00010
+* 00011
+* 00101
+* 00110
+* 00111
+* 01000
+
+[code](./exercise_code/findMissing.py)
+
+我们用上面的8个数字当作例子,[0,8]缺4.
+
+ 1.第一次迭代发现最末位1出现4次0出现3次,所以missnum的最后一位是4,排除掉末位为1的数字
+ 2.然后一次次迭代
+
+### Problems 3 : Parameter-passing costs
+
+Throughout this book, we assume that parameter passing during procedure calls takes constant time, even if an N-element array is being passed. This assumption is valid in most systems because a pointer to the array is passed, not the array itself. This problem examines the implications of three parameter-passing strategies:
+
+
+1. An array is passed by pointer. Time = Θ(1).
+
+
+2. An array is passed by copying. Time = Θ(N), where N is the size of the array.
+
+
+3. An array is passed by copying only the subrange that might be accessed by the calledprocedure. Time = Θ(q - p + 1) if the subarray A[p...q] is passed.
+
+**a.** Consider the recursive binary search algorithm for finding a number in a sorted array (see Exercise 2.3-5). Give recurrences for the worst-case running times of binary search when arrays are passed using each of the three methods above, and give good upper bounds on the solutions of the recurrences. Let N be the size of the original problem and n be the size of a subproblem.
+
+**b.** Redo part (a) for the MERGE-SORT algorithm from Section 2.3.1.
+
+
+### `Answer`
+
+**a.**
+
+1. T(n) = T(n/2) + 2, O(lgN)
+
+2. T(n) = T(n/2) + N, O(NlgN)
+
+3. T(n) = T(n/2) + n, O(N)
+
+
+**b.**
+
+1. T(n) = 2T(n/2) + n + 2, O(NlgN)
+
+2. T(n) = 2T(n/2) + n + 2N, O(2^{lgN})
+
+3. T(N) = 2T(n/2) + n + 2n, O(Nlg^**2**N)
+
+
+### Problems 4 : More recurrence examples
+
+**a.**
+ = 3T\(n/2\) + \\lg{n} \\\\
+by~applying~master~method~\\Theta\(n^{\\log_3{4}}\))
+***
+**b.**
+ = 5T\(n/5\) + n/\\lg{n} \\\\
+T\(n\) = 5T\(n/5\) + \\frac{n}{\\lg{n}} = 25T\(n/25\) + 5\\frac{n/5}{\\lg\(n/5\)} + \\frac{n}{\\lg{n}} = 25T\(n/25\) + \\frac{n}{\\lg{n}-\\lg{5}} + \\frac{n}{\\lg{n}} = nT\(1\)+\\sum_{i = 0}^{\\lg{n}-1}\\frac{n}{\\lg{n}-i\\lg{5}} = nT\(1\)+n\\sum_{i = 1}^{\\lg{n}}\\frac{1}{\\lg{n}} = \\Theta\(n\\lg{\\lg{n}}\))
+***
+**c.**
+ = 4T\(n/2\)+n^2\\sqrt{n} \\\\
+by ~applying~master~method~ \\Theta\(n^2\\sqrt{n}\) )
+***
+**d.**
+ = 3T\(n/3+5\) + n/2 \\\\
+by ~applying~master~method~ \\Theta\(n\\lg{n}\)
+)
+***
+**e.**
+The same as b
+***
+**f.**
+ = T\(n/2\) + T\(n/4\) + T\(n/8\) + n\\\\
+Let's ~ guess ~ \\Theta\(n\) \\\\
+T\(n\) = cn/2 + cn/4 + cn/8 \\le cn = O\(n\) \\\\
+T\(n\) = cn/2 + cn/4 + cn/8 \\ge cn = \\Omega\(n\)
+)
+***
+**g.**
+ = T\(n-1\) + 1/n \\\\
+T\(n\) = \\sum_{i = 1}^{n}\\frac{1}{i} = \\Theta\(lg{n}\)
+)
+***
+**h.**
+ = T\(n-1\) + \\lg{n} \\\\
+T\(n\) = \\sum_{i=1}^{n}\\lg{i} = \\lg{n!} = \\Theta\(n\\lg{n}\) ~ remember ~we ~prove ~it ~in ~section~1
+)
+***
+**i.**
+ = T\(n-2\) + 2\\lg{n} ~ The ~same
+)
+***
+**j.**
+ = \\sqrt{n}T\(\\sqrt{n}\)+n \\\\
+Let's ~ guess ~ \\Theta\(cn\\lg{\\lg{n}}\) \\\\
+T\(n\) \\le \\sqrt{n}c\\sqrt{n}\\lg{\\lg{\\sqrt{n}}}+n \\\\
+= cn\\lg{\\lg{\\sqrt{n}}}+n \\\\
+= cn\\lg{\\frac{\\lg{n}}{2}}+n \\\\
+= cn\\lg{\\lg{n}} - cn\\lg{2}+n \\\\
+= cn\\lg{\\lg{n}} + \(1-c\)n \\\\
+\\le cn\\lg{\\lg{n}} ~~~~~~~~~ if ~ c > 1 \\\\
+= \\Theta\(cn\\lg{\\lg{n}}\)
+)
+
+
+### Problems 5 : Fibonacci numbers
+***
+This problem develops properties of the Fibonacci numbers, which are defined by recurrence (3.21). We shall use the technique of generating functions to solve the Fibonacci recurrence. Define the generating function (or formal power series) F as
+
+ = \\sum_{i=0}^{\\infty}F\_iz^i \\\\
+= 0 + z + z^2 + 2z^3 + 3z^4 + 5z^5 + 8z^6 + 13z^7 + 21z^8 + \\ldots \\\\
+where~ F_i ~is ~the~ ith ~Fibonacci~ number.)
+
+a. Show that  = z + z\mathcal{F}\(z\) + z^2\mathcal{F}\(z\))
+
+b. Show that  = \\frac{z}{1-z-z^2} = \\frac{z}{\(1-\\phi{z}\)\(1-\\widehat\\phi{z}\)} = \\frac{1}{\\sqrt{5}}\(\\frac{1}{1-\\phi{z}}-\\frac{1}{1-\\widehat\\phi{z}}\)
+)
+
+c. Show that Show that  = \\sum_{i=0}^{\\infty}\\frac{1}{\\sqrt{5}}\(\\phi^i - \\widehat\\phi^i\)z^i )
+
+d. Prove that  = \\phi^i/\\sqrt{5}) for i > 0 , rounded to the nearest integer.
+
+e. Prove that  for i ≥ 0.
+
+### `Answer`
+**a.**
+
+ + z^2\mathcal{F}\(z\) \\\\
+= z + z\\sum_{i=0}^{\\infty}F\_iz^i + z^2\\sum_{i=0}^{\\infty}F\_iz^i \\\\
+= z + z\\sum_{i=1}^{\\infty}F\_{i-1}z^i + z^2\\sum_{i=2}^{\\infty}F\_{i-2}z^i \\\\
+= z + F_1z + \\sum_{i=2}^{\\infty}\(F_{i-1}+F_{i-2}\)z^i \\\\
+= z + F_1z + \\sum_{i=2}^{\\infty}F_iz^i \\\\
+= \\mathcal{F}\(z\))
+
+**b.**
+这个结论的证明还是很straight-forward的,就不写公式啦.
+
+**c.**
+
+ = \\frac{1}{\\sqrt5}\\Big\(\\frac{1}{1 - \\phi z} - \\frac{1}{1 - \\hat\\phi z}\\Big\) = \\frac{1}{\\sqrt5}\\Big\(\\sum_{i=0}^{\\infty}\\phi^i z^i - \\sum_{i=0}^{\\infty}\\hat{\\phi}^i z^i\\Big\)= \\sum_{i=0}^{\\infty}\\frac{1}{\\sqrt5}\(\\phi^i - \\hat{\\phi}^i\) z^i
+)
+
+**d.**
+
+ = \\sum_{i=0}^{\\infty}\\alpha_iz^i \\quad\\text{ where } \\alpha_i = \\frac{\\phi^i - \\hat{\\phi}^i}{\\sqrt5} \\\\
+\\quad\\text{ so we have } \\alpha_i = F_i \\\\
+F_i = \\frac{\\phi^i - \\hat{\\phi}^i}{\\sqrt5} = \\frac{\\phi^i}{\\sqrt5} - \\frac{\\hat{\\phi}^i}{\\sqrt5} \\\\
+\\quad\\text{because } |\\hat\\phi| < 1 \\quad\\text{, so } \\frac{|\\hat\\phi^i|}{\\sqrt{5}} < 0.5
+)
+
+**e.**
+[we had prove it previously](https://github.com/gzc/CLRS/blob/master/C03-Growth-of-Functions/3.2.md#exercises-32-7)
+
+### Problems 6 : VLSI chip testing
+***
+Professor Diogenes has n supposedly identical VLSI[1] chips that in principle are capable of testing each other. The professor's test jig accommodates two chips at a time. When the jig is loaded, each chip tests the other and reports whether it is good or bad. A good chip always reports accurately whether the other chip is good or bad, but the answer of a bad chip cannot be trusted. Thus, the four possible outcomes of a test are as follows:
+
+Chip A says | Chip B says | Conclusion
+:----:|:----:|:----:
+B is good | A is good | both are good, or both are bad
+B is good | A is bad | at least one is bad
+B is bad | A is good | at least one is bad
B is bad | A is bad | at least one is bad
-
a. Show that if more than n/2 chips are bad, the professor cannot necessarily determine which chips are good using any strategy based on this kind of pairwise test. Assume that the bad chips can conspire to fool the professor.
-
b. Consider the problem of finding a single good chip from among n chips, assuming that
more than n/2 of the chips are good. Show that ⌊n/2⌋ pairwise tests are sufficient to
reduce the problem to one of nearly half the size.
-
c. Show that the good chips can be identified with Θ(n) pairwise tests, assuming that
more than n/2 of the chips are good. Give and solve the recurrence that describes the number of tests.
-
-
### `Answer`
a. 如果超过一半是坏的,那么我们可以从这些坏的中取出一组数量和好的一样多的,他们的表现能和好的一样.
-
b. 将所有的芯片两两配对,如果报告是both are good or bad,那么就从中随机选一个留下来,否则全部扔掉. 一直这样递归下去,最后剩下的是好的.
-
c. T(n) = T(n/2)+n/2,是Θ(n)的.
-
### Problems 7 : Monge arrays
-***
-An m × n array A of real numbers is a **Monge array** if for all i, j, k, and l such that 1 ≤ i < k ≤ m and 1 ≤ j < l ≤ n, we have
-
A[i, j] + A[k, l] ≤ A[i, l] + A[k, j].
-
In other words, whenever we pick two rows and two columns of a Monge array and consider the four elements at the intersections of the rows and the columns, the sum of the upper-left and lower-right elements is less or equal to the sum of the lower-left and upper-right elements. For example, the following array is Monge:
-

-
-a. Prove that an array is Monge if and only if for all i = 1,2,...,m-1 and j = 1,2,...,n- 1, we have
A[i, j] + A[i + 1, j + 1] ≤ A[i, j + 1] + A[i + 1, j].
Note (For the "only if" part, use induction separately on rows and columns.)
-
b. The following array is not Monge. Change one element in order to make it Monge.
-
-
-
-c. Let f(i) be the index of the column containing the leftmost minimum element of row i. Prove that f(1) ≤ f(2) ≤ ··· ≤ f(m) for any m × n Monge array.
-
-d. Here is a description of a divide-and-conquer algorithm that computes the left-most minimum element in each row of an m × n Monge array A:
Construct a submatrix A′ of A consisting of the even-numbered rows of A. Recursively determine the leftmost minimum for each row of A′. Then compute the leftmost minimum in the odd-numbered rows of A.
Explain how to compute the leftmost minimum in the odd-numbered rows of A (given that the leftmost minimum of the even-numbered rows is known) in O(m + n) time.
-
e. Write the recurrence describing the running time of the algorithm described in part (d). Show that its solution is O(m + n log m).
### `Answer`
-**a.**
-
-
-***
-
-
-**b.**
-
-
-
-**c.** **反证法**
-
-如果i < j,f(i) >= f(j)
-
-A[i,f(j)]+A[j,f(i))] <= A[i,f(i)]+A[j,f(j)] 但是A[i,f(i)]和A[j,f(j)]是两行最小的元素,等式不成立.
-
-**d.**根据c可以知道第i行的左端最小值落在f(i-1)和f(i+1)之间. 总共有n/2个奇数行,总共需要比较m次,所以是O(m+n).
-
-**e.**
-T(m) = T(m/2) + cn + dm = O(nlgm + m)
-
-[code](./exercise_code/findIndex.py)
-
-
-
-***
-Follow [@louis1992](https://github.com/gzc) on github to help finish this task.
-
+
+
+a. Show that if more than n/2 chips are bad, the professor cannot necessarily determine which chips are good using any strategy based on this kind of pairwise test. Assume that the bad chips can conspire to fool the professor.
+
+b. Consider the problem of finding a single good chip from among n chips, assuming that
+more than n/2 of the chips are good. Show that ⌊n/2⌋ pairwise tests are sufficient to
+reduce the problem to one of nearly half the size.
+
+c. Show that the good chips can be identified with Θ(n) pairwise tests, assuming that
+more than n/2 of the chips are good. Give and solve the recurrence that describes the number of tests.
+
+
+### `Answer`
+a. 如果超过一半是坏的,那么我们可以从这些坏的中取出一组数量和好的一样多的,他们的表现能和好的一样.
+
+b. 将所有的芯片两两配对,如果报告是both are good or bad,那么就从中随机选一个留下来,否则全部扔掉. 一直这样递归下去,最后剩下的是好的.
+
+c. T(n) = T(n/2)+n/2,是Θ(n)的.
+
+
+### Problems 7 : Monge arrays
+***
+An m × n array A of real numbers is a **Monge array** if for all i, j, k, and l such that 1 ≤ i < k ≤ m and 1 ≤ j < l ≤ n, we have
+
+A[i, j] + A[k, l] ≤ A[i, l] + A[k, j].
+
+In other words, whenever we pick two rows and two columns of a Monge array and consider the four elements at the intersections of the rows and the columns, the sum of the upper-left and lower-right elements is less or equal to the sum of the lower-left and upper-right elements. For example, the following array is Monge:
+
+
+
+a. Prove that an array is Monge if and only if for all i = 1,2,...,m-1 and j = 1,2,...,n- 1, we have
+A[i, j] + A[i + 1, j + 1] ≤ A[i, j + 1] + A[i + 1, j].
+Note (For the "only if" part, use induction separately on rows and columns.)
+
+
+b. The following array is not Monge. Change one element in order to make it Monge.
+
+
+
+c. Let f(i) be the index of the column containing the leftmost minimum element of row i. Prove that f(1) ≤ f(2) ≤ ··· ≤ f(m) for any m × n Monge array.
+
+d. Here is a description of a divide-and-conquer algorithm that computes the left-most minimum element in each row of an m × n Monge array A:
+Construct a submatrix A′ of A consisting of the even-numbered rows of A. Recursively determine the leftmost minimum for each row of A′. Then compute the leftmost minimum in the odd-numbered rows of A.
+Explain how to compute the leftmost minimum in the odd-numbered rows of A (given that the leftmost minimum of the even-numbered rows is known) in O(m + n) time.
+
+
+e. Write the recurrence describing the running time of the algorithm described in part (d). Show that its solution is O(m + n log m).
+
+### `Answer`
+**a.**
+
+
+***
+
+
+**b.**
+
+
+
+**c.** **反证法**
+
+如果i < j,f(i) >= f(j)
+
+A[i,f(j)]+A[j,f(i))] <= A[i,f(i)]+A[j,f(j)] 但是A[i,f(i)]和A[j,f(j)]是两行最小的元素,等式不成立.
+
+**d.**根据c可以知道第i行的左端最小值落在f(i-1)和f(i+1)之间. 总共有n/2个奇数行,总共需要比较m次,所以是O(m+n).
+
+**e.**
+T(m) = T(m/2) + cn + dm = O(nlgm + m)
+
+[code](./exercise_code/findIndex.py)
+
+
+
+***
+Follow [@louis1992](https://github.com/gzc) on github to help finish this task.
+
diff --git a/C05-Probabilistic-Analysis-and-Randomized-Algorithms/5.2.md b/C05-Probabilistic-Analysis-and-Randomized-Algorithms/5.2.md
index 89e348a6..e889268b 100644
--- a/C05-Probabilistic-Analysis-and-Randomized-Algorithms/5.2.md
+++ b/C05-Probabilistic-Analysis-and-Randomized-Algorithms/5.2.md
@@ -1,52 +1,52 @@
-### Exercises 5.2-1
-***
+### Exercises 5.2-1
+***
In HIRE-ASSISTANT, assuming that the candidates are presented in a random order, what is the probability that you will hire exactly one time? What is the probability that you will hire exactly n times?
-### `Answer`
-分别是1/n和1/n!
-
-
-### Exercises 5.2-2
-***
+### `Answer`
+分别是1/n和1/n!
+
+
+### Exercises 5.2-2
+***
In HIRE-ASSISTANT, assuming that the candidates are presented in a random order, what is the probability that you will hire exactly twice?
-
-### `Answer`
-如果第一个雇员的质量是k,那么质量高于k的雇员都必须在质量最高的雇员后面.
-
-假设有n个雇员,质量分别是1,2,...,n.当第一个质量为k时,只雇佣2次的概率p = 1/(n-k).因为有n-k个质量比k高的,而且必须要最高的那个在前.而第一个质量为k的概率是1/n.所以
-
-
-
-### Exercises 5.2-3
-***
-Use indicator random variables to compute the expected value of the sum of n dice.
-
-### `Answer`
-Expectation of a single die
-
- = \\frac{1+2+3+4+5+6}{6} = 3.5 )
-
-Expectation of N dies
-
- = \\sum_{i = 1}^{n} E\(X_i\) = 3.5n )
-
-### Exercises 5.2-4
-***
-Use indicator random variables to solve the following problem, which is known as the **hat- check problem**. Each of n customers gives a hat to a hat-check person at a restaurant. The hat- check person gives the hats back to the customers in a random order. What is the expected number of customers that get back their own hat?
-
-### `Answer`
-每个人都是1/n的期望,所以总的是1
-
-### Exercises 5.2-5
-***
-Let A[1...n] be an array of n distinct numbers. If i < j and A[i] > A[j], then the pair (i, j) is called an inversion of A. (See Problem 2-4 for more on inversions.) Suppose that each element of A is chosen randomly, independently, and uniformly from the range 1 through n. Use indicator random variables to compute the expected number of inversions.
-
-### `Answer`
-最简单的解法如下:
-
-因为概率是一样的,所以出现正序和逆序是等量的,总共有n(n-1)/2对,所以期望是n(n-1)/4对.
-
-
-***
-Follow [@louis1992](https://github.com/gzc) on github to help finish this task.
-
+
+### `Answer`
+如果第一个雇员的质量是k,那么质量高于k的雇员都必须在质量最高的雇员后面.
+
+假设有n个雇员,质量分别是1,2,...,n.当第一个质量为k时,只雇佣2次的概率p = 1/(n-k).因为有n-k个质量比k高的,而且必须要最高的那个在前.而第一个质量为k的概率是1/n.所以
+
+
+
+### Exercises 5.2-3
+***
+Use indicator random variables to compute the expected value of the sum of n dice.
+
+### `Answer`
+Expectation of a single die
+
+ = \\frac{1+2+3+4+5+6}{6} = 3.5 )
+
+Expectation of N dies
+
+ = \\sum_{i = 1}^{n} E\(X_i\) = 3.5n )
+
+### Exercises 5.2-4
+***
+Use indicator random variables to solve the following problem, which is known as the **hat- check problem**. Each of n customers gives a hat to a hat-check person at a restaurant. The hat- check person gives the hats back to the customers in a random order. What is the expected number of customers that get back their own hat?
+
+### `Answer`
+每个人都是1/n的期望,所以总的是1
+
+### Exercises 5.2-5
+***
+Let A[1...n] be an array of n distinct numbers. If i < j and A[i] > A[j], then the pair (i, j) is called an inversion of A. (See Problem 2-4 for more on inversions.) Suppose that each element of A is chosen randomly, independently, and uniformly from the range 1 through n. Use indicator random variables to compute the expected number of inversions.
+
+### `Answer`
+最简单的解法如下:
+
+因为概率是一样的,所以出现正序和逆序是等量的,总共有n(n-1)/2对,所以期望是n(n-1)/4对.
+
+
+***
+Follow [@louis1992](https://github.com/gzc) on github to help finish this task.
+
diff --git a/C05-Probabilistic-Analysis-and-Randomized-Algorithms/5.3.md b/C05-Probabilistic-Analysis-and-Randomized-Algorithms/5.3.md
index 134e688a..84f8551b 100644
--- a/C05-Probabilistic-Analysis-and-Randomized-Algorithms/5.3.md
+++ b/C05-Probabilistic-Analysis-and-Randomized-Algorithms/5.3.md
@@ -1,61 +1,81 @@
-### Exercises 5.3-1
-***
-Professor Marceau objects to the loop invariant used in the proof of Lemma 5.5. He questions whether it is true prior to the first iteration. His reasoning is that one could just as easily declare that an empty subarray contains no 0-permutations. Therefore, the probability that an empty subarray contains a 0-permutation should be 0, thus invalidating the loop invariant prior to the first iteration. Rewrite the procedure RANDOMIZE-IN-PLACE so that its associated loop invariant applies to a nonempty subarray prior to the first iteration, and modify the proof of Lemma 5.5 for your procedure.
-
-### `Answer`
-感觉这题完全没意义呀...
-
-
-### Exercises 5.3-2
-***
-Professor Kelp decides to write a procedure that will produce at random any permutation besides the identity permutation. He proposes the following procedure:
-
PERMUTE-WITHOUT-IDENTITY(A)
1 n ← length[A]
2 for i ← 1 to n
3 do swap A[i] ↔ A[RANDOM(i + 1, n)]
-
Does this code do what Professor Kelp intends?
-
-### `Answer`
-没有,因为[1,3,2...]这样的序列虽然不是identity permutation但是却不会产生.
-
-### Exercises 5.3-3
-***
-Suppose that instead of swapping element A[i] with a random element from the subarray A[i .. n], we swapped it with a random element from anywhere in the array:

-
- PERMUTE-WITH-ALL(A)
1 n ← length[A]
2 for i ← 1 to n
3 do swap A[i] ↔ A[RANDOM(1, n)]
Does this code produce a uniform random permutation? Why or why not?
-
-### `Answer`
-不可以,因为本来有n!种排列,但是这种做法每个迭代有n种选择,所以有n^n种选择.而n^n不能被n!整除,因此不是uniform的排列.
-
-### Exercises 5.3-4
-***
-Professor Armstrong suggests the following procedure for generating a uniform random permutation:
-
PERMUTE-BY-CYCLIC(A)
1 n ← length[A]
2 offset ← RANDOM(1, n)
- 3 for i <- i to n
- 4 do dest <- i + offset
- 5 if dest > n
- 6 then dest <- dest-n
- 7 B[dest] -< A[i]
- 8 return B
Show that each element A[i] has a 1/n probability of winding up in any particular position in B. Then show that Professor Armstrong is mistaken by showing that the resulting permutation is not uniformly random.
-
-### `Answer`
-这个算法对于给定的offset,只会产生一种排列...也就是最后只有n种排列而不是n!种. 只会将原数组**平移**,谈不上**随机**.
-
-### Exercises 5.3-5
-***
-Prove that in the array P in procedure PERMUTE-BY-SORTING, the probability that all
elements are unique is at least 1 - 1/n.
-
-### `Answer`
-\(1-\\frac{2}{n^3}\) \(1-\\frac{3}{n^3}\)\\ldots \\\\ ~ \\hspace{10 mm}
-\\ge 1\(1-\\frac{n}{n^3}\)\(1-\\frac{n}{n^3}\) \(1-\\frac{n}{n^3}\)\\ldots \\\\ ~ \\hspace{10 mm}
-\\ge \(1-\\frac{1}{n^2}\)^n \\\\ ~ \\hspace{10 mm}
-\\ge 1-\\frac{1}{n} )
-
-### Exercises 5.3-6
-***
-Explain how to implement the algorithm PERMUTE-BY-SORTING to handle the case in which two or more priorities are identical. That is, your algorithm should produce a uniform random permutation, even if two or more priorities are identical.
-
-### `Answer`
-既然冲突了,那就重新产生嘛...
-
-
-***
-Follow [@louis1992](https://github.com/gzc) on github to help finish this task.
-
+### Exercises 5.3-1
+***
+Professor Marceau objects to the loop invariant used in the proof of Lemma 5.5. He questions whether it is true prior to the first iteration. His reasoning is that one could just as easily declare that an empty subarray contains no 0-permutations. Therefore, the probability that an empty subarray contains a 0-permutation should be 0, thus invalidating the loop invariant prior to the first iteration. Rewrite the procedure RANDOMIZE-IN-PLACE so that its associated loop invariant applies to a nonempty subarray prior to the first iteration, and modify the proof of Lemma 5.5 for your procedure.
+
+
+### `Answer`
+感觉这题完全没意义呀...
+
+
+### Exercises 5.3-2
+***
+Professor Kelp decides to write a procedure that will produce at random any permutation besides the identity permutation. He proposes the following procedure:
+
+ PERMUTE-WITHOUT-IDENTITY(A)
+ 1 n ← length[A]
+ 2 for i ← 1 to n
+ 3 do swap A[i] ↔ A[RANDOM(i + 1, n)]
+
+
+Does this code do what Professor Kelp intends?
+
+### `Answer`
+没有,因为[1,3,2...]这样的序列虽然不是identity permutation但是却不会产生.
+
+### Exercises 5.3-3
+***
+Suppose that instead of swapping element A[i] with a random element from the subarray A[i .. n], we swapped it with a random element from anywhere in the array:
+
+
+ PERMUTE-WITH-ALL(A)
+ 1 n ← length[A]
+ 2 for i ← 1 to n
+ 3 do swap A[i] ↔ A[RANDOM(1, n)]
+
+Does this code produce a uniform random permutation? Why or why not?
+
+### `Answer`
+不可以,因为本来有n!种排列,但是这种做法每个迭代有n种选择,所以有n^n种选择.而n^n不能被n!整除,因此不是uniform的排列.
+
+### Exercises 5.3-4
+***
+Professor Armstrong suggests the following procedure for generating a uniform random permutation:
+
+ PERMUTE-BY-CYCLIC(A)
+ 1 n ← length[A]
+ 2 offset ← RANDOM(1, n)
+ 3 for i <- i to n
+ 4 do dest <- i + offset
+ 5 if dest > n
+ 6 then dest <- dest-n
+ 7 B[dest] -< A[i]
+ 8 return B
+
+
+Show that each element A[i] has a 1/n probability of winding up in any particular position in B. Then show that Professor Armstrong is mistaken by showing that the resulting permutation is not uniformly random.
+
+### `Answer`
+这个算法对于给定的offset,只会产生一种排列...也就是最后只有n种排列而不是n!种. 只会将原数组**平移**,谈不上**随机**.
+
+### Exercises 5.3-5
+***
+Prove that in the array P in procedure PERMUTE-BY-SORTING, the probability that all
+elements are unique is at least 1 - 1/n.
+
+### `Answer`
+\(1-\\frac{2}{n^3}\) \(1-\\frac{3}{n^3}\)\\ldots \\\\ ~ \\hspace{10 mm}
+\\ge 1\(1-\\frac{n}{n^3}\)\(1-\\frac{n}{n^3}\) \(1-\\frac{n}{n^3}\)\\ldots \\\\ ~ \\hspace{10 mm}
+\\ge \(1-\\frac{1}{n^2}\)^n \\\\ ~ \\hspace{10 mm}
+\\ge 1-\\frac{1}{n} )
+
+### Exercises 5.3-6
+***
+Explain how to implement the algorithm PERMUTE-BY-SORTING to handle the case in which two or more priorities are identical. That is, your algorithm should produce a uniform random permutation, even if two or more priorities are identical.
+
+### `Answer`
+既然冲突了,那就重新产生嘛...
+
+
+***
+Follow [@louis1992](https://github.com/gzc) on github to help finish this task.
+
diff --git a/C05-Probabilistic-Analysis-and-Randomized-Algorithms/5.4.md b/C05-Probabilistic-Analysis-and-Randomized-Algorithms/5.4.md
index ec9bdf9a..2ca00868 100644
--- a/C05-Probabilistic-Analysis-and-Randomized-Algorithms/5.4.md
+++ b/C05-Probabilistic-Analysis-and-Randomized-Algorithms/5.4.md
@@ -1,67 +1,69 @@
-### Exercises 5.4-1
-***
-How many people must there be in a room before the probability that someone has the same birthday as you do is at least 1/2? How many people must there be before the probability that at least two people have a birthday on July 4 is greater than 1/2?
-
-### `Answer`
-^n \\ge \\frac{1}{2} \\rightarrow n \\ge 253 )
-
-\(\\frac{364}{365}\)^{n-1} - C^0_n\(\\frac{364}{365}\)^n \\ge \\frac{1}{2} \\rightarrow n \\ge 613 )
-
-
-### Exercises 5.4-2
-***
+### Exercises 5.4-1
+***
+How many people must there be in a room before the probability that someone has the same birthday as you do is at least 1/2? How many people must there be before the probability that at least two people have a birthday on July 4 is greater than 1/2?
+
+
+### `Answer`
+^n \\ge \\frac{1}{2} \\rightarrow n \\ge 253 )
+
+\(\\frac{364}{365}\)^{n-1} - C^0_n\(\\frac{364}{365}\)^n \\ge \\frac{1}{2} \\rightarrow n \\ge 613 )
+
+
+### Exercises 5.4-2
+***
Suppose that balls are tossed into b bins. Each toss is independent, and each ball is equally likely to end up in any bin. What is the expected number of ball tosses before at least one of the bins contains two balls?
-
-### `Answer`
-This is a rewording of the Birthday Problem. The answer is the following:
-
- = 1 + \\sum_{k = 1}^{B} \\frac{B!}{\(B-k\)!B^k} )
-
-check [Quora](http://www.quora.com/What-is-the-expected-number-of-ball-tosses-until-some-bin-contains-two-balls) and [wiki](https://en.wikipedia.org/wiki/Birthday_problem#Average_number_of_people)
-### Exercises 5.4-3
-***
+
+### `Answer`
+This is a rewording of the Birthday Problem. The answer is the following:
+
+ = 1 + \\sum_{k = 1}^{B} \\frac{B!}{\(B-k\)!B^k} )
+
+check [Quora](http://www.quora.com/What-is-the-expected-number-of-ball-tosses-until-some-bin-contains-two-balls) and [wiki](https://en.wikipedia.org/wiki/Birthday_problem#Average_number_of_people)
+### Exercises 5.4-3
+***
For the analysis of the birthday paradox, is it important that the birthdays be mutually independent, or is pairwise independence sufficient? Justify your answer.
-
-### `Answer`
-Pairwise independence is sufficient.
-
-### Exercises 5.4-4
-***
-How many people should be invited to a party in order to make it likely that there are three people with the same birthday?
-
-### `Answer`
-If n=365 and k is the number of people at the party:
-
-
-
-
-### Exercises 5.4-5
-***
-What is the probability that a k-string over a set of size n is actually a k-permutation? How does this question relate to the birthday paradox?
-
-### `Answer`
-
-
-To be a k-permutation, there can be no repeated letters, so this is the birthday problem where k is the number of people and n is the number of days.
-
-### Exercises 5.4-6
-***
-Suppose that n balls are tossed into n bins, where each toss is independent and the ball is equally likely to end up in any bin. What is the expected number of empty bins? What is the expected number of bins with exactly one ball?
-
-### `Answer`
-
-^n = \(1-\\frac{1}{n}\)^n \\approx \\frac{1}{e} \\\\
-E[X] = \\sum_{1}^{n}E[X_i] = \\frac{n}{e} \\\\
-Pr\\{Y_i\\} \\quad\\text{is the probability that ith has one ball} \\\\
- P_r\\{Y_i\\} = n \\cdot \\frac{1}{n} \(\\frac{n-1}{n}\)^{n-1} \\approx \\frac{1}{e} \\\\E[Y] = \\sum_{1}^{n}E[Y_i] = \\frac{n}{e} \\\\)
-
-
-### Exercises 5.4-7
-***
-Sharpen the lower bound on streak length by showing that in n flips of a fair coin, the
probability is less than 1/n that no streak longer than lg n-2 lg lg n consecutive heads occurs.
-### `Answer`
-**UNSOLVED**
-
-***
-Follow [@louis1992](https://github.com/gzc) on github to help finish this task.
-
+
+### `Answer`
+Pairwise independence is sufficient.
+
+### Exercises 5.4-4
+***
+How many people should be invited to a party in order to make it likely that there are three people with the same birthday?
+
+### `Answer`
+If n=365 and k is the number of people at the party:
+
+
+
+
+### Exercises 5.4-5
+***
+What is the probability that a k-string over a set of size n is actually a k-permutation? How does this question relate to the birthday paradox?
+
+### `Answer`
+
+
+To be a k-permutation, there can be no repeated letters, so this is the birthday problem where k is the number of people and n is the number of days.
+
+### Exercises 5.4-6
+***
+Suppose that n balls are tossed into n bins, where each toss is independent and the ball is equally likely to end up in any bin. What is the expected number of empty bins? What is the expected number of bins with exactly one ball?
+
+### `Answer`
+
+^n = \(1-\\frac{1}{n}\)^n \\approx \\frac{1}{e} \\\\
+E[X] = \\sum_{1}^{n}E[X_i] = \\frac{n}{e} \\\\
+Pr\\{Y_i\\} \\quad\\text{is the probability that ith has one ball} \\\\
+ P_r\\{Y_i\\} = n \\cdot \\frac{1}{n} \(\\frac{n-1}{n}\)^{n-1} \\approx \\frac{1}{e} \\\\E[Y] = \\sum_{1}^{n}E[Y_i] = \\frac{n}{e} \\\\)
+
+
+### Exercises 5.4-7
+***
+Sharpen the lower bound on streak length by showing that in n flips of a fair coin, the
+probability is less than 1/n that no streak longer than lg n-2 lg lg n consecutive heads occurs.
+### `Answer`
+**UNSOLVED**
+
+***
+Follow [@louis1992](https://github.com/gzc) on github to help finish this task.
+
diff --git a/C05-Probabilistic-Analysis-and-Randomized-Algorithms/problem.md b/C05-Probabilistic-Analysis-and-Randomized-Algorithms/problem.md
index f39bada5..57a0cc91 100644
--- a/C05-Probabilistic-Analysis-and-Randomized-Algorithms/problem.md
+++ b/C05-Probabilistic-Analysis-and-Randomized-Algorithms/problem.md
@@ -1,80 +1,116 @@
-### Problems 1 : Probabilistic counting
-***
-With a b-bit counter, we can ordinarily only count up to 2b - 1. With R. Morris's probabilistic counting, we can count up to a much larger value at the expense of some loss of precision.

-We let a counter value of i represent a count of ni for i = 0, 1,..., 2b -1, where the ni form an increasing sequence of nonnegative values. We assume that the initial value of the counter is 0, representing a count of n0 = 0. The INCREMENT operation works on a counter containing the value i in a probabilistic manner. If i = 2b - 1, then an overflow error is reported. Otherwise, the counter is increased by 1 with probability 1/(ni+1 - ni), and it remains unchanged with probability 1 - 1/(ni+1 - ni).
-
If we select ni = i for all i ≥ 0, then the counter is an ordinary one. More interesting situations arise if we select, say, ni = 2i-1 for i > 0 or ni = Fi (the ith Fibonacci number-see Section 3.2).
-
For this problem, assume that n_(2^b-1) is large enough that the probability of an overflow error is negligible.
-
a. Show that the expected value represented by the counter after n INCREMENT operations have been performed is exactly n.
-
b. The analysis of the variance of the count represented by the counter depends on the sequence of the ni. Let us consider a simple case: ni = 100i for all i ≥ 0. Estimate the variance in the value represented by the register after n INCREMENT operations have been performed.
-
-### `Answer`
-**a.**
-每一次递增操作增加的期望为
-
- + 1\\cdot \(n_{i+1}-n_i\) \\cdot \\frac{1}{n_{i+1}-n_i} = 1 )
-
-**b.**
-
- - 1^2 = 99 \\\\
-Var[X] = \\sum_{i= 1}^{n}Var[X_i] = 99n)
-
-
-### Problems 2 : Searching an unsorted array
-***
-Thus problem examines three algorithms for searching for a value x in an unsorted array A consisting of n elements.
-
Consider the following randomized strategy: pick a random index i into A. If A[i] = x, then we terminate; otherwise, we continue the search by picking a new random index into A. We continue picking random indices into A until we find an index j such that A[j] = x or until we have checked every element of A. Note that we pick from the whole set of indices each time, so that we may examine a given element more than once.
-
**a.** Write pseudocode for a procedure RANDOM-SEARCH to implement the strategy above. Be sure that your algorithm terminates when all indices into A have been picked.
-
**b.** Suppose that there is exactly one index i such that A[i] = x. What is the expected number of indices into A that must be picked before x is found and RANDOM- SEARCH terminates?
-
**c.** Generalizing your solution to part (b), suppose that there are k ≥ 1 indices i such that A[i] = x. What is the expected number of indices into A that must be picked before x is found and RANDOM-SEARCH terminates? Your answer should be a function of n and k.
-
**d.** Suppose that there are no indices i such that A[i] = x. What is the expected number of indices into A that must be picked before all elements of A have been checked and RANDOM-SEARCH terminates?
-
Now consider a deterministic linear search algorithm, which we refer to as DETERMINISTIC-SEARCH. Specifically, the algorithm searches A for x in order,
considering A[1], A[2], A[3],..., A[n] until either A[i] = x is found or the end of the array is reached. Assume that all possible permutations of the input array are equally likely.
-
**e.** Suppose that there is exactly one index i such that A[i] = x. What is the expected running time of DETERMINISTIC-SEARCH? What is the worst-case running time of DETERMINISTIC-SEARCH?
-
**f.** Generalizing your solution to part (e), suppose that there are k ≥ 1 indices i such that A[i] = x. What is the expected running time of DETERMINISTIC-SEARCH? What is the worst-case running time of DETERMINISTIC-SEARCH? Your answer should be a function of n and k.
-
**g.** Suppose that there are no indices i such that A[i] = x. What is the expected running time of DETERMINISTIC-SEARCH? What is the worst-case running time of DETERMINISTIC-SEARCH?
-
Finally, consider a randomized algorithm SCRAMBLE-SEARCH that works by first randomly permuting the input array and then running the deterministic linear search given above on the resulting permuted array.
-
**h.** Letting k be the number of indices i such that A[i] = x, give the worst-case and expected running times of SCRAMBLE-SEARCH for the cases in which k = 0 and k = 1. Generalize your solution to handle the case in which k ≥ 1.
-
**i.** Which of the three searching algorithms would you use? Explain your answer.
-
### `Answer`
-
**a.**
-
RANDOM-SEARCH(A, v):
- B = new array[n]
- count = 0
- while(count < n):
- r = RANDOM(1, n)
- if A[r] == v:
- return r
- if B[r] == false:
- count += 1
- B[r] = true
- return false
-
-**b.**
-就是几何分布~n
-
-**c.**
-还是几何分布~n/k
-
-**d.**
-Section5.4.2,在每个盒子里至少有一个球前,要投多少个球.
-
-n(lnn+O(1))
-
-**e.**
-平均查找时间是n/2,最坏查找时间是n
-
-**f.**
-最坏运行时间是n-k+1.平均运行时间是n/(k+1)
-
-**g.**
-都是n
-
-**h.**
-跟DETERMINISTIC-SEARCH是一样的,这时候的期望就是平均.
-
-**i.**
-自然是**DETERMINISTIC-SEARCH**
-
-
-***
-Follow [@louis1992](https://github.com/gzc) on github to help finish this task.
-
+### Problems 1 : Probabilistic counting
+***
+With a b-bit counter, we can ordinarily only count up to 2b - 1. With R. Morris's probabilistic counting, we can count up to a much larger value at the expense of some loss of precision.
+
+We let a counter value of i represent a count of ni for i = 0, 1,..., 2b -1, where the ni form an increasing sequence of nonnegative values. We assume that the initial value of the counter is 0, representing a count of n0 = 0. The INCREMENT operation works on a counter containing the value i in a probabilistic manner. If i = 2b - 1, then an overflow error is reported. Otherwise, the counter is increased by 1 with probability 1/(ni+1 - ni), and it remains unchanged with probability 1 - 1/(ni+1 - ni).
+
+If we select ni = i for all i ≥ 0, then the counter is an ordinary one. More interesting situations arise if we select, say, ni = 2i-1 for i > 0 or ni = Fi (the ith Fibonacci number-see Section 3.2).
+
+
+For this problem, assume that n_(2^b-1) is large enough that the probability of an overflow error is negligible.
+
+
+a. Show that the expected value represented by the counter after n INCREMENT operations have been performed is exactly n.
+
+
+b. The analysis of the variance of the count represented by the counter depends on the sequence of the ni. Let us consider a simple case: ni = 100i for all i ≥ 0. Estimate the variance in the value represented by the register after n INCREMENT operations have been performed.
+
+### `Answer`
+**a.**
+每一次递增操作增加的期望为
+
+ + 1\\cdot \(n_{i+1}-n_i\) \\cdot \\frac{1}{n_{i+1}-n_i} = 1 )
+
+**b.**
+
+ - 1^2 = 99 \\\\
+Var[X] = \\sum_{i= 1}^{n}Var[X_i] = 99n)
+
+
+### Problems 2 : Searching an unsorted array
+***
+Thus problem examines three algorithms for searching for a value x in an unsorted array A consisting of n elements.
+
+Consider the following randomized strategy: pick a random index i into A. If A[i] = x, then we terminate; otherwise, we continue the search by picking a new random index into A. We continue picking random indices into A until we find an index j such that A[j] = x or until we have checked every element of A. Note that we pick from the whole set of indices each time, so that we may examine a given element more than once.
+
+**a.** Write pseudocode for a procedure RANDOM-SEARCH to implement the strategy above. Be sure that your algorithm terminates when all indices into A have been picked.
+
+
+**b.** Suppose that there is exactly one index i such that A[i] = x. What is the expected number of indices into A that must be picked before x is found and RANDOM- SEARCH terminates?
+
+
+**c.** Generalizing your solution to part (b), suppose that there are k ≥ 1 indices i such that A[i] = x. What is the expected number of indices into A that must be picked before x is found and RANDOM-SEARCH terminates? Your answer should be a function of n and k.
+
+
+**d.** Suppose that there are no indices i such that A[i] = x. What is the expected number of indices into A that must be picked before all elements of A have been checked and RANDOM-SEARCH terminates?
+
+
+Now consider a deterministic linear search algorithm, which we refer to as DETERMINISTIC-SEARCH. Specifically, the algorithm searches A for x in order,
+considering A[1], A[2], A[3],..., A[n] until either A[i] = x is found or the end of the array is reached. Assume that all possible permutations of the input array are equally likely.
+
+
+**e.** Suppose that there is exactly one index i such that A[i] = x. What is the expected running time of DETERMINISTIC-SEARCH? What is the worst-case running time of DETERMINISTIC-SEARCH?
+
+
+**f.** Generalizing your solution to part (e), suppose that there are k ≥ 1 indices i such that A[i] = x. What is the expected running time of DETERMINISTIC-SEARCH? What is the worst-case running time of DETERMINISTIC-SEARCH? Your answer should be a function of n and k.
+
+
+**g.** Suppose that there are no indices i such that A[i] = x. What is the expected running time of DETERMINISTIC-SEARCH? What is the worst-case running time of DETERMINISTIC-SEARCH?
+
+
+Finally, consider a randomized algorithm SCRAMBLE-SEARCH that works by first randomly permuting the input array and then running the deterministic linear search given above on the resulting permuted array.
+
+**h.** Letting k be the number of indices i such that A[i] = x, give the worst-case and expected running times of SCRAMBLE-SEARCH for the cases in which k = 0 and k = 1. Generalize your solution to handle the case in which k ≥ 1.
+
+
+**i.** Which of the three searching algorithms would you use? Explain your answer.
+
+
+### `Answer`
+
+**a.**
+
+
+ RANDOM-SEARCH(A, v):
+ B = new array[n]
+ count = 0
+ while(count < n):
+ r = RANDOM(1, n)
+ if A[r] == v:
+ return r
+ if B[r] == false:
+ count += 1
+ B[r] = true
+ return false
+
+**b.**
+就是几何分布~n
+
+**c.**
+还是几何分布~n/k
+
+**d.**
+Section5.4.2,在每个盒子里至少有一个球前,要投多少个球.
+
+n(lnn+O(1))
+
+**e.**
+平均查找时间是n/2,最坏查找时间是n
+
+**f.**
+最坏运行时间是n-k+1.平均运行时间是n/(k+1)
+
+**g.**
+都是n
+
+**h.**
+跟DETERMINISTIC-SEARCH是一样的,这时候的期望就是平均.
+
+**i.**
+自然是**DETERMINISTIC-SEARCH**
+
+
+
+***
+Follow [@louis1992](https://github.com/gzc) on github to help finish this task.
+
diff --git a/C06-Heapsort/6.3.md b/C06-Heapsort/6.3.md
index e1bcfa71..c287ba89 100644
--- a/C06-Heapsort/6.3.md
+++ b/C06-Heapsort/6.3.md
@@ -1,35 +1,38 @@
-### Exercises 6.3-1
-***
-Using Figure 6.3 as a model, illustrate the operation of BUILD-MAX-HEAP on the array A = [5, 3, 17, 10, 84, 19, 6, 22, 9].
-
-### `Answer`
-
-
-
-### Exercises 6.3-2
-***
-Why do we want the loop index i in line 2 of BUILD-MAX-HEAP to decrease from ⌞length[A]/2⌟ to 1 rather than increase from 1 to ⌞length[A]/2⌟?
-
-### `Answer`
-如果先从1开始,它的子树并不是最大堆,肯定不能这样迭代.
-
-If we start from 1, because its subtree is not a maximum heap, we can't follow this order.
-
-### Exercises 6.3-3
-***
+### Exercises 6.3-1
+***
+Using Figure 6.3 as a model, illustrate the operation of BUILD-MAX-HEAP on the array A = [5, 3, 17, 10, 84, 19, 6, 22, 9].
+
+
+
+### `Answer`
+
+
+
+### Exercises 6.3-2
+***
+Why do we want the loop index i in line 2 of BUILD-MAX-HEAP to decrease from ⌞length[A]/2⌟ to 1 rather than increase from 1 to ⌞length[A]/2⌟?
+
+
+### `Answer`
+如果先从1开始,它的子树并不是最大堆,肯定不能这样迭代.
+
+If we start from 1, because its subtree is not a maximum heap, we can't follow this order.
+
+### Exercises 6.3-3
+***
Show that there are at most  \\rceil) nodes of height h in any n-element heap.
-
-### `Answer`
-According to [6.1.1](./6.1.md)we have
-
-
-
-Mark h0 as the height of tree.
-
- \\rceil \\le \\lceil \(2^{h_0+1}-1\)/\(2^{h+1}\) \\rceil = 2^{h_0-h})
-
-If the current layer is full, then we have equal. If in the leaf node and not full, we have less.
-
-***
-Follow [@louis1992](https://github.com/gzc) on github to help finish this task.
-
+
+### `Answer`
+According to [6.1.1](./6.1.md)we have
+
+
+
+Mark h0 as the height of tree.
+
+ \\rceil \\le \\lceil \(2^{h_0+1}-1\)/\(2^{h+1}\) \\rceil = 2^{h_0-h})
+
+If the current layer is full, then we have equal. If in the leaf node and not full, we have less.
+
+***
+Follow [@louis1992](https://github.com/gzc) on github to help finish this task.
+
diff --git a/C06-Heapsort/6.4.md b/C06-Heapsort/6.4.md
index 42973d63..6a3bb133 100644
--- a/C06-Heapsort/6.4.md
+++ b/C06-Heapsort/6.4.md
@@ -1,45 +1,49 @@
-### Exercises 6.4-1
-***
-Using Figure 6.4 as a model, illustrate the operation of HEAPSORT on the array A = [5, 13, 2, 25, 7, 17, 20, 8, 4].
-
-### `Answer`
-
-
-
-### Exercises 6.4-2
-***
-Argue the correctness of HEAPSORT using the following loop invariant:
-
• At the start of each iteration of the for loop of lines 2-5, the subarray A[1...i] is a max-heap containing the i smallest elements of A[1...n], and the subarray A[i + 1...n] contains the n - i largest elements of A[1...n], sorted.
-
-### `Answer`
-It is very obvious.
-
-### Exercises 6.4-3
-***
+### Exercises 6.4-1
+***
+Using Figure 6.4 as a model, illustrate the operation of HEAPSORT on the array A = [5, 13, 2, 25, 7, 17, 20, 8, 4].
+
+
+
+### `Answer`
+
+
+
+### Exercises 6.4-2
+***
+Argue the correctness of HEAPSORT using the following loop invariant:
+
+• At the start of each iteration of the for loop of lines 2-5, the subarray A[1...i] is a max-heap containing the i smallest elements of A[1...n], and the subarray A[i + 1...n] contains the n - i largest elements of A[1...n], sorted.
+
+
+### `Answer`
+It is very obvious.
+
+### Exercises 6.4-3
+***
What is the running time of heapsort on an array A of length n that is already sorted in increasing order? What about decreasing order?
-
-### `Answer`
-If the array is in descending order, then we have the worst case, we need
-
- )
-
-If it is in increasing order, we still need ),Because the cost of Max_heapify doesn't change.
-
-
-### Exercises 6.4-4
-***
-Show that the worst-case running time of heapsort is Ω(n lg n).
-
-### `Answer`
-Same as 6.3.3.
-
-### Exercises 6.4-5
-***
-Show that when all elements are distinct, the best-case running time of heapsort is Ω(n lg n).
-
-### `Answer`
-It is actually a hard problem, see [solution](http://stackoverflow.com/questions/4589988/lower-bound-on-heapsort)
-
-***
-Follow [@louis1992](https://github.com/gzc) on github to help finish this task.
-
+
+### `Answer`
+If the array is in descending order, then we have the worst case, we need
+
+ )
+
+If it is in increasing order, we still need ),Because the cost of Max_heapify doesn't change.
+
+
+### Exercises 6.4-4
+***
+Show that the worst-case running time of heapsort is Ω(n lg n).
+
+### `Answer`
+Same as 6.3.3.
+
+### Exercises 6.4-5
+***
+Show that when all elements are distinct, the best-case running time of heapsort is Ω(n lg n).
+
+### `Answer`
+It is actually a hard problem, see [solution](http://stackoverflow.com/questions/4589988/lower-bound-on-heapsort)
+
+***
+Follow [@louis1992](https://github.com/gzc) on github to help finish this task.
+
diff --git a/C06-Heapsort/problem.md b/C06-Heapsort/problem.md
index eae8471c..80bc8895 100644
--- a/C06-Heapsort/problem.md
+++ b/C06-Heapsort/problem.md
@@ -1,66 +1,98 @@
-### Problems 1 : Building a heap using insertion
-***
-The procedure BUILD-MAX-HEAP in Section 6.3 can be implemented by repeatedly using MAX-HEAP-INSERT to insert the elements into the heap. Consider the following implementation:
-
- BUILD-MAX-HEAP'(A)
heap-size[A] ← 1
for i←2 to length[A]
do MAX-HEAP-INSERT(A, A[i])
-
-a. Do the procedures BUILD-MAX-HEAP and BUILD-MAX-HEAP' always create the same heap when run on the same input array? Prove that they do, or provide a counterexample.

-b. Show that in the worst case, BUILD-MAX-HEAP' requires Θ(n lg n) time to build an n-element heap.
-
-### `Answer`
-**a.**
-不一定.对于数组[1,2,3,4,5,6].有
-
-
-
-
-**b.**
-当原数组是递增序列时,需要的时间
-
- )
-
-
-
-### Problems 2 : Analysis of d-ary heaps
-***
-A d-ary heap is like a binary heap, but (with one possible exception) non-leaf nodes have d children instead of 2 children.
-
a. How would you represent a d-ary heap in an array?
b. What is the height of a d-ary heap of n elements in terms of n and d?
c. Give an efficient implementation of EXTRACT-MAX in a d-ary max-heap. Analyze
its running time in terms of d and n.
d. Give an efficient implementation of INSERT in a d-ary max-heap. Analyze its running
time in terms of d and n.
e. Give an efficient implementation of INCREASE-KEY(A, i, k), which first sets A[i] ←
max(A[i], k) and then updates the d-ary max-heap structure appropriately. Analyze its running time in terms of d and n.
-### `Answer`
-[implementation](./d-ary-heaps.cpp)
-
-
-
-
-
-
-### Problems 3 : Young tableaus
-***
-An m × n Young tableau is an m × n matrix such that the entries of each row are in sorted order from left to right and the entries of each column are in sorted order from top to bottom. Some of the entries of a Young tableau may be ∞, which we treat as nonexistent elements. Thus, a Young tableau can be used to hold r ≤ mn finite numbers.
-
a. Draw a 4×4 Young tableau containing the elements {9, 16, 3, 2, 4, 8, 5, 14, 12}.
b. Arguethatanm×nYoungtableauYisemptyifY[1,1]=∞.ArguethatYisfull
(contains mn elements) if Y[m, n] < ∞.
c. Give an algorithm to implement EXTRACT-MIN on a nonempty m × n Young
tableau that runs in O(m + n) time. Your algorithm should use a recursive subroutine that solves an m × n problem by recursively solving either an (m - 1) × n or an m × (n - 1) subproblem. (Hint: Think about MAX-HEAPIFY.) Define T(p), where p = m + n, to be the maximum running time of EXTRACT-MIN on any m × n Young tableau. Give and solve a recurrence for T(p) that yields the O(m + n) time bound.
d. Show how to insert a new element into a nonfull m × n Young tableau in O(m + n) time.
e. Using no other sorting method as a subroutine, show how to use an n × n Young tableau to sort n^2 numbers in O(n^3) time.
f. Give an O(m+n)-time algorithm to determine whether a given number is stored in a given m × n Young tableau.
-
### `Answer`
-
[implementation](./young.cpp)
-
**a.**
-
-
-
**b.**
-
这是显然的~
-
**c.**
-T(p) = T(p-1) + O(1) = O(p)
-
+### Problems 1 : Building a heap using insertion
+***
+The procedure BUILD-MAX-HEAP in Section 6.3 can be implemented by repeatedly using MAX-HEAP-INSERT to insert the elements into the heap. Consider the following implementation:
+
+ BUILD-MAX-HEAP'(A)
+ heap-size[A] ← 1
+ for i←2 to length[A]
+ do MAX-HEAP-INSERT(A, A[i])
+
+
+a. Do the procedures BUILD-MAX-HEAP and BUILD-MAX-HEAP' always create the same heap when run on the same input array? Prove that they do, or provide a counterexample.
+
+b. Show that in the worst case, BUILD-MAX-HEAP' requires Θ(n lg n) time to build an n-element heap.
+
+### `Answer`
+**a.**
+不一定.对于数组[1,2,3,4,5,6].有
+
+
+
+
+**b.**
+当原数组是递增序列时,需要的时间
+
+ )
+
+
+
+### Problems 2 : Analysis of d-ary heaps
+***
+A d-ary heap is like a binary heap, but (with one possible exception) non-leaf nodes have d children instead of 2 children.
+
+a. How would you represent a d-ary heap in an array?
+b. What is the height of a d-ary heap of n elements in terms of n and d?
+c. Give an efficient implementation of EXTRACT-MAX in a d-ary max-heap. Analyze
+its running time in terms of d and n.
+d. Give an efficient implementation of INSERT in a d-ary max-heap. Analyze its running
+time in terms of d and n.
+e. Give an efficient implementation of INCREASE-KEY(A, i, k), which first sets A[i] ←
+max(A[i], k) and then updates the d-ary max-heap structure appropriately. Analyze its running time in terms of d and n.
+
+### `Answer`
+[implementation](./d-ary-heaps.cpp)
+
+
+
+
+
+
+### Problems 3 : Young tableaus
+***
+An m × n Young tableau is an m × n matrix such that the entries of each row are in sorted order from left to right and the entries of each column are in sorted order from top to bottom. Some of the entries of a Young tableau may be ∞, which we treat as nonexistent elements. Thus, a Young tableau can be used to hold r ≤ mn finite numbers.
+
+a. Draw a 4×4 Young tableau containing the elements {9, 16, 3, 2, 4, 8, 5, 14, 12}.
+b. Arguethatanm×nYoungtableauYisemptyifY[1,1]=∞.ArguethatYisfull
+(contains mn elements) if Y[m, n] < ∞.
+c. Give an algorithm to implement EXTRACT-MIN on a nonempty m × n Young
+tableau that runs in O(m + n) time. Your algorithm should use a recursive subroutine that solves an m × n problem by recursively solving either an (m - 1) × n or an m × (n - 1) subproblem. (Hint: Think about MAX-HEAPIFY.) Define T(p), where p = m + n, to be the maximum running time of EXTRACT-MIN on any m × n Young tableau. Give and solve a recurrence for T(p) that yields the O(m + n) time bound.
+d. Show how to insert a new element into a nonfull m × n Young tableau in O(m + n) time.
+e. Using no other sorting method as a subroutine, show how to use an n × n Young tableau to sort n^2 numbers in O(n^3) time.
+f. Give an O(m+n)-time algorithm to determine whether a given number is stored in a given m × n Young tableau.
+
+
+### `Answer`
+
+[implementation](./young.cpp)
+
+**a.**
+
+
+
+
+**b.**
+
+这是显然的~
+
+
+**c.**
+T(p) = T(p-1) + O(1) = O(p)
+
**d.**
-跟c是一个相反的操作,具体见代码.
-
-**e.**
- = O\(n^3\))
-
-**f.**
-实现见代码.
-
-
-***
-Follow [@louis1992](https://github.com/gzc) on github to help finish this task.
-
+跟c是一个相反的操作,具体见代码.
+
+**e.**
+ = O\(n^3\))
+
+**f.**
+实现见代码.
+
+
+***
+Follow [@louis1992](https://github.com/gzc) on github to help finish this task.
+
diff --git a/C07-Quicksort/problem.md b/C07-Quicksort/problem.md
index ba81fe76..2055d5a3 100644
--- a/C07-Quicksort/problem.md
+++ b/C07-Quicksort/problem.md
@@ -1,232 +1,265 @@
-### Problems 1 : Hoare partition correctness
-***
-The version of PARTITION given in this chapter is not the original partitioning algorithm. Here is the original partition algorithm, which is due to T. Hoare:
-
- HOARE-PARTITION(A, p, r):
x <- A[p]
- i <- p - 1
- j <- r + 1
- while TRUE:
- do repeat j <- j - 1
- until A[j] <= x
- repeat i <- i + 1
- until A[i] >= x
- if i < j:
- then exchange A[i] <-> A[j]
- else return j
-
-
-**a.** Demonstrate the operation of HOARE-PARTITION on the array A = [13, 19, 9, 5, 12, 8, 7, 4, 11, 2, 6, 21], showing the values of the array and auxiliary values after each iteration of the for loop in lines 4-11.
-
-The next three questions ask you to give a careful argument that the procedure HOARE- PARTITION is correct. Prove the following:
-
-**b.** The indices i and j are such that we never access an element of A outside the subarray A[p...r].
-
**c.** When HOARE-PARTITION terminates, it returns a value j such that p ≤ j < r.
-
**d.** Every element of A[p...j] is less than or equal to every element of A[j+1...r] when
HOARE-PARTITION terminates.
-
The PARTITION procedure in Section 7.1 separates the pivot value (originally in A[r]) from the two partitions it forms. The HOARE-PARTITION procedure, on the other hand, always places the pivot value (originally in A[p]) into one of the two partitions A[p...j] and A[j + 1...r]. Since p ≤ j < r, this split is always nontrivial.
-
**e.** Rewrite the QUICKSORT procedure to use HOARE-PARTITION.
-### `Answer`
-**a.**
-
-
-
-
-**b.**
-
-这是肯定的,因为i,j是往中间靠拢的.
-
-**c.**
-j至少会减2次. 若第一次进入while,j只减了1次,那么会做一次swap.然后继续进入while.
-
-**d.**
-
-很显然,小于x的放前面了,大于等于x的在后面.
-
-**e.**
-
-[implementation](./exercise_code/hoare.py)
-
-
-### Problems 2 : Alternative quicksort analysis
-***
-An alternative analysis of the running time of randomized quicksort focuses on the expected running time of each individual recursive call to QUICKSORT, rather than on the number of comparisons performed.
-
**a.** Argue that, given an array of size n, the probability that any particular element is chosen as the pivot is 1/n. Use this to define indicator random variables Xi = I{ith smallest element is chosen as the pivot}. What is E [Xi]?
-
**b.** Let T (n) be a random variable denoting the running time of quicksort on an array of size n. Argue that
-] = E\\bigg[\\sum_{q=1}^nX_q\(T\(q-1\) + T\(n-q\) + \\Theta\(n\)\)\\bigg] )
-
**c.**
Show that equation (7.5) simplifies to
-
-] = \\frac{2}{n}\\sum_{q=2}^{n-1}E[T\(q\)] + \\Theta\(n\) )
-
-**d.**
-Show that
-
-
-
-**e.**
-
-Using the bound from equation (7.7), show that the recurrence in equation (7.6) has
the solution E [T (n)] = Θ(n lg n). (Hint: Show, by substitution, that E[T (n)] ≤ an log n - bn for some positive constants a and b.)
-
-
### `Answer`
**a.**
-
E[Xi]=1/n.
-
**b.**
-
-这个算式的本质和之前分析快速排序的算式是一样的.
-
-**c.**
-
-就是简单的化简
-
-**d.**
-
- + \\sum_{k=n/2 + 1}^{n}k\\lg{n} \\\\ ~ \\hspace{22 mm}
- = \\lg\(n/2\)\\sum_{k=2}^{n/2}k + \\lg{n}\\sum_{k=n/2 + 1}^{n}k \\\\ ~ \\hspace{22 mm}
- = \(\\lg{n} - \\lg{2}\)\\bigg\(\\frac{\(n/2\)\(n/2 + 1\)}{2}\\bigg\) + \\lg{n}\\bigg\(\\frac{n\(n+1\)}{2} - \\frac{\(n/2\)\(n/2 + 1\)}{2}\\bigg\) \\\\ ~ \\hspace{22 mm}
- = \\lg{n}\\frac{n\(n+1\)}{2} - \\frac{\(n/2\)\(n/2 + 1\)}{2} \\\\ ~ \\hspace{22 mm}
- = \\frac{1}{2}\\lg{n}\(n^2 + 2n + 1\) - \\frac{1}{8}\(n^2 + 2n + 1/8\) \\\\ ~ \\hspace{22 mm}
- = \\frac{1}{2}n^2\\lg{n} - \\frac{1}{8}n^2 - \\frac{8n\\lg{n} + 4\\lg{n} - 2n - 1/8}{8} \\\\ ~ \\hspace{22 mm}
- \\le \\frac{1}{2}n^2\\lg{n} - \\frac{1}{8}n^2)
-
-**e.**
-
-我们猜想 E[T(n)] ≤ anlgn
-
-] = \\frac{2}{n}\\sum_{q=2}^{n-1}E[T\(q\)] + \\Theta\(n\) \\\\ ~ \\hspace{21 mm}
- \\le \\frac{2}{n}\\sum_{q=2}^{n-1}an\\lg{n} + \\Theta\(n\) \\\\ ~ \\hspace{21 mm}
- \\le \\frac{2a}{n}\\bigg\(\\frac{1}{2}n^2\\lg{n} - \\frac{1}{8}n^2\\bigg\)
- + \\Theta\(n\) \\\\\ ~ \\hspace{21 mm}
- = an\\lg{n} - \\frac{a}{4}n + \\Theta\(n\) \\\\ ~ \\hspace{21 mm}
+### Problems 1 : Hoare partition correctness
+***
+The version of PARTITION given in this chapter is not the original partitioning algorithm. Here is the original partition algorithm, which is due to T. Hoare:
+
+ HOARE-PARTITION(A, p, r):
+ x <- A[p]
+ i <- p - 1
+ j <- r + 1
+ while TRUE:
+ do repeat j <- j - 1
+ until A[j] <= x
+ repeat i <- i + 1
+ until A[i] >= x
+ if i < j:
+ then exchange A[i] <-> A[j]
+ else return j
+
+
+**a.** Demonstrate the operation of HOARE-PARTITION on the array A = [13, 19, 9, 5, 12, 8, 7, 4, 11, 2, 6, 21], showing the values of the array and auxiliary values after each iteration of the for loop in lines 4-11.
+
+The next three questions ask you to give a careful argument that the procedure HOARE- PARTITION is correct. Prove the following:
+
+**b.** The indices i and j are such that we never access an element of A outside the subarray A[p...r].
+
+**c.** When HOARE-PARTITION terminates, it returns a value j such that p ≤ j < r.
+
+**d.** Every element of A[p...j] is less than or equal to every element of A[j+1...r] when
+HOARE-PARTITION terminates.
+
+The PARTITION procedure in Section 7.1 separates the pivot value (originally in A[r]) from the two partitions it forms. The HOARE-PARTITION procedure, on the other hand, always places the pivot value (originally in A[p]) into one of the two partitions A[p...j] and A[j + 1...r]. Since p ≤ j < r, this split is always nontrivial.
+
+**e.** Rewrite the QUICKSORT procedure to use HOARE-PARTITION.
+
+
+### `Answer`
+**a.**
+
+
+
+
+**b.**
+
+这是肯定的,因为i,j是往中间靠拢的.
+
+**c.**
+j至少会减2次. 若第一次进入while,j只减了1次,那么会做一次swap.然后继续进入while.
+
+**d.**
+
+很显然,小于x的放前面了,大于等于x的在后面.
+
+**e.**
+
+[implementation](./exercise_code/hoare.py)
+
+
+### Problems 2 : Alternative quicksort analysis
+***
+An alternative analysis of the running time of randomized quicksort focuses on the expected running time of each individual recursive call to QUICKSORT, rather than on the number of comparisons performed.
+
+**a.** Argue that, given an array of size n, the probability that any particular element is chosen as the pivot is 1/n. Use this to define indicator random variables Xi = I{ith smallest element is chosen as the pivot}. What is E [Xi]?
+
+**b.** Let T (n) be a random variable denoting the running time of quicksort on an array of size n. Argue that
+] = E\\bigg[\\sum_{q=1}^nX_q\(T\(q-1\) + T\(n-q\) + \\Theta\(n\)\)\\bigg] )
+
+**c.**
+Show that equation (7.5) simplifies to
+
+] = \\frac{2}{n}\\sum_{q=2}^{n-1}E[T\(q\)] + \\Theta\(n\) )
+
+**d.**
+Show that
+
+
+
+**e.**
+
+Using the bound from equation (7.7), show that the recurrence in equation (7.6) has
+the solution E [T (n)] = Θ(n lg n). (Hint: Show, by substitution, that E[T (n)] ≤ an log n - bn for some positive constants a and b.)
+
+
+### `Answer`
+**a.**
+
+E[Xi]=1/n.
+
+
+**b.**
+
+这个算式的本质和之前分析快速排序的算式是一样的.
+
+**c.**
+
+就是简单的化简
+
+**d.**
+
+ + \\sum_{k=n/2 + 1}^{n}k\\lg{n} \\\\ ~ \\hspace{22 mm}
+ = \\lg\(n/2\)\\sum_{k=2}^{n/2}k + \\lg{n}\\sum_{k=n/2 + 1}^{n}k \\\\ ~ \\hspace{22 mm}
+ = \(\\lg{n} - \\lg{2}\)\\bigg\(\\frac{\(n/2\)\(n/2 + 1\)}{2}\\bigg\) + \\lg{n}\\bigg\(\\frac{n\(n+1\)}{2} - \\frac{\(n/2\)\(n/2 + 1\)}{2}\\bigg\) \\\\ ~ \\hspace{22 mm}
+ = \\lg{n}\\frac{n\(n+1\)}{2} - \\frac{\(n/2\)\(n/2 + 1\)}{2} \\\\ ~ \\hspace{22 mm}
+ = \\frac{1}{2}\\lg{n}\(n^2 + 2n + 1\) - \\frac{1}{8}\(n^2 + 2n + 1/8\) \\\\ ~ \\hspace{22 mm}
+ = \\frac{1}{2}n^2\\lg{n} - \\frac{1}{8}n^2 - \\frac{8n\\lg{n} + 4\\lg{n} - 2n - 1/8}{8} \\\\ ~ \\hspace{22 mm}
+ \\le \\frac{1}{2}n^2\\lg{n} - \\frac{1}{8}n^2)
+
+**e.**
+
+我们猜想 E[T(n)] ≤ anlgn
+
+] = \\frac{2}{n}\\sum_{q=2}^{n-1}E[T\(q\)] + \\Theta\(n\) \\\\ ~ \\hspace{21 mm}
+ \\le \\frac{2}{n}\\sum_{q=2}^{n-1}an\\lg{n} + \\Theta\(n\) \\\\ ~ \\hspace{21 mm}
+ \\le \\frac{2a}{n}\\bigg\(\\frac{1}{2}n^2\\lg{n} - \\frac{1}{8}n^2\\bigg\)
+ + \\Theta\(n\) \\\\\ ~ \\hspace{21 mm}
+ = an\\lg{n} - \\frac{a}{4}n + \\Theta\(n\) \\\\ ~ \\hspace{21 mm}
\\le an\\lg{n} )
-
-
-### Problems 3 : Stooge sort
-***
-Professors Howard, Fine, and Howard have proposed the following "elegant" sorting algorithm:
-
- STOOGE-SORT(A, i, j):
- if A[i] > A[j]
- then exchange A[i] <-> A[j]
- if i + 1 >= j
- then return
- k <- ⌊(j-i+1)/3⌋
- STOOGE-SORT(A, i, j-k)
- STOOGE-SORT(A, i+k, j)
- STOOGE-SORT(A, i, j-k)
-
-a. Argue that, if n = length[A], then STOOGE-SORT(A, 1, length[A]) correctly sorts the input array A[1...n].
-
b. Give a recurrence for the worst-case running time of STOOGE-SORT and a tight asymptotic (Θ-notation) bound on the worst-case running time.
-
c. Compare the worst-case running time of STOOGE-SORT with that of insertion sort, merge sort, heapsort, and quicksort. Do the professors deserve tenure?
-### `Answer`
-
-**a.**
-
-分三次进行排序,先将前2/3排好,再排后2/3,那么这时候最大的1/3已经在后面了。最后再对前面的2/3进行排序。是正确的。
-
-**b.**
-
-T(n) = 3T(2n/3) + O(1)
-
-根据主定理最坏运行时间是)
-
-**c.**
-
-当然不会!
-
-
-### Problems 4 : Stack depth for quicksort
-***
-The QUICKSORT algorithm of Section 7.1 contains two recursive calls to itself. After the call to PARTITION, the left subarray is recursively sorted and then the right subarray is recursively sorted. The second recursive call in QUICKSORT is not really necessary; it can be avoided by using an iterative control structure. This technique, called tail recursion, is provided automatically by good compilers. Consider the following version of quicksort, which simulates tail recursion.
-
- QUICKSORT'(A, p, r):
- while p < r:
- do Partition and sort left subarray.
- q <- PARTITION(A, p, r)
- QUICKSORT'(A, p, q-1)
- p <- q + 1
-
-**a.** Argue that QUICKSORT'(A, 1, length[A]) correctly sorts the array A.
-
-Compilers usually execute recursive procedures by using a stack that contains pertinent information, including the parameter values, for each recursive call. The information for the most recent call is at the top of the stack, and the information for the initial call is at the bottom. When a procedure is invoked, its information is pushed onto the stack; when it terminates, its information is popped. Since we assume that array parameters are represented by pointers, the information for each procedure call on the stack requires O(1) stack space. The stack depth is the maximum amount of stack space used at any time during a computation.
-
-**b.** Describe a scenario in which the stack depth of QUICKSORT' is Θ(n) on an n-element input array.
-
-**c.**
-Modify the code for QUICKSORT' so that the worst-case stack depth is Θ(lg n). Maintain the O(n lg n) expected running time of the algorithm.
-
-### `Answer`
-**a.**
-
-这个递推式总是先把左边的排好序,再排右边的. 顺序上和以前的版本是一样的.
-
-**b.**
-
-当运气比较差的时候,每次PARTITION都return r,会产生O(n)的堆栈深度.
-
-**c.**
-
-key idea : 先选range小的那一边迭代
-
-[implementation](./exercise_code/tailrecursive.py)
-
-
+
+
+### Problems 3 : Stooge sort
+***
+Professors Howard, Fine, and Howard have proposed the following "elegant" sorting algorithm:
+
+ STOOGE-SORT(A, i, j):
+ if A[i] > A[j]
+ then exchange A[i] <-> A[j]
+ if i + 1 >= j
+ then return
+ k <- ⌊(j-i+1)/3⌋
+ STOOGE-SORT(A, i, j-k)
+ STOOGE-SORT(A, i+k, j)
+ STOOGE-SORT(A, i, j-k)
+
+a. Argue that, if n = length[A], then STOOGE-SORT(A, 1, length[A]) correctly sorts the input array A[1...n].
+
+b. Give a recurrence for the worst-case running time of STOOGE-SORT and a tight asymptotic (Θ-notation) bound on the worst-case running time.
+
+c. Compare the worst-case running time of STOOGE-SORT with that of insertion sort, merge sort, heapsort, and quicksort. Do the professors deserve tenure?
+
+### `Answer`
+
+**a.**
+
+分三次进行排序,先将前2/3排好,再排后2/3,那么这时候最大的1/3已经在后面了。最后再对前面的2/3进行排序。是正确的。
+
+**b.**
+
+T(n) = 3T(2n/3) + O(1)
+
+根据主定理最坏运行时间是)
+
+**c.**
+
+当然不会!
+
+
+### Problems 4 : Stack depth for quicksort
+***
+The QUICKSORT algorithm of Section 7.1 contains two recursive calls to itself. After the call to PARTITION, the left subarray is recursively sorted and then the right subarray is recursively sorted. The second recursive call in QUICKSORT is not really necessary; it can be avoided by using an iterative control structure. This technique, called tail recursion, is provided automatically by good compilers. Consider the following version of quicksort, which simulates tail recursion.
+
+ QUICKSORT'(A, p, r):
+ while p < r:
+ do Partition and sort left subarray.
+ q <- PARTITION(A, p, r)
+ QUICKSORT'(A, p, q-1)
+ p <- q + 1
+
+**a.** Argue that QUICKSORT'(A, 1, length[A]) correctly sorts the array A.
+
+Compilers usually execute recursive procedures by using a stack that contains pertinent information, including the parameter values, for each recursive call. The information for the most recent call is at the top of the stack, and the information for the initial call is at the bottom. When a procedure is invoked, its information is pushed onto the stack; when it terminates, its information is popped. Since we assume that array parameters are represented by pointers, the information for each procedure call on the stack requires O(1) stack space. The stack depth is the maximum amount of stack space used at any time during a computation.
+
+**b.** Describe a scenario in which the stack depth of QUICKSORT' is Θ(n) on an n-element input array.
+
+**c.**
+Modify the code for QUICKSORT' so that the worst-case stack depth is Θ(lg n). Maintain the O(n lg n) expected running time of the algorithm.
+
+### `Answer`
+**a.**
+
+这个递推式总是先把左边的排好序,再排右边的. 顺序上和以前的版本是一样的.
+
+**b.**
+
+当运气比较差的时候,每次PARTITION都return r,会产生O(n)的堆栈深度.
+
+**c.**
+
+key idea : 先选range小的那一边迭代
+
+[implementation](./exercise_code/tailrecursive.py)
+
+
### Problems 5 : Median-of-3 partition
-***
-One way to improve the RANDOMIZED-QUICKSORT procedure is to partition around a pivot that is chosen more carefully than by picking a random element from the subarray. One common approach is the **median-of-3** method: choose the pivot as the median (middle element) of a set of 3 elements randomly selected from the subarray. (See Exercise 7.4-6.) For this problem, let us assume that the elements in the input array A[1...n] are distinct and that n ≥ 3. We denote the sorted output array by A'[1...n]. Using the median-of-3 method to choose the pivot element x, define pi = Pr{x = A'[i]}.
-
-**a.** Give an exact formula for pi as a function of n and i for i=2,3,...,n-1.(Note that p1 = pn = 0.)
-
-**b.** By what amount have we increased the likelihood of choosing the pivot as x = A'[⌊(n
+ 1/2⌋], the median of A[1...n], compared to the ordinary implementation? Assume that n → ∞, and give the limiting ratio of these probabilities.
-
**c.** If we define a "good" split to mean choosing the pivot as x = A'[i], where n/ ≤ i ≤ 2n/3, by what amount have we increased the likelihood of getting a good split compared to the ordinary implementation? (Hint: Approximate the sum by an integral.)
-
**d.** Argue that in the Ω(n lg n) running time of quicksort, the median-of-3 method affects only the constant factor.
-
-### `Answer`
-**a.**
-
-\(n-i\)}{n\(n-1\)\(n-2\)} )
-
-**b.**
-
-\(n-i\)}{n\(n-1\)\(n-2\)}/\\frac{1}{n}
- = \\lim_{n \\to \\infty}\\frac{6n\(n/2 - 1\)\(n/2\)}{\(n-1\)\(n-2\)} = 1.5 )
-
-**c.**
-
-\(n-i\)}{n\(n-1\)\(n-2\)} =
-\\lim_{n \\to \\infty}\\frac{6}{n\(n-1\)\(n-2\)}\\sum_{i=n/3}^{2n/3}\(i-1\)\(n-i\) =
-\\frac{13}{27})
-
-**d.**
-这种方法无法保证能取最优划分点,依然是概率问题,而且比普通的提升并没有很大.
-
-
-### Problems 6 : Fuzzy sorting of intervals
-Consider a sorting problem in which the numbers are not known exactly. Instead, for each number, we know an interval on the real line to which it belongs. That is, we are given n closed intervals of the form [ai, bi], where ai ≤ bi. The goal is to **fuzzy-sort** these intervals, i.e., produce a permutation [i1, i2,..., in] of the intervals such that there exist
-, satisfying c1 ≤c2 ≤···≤cn.
**a.** Design an algorithm for fuzzy-sorting n intervals. Your algorithm should have the general structure of an algorithm that quicksorts the left endpoints (the ai 's), but it should take advantage of overlapping intervals to improve the running time. (As the intervals overlap more and more, the problem of fuzzy-sorting the intervals gets easier and easier. Your algorithm should take advantage of such overlapping, to the extent that it exists.)
-
### `Answer`
-
[implementation](./exercise_code/fuzzy_sort.py)
-
类似于quicksort,只是当重叠区域越多,pivot内的区别就越多~~
-
-### Problems 2 (3rd Edition): Quicksort with equal element values
-The analysis of the expected running time of randomized quicksort in Section 7.4.2 assumes that all element values are distinct. In this problem, we examine what happens when they are not.
-
-**a.** Suppose that all element values are equal. What would be randomized quick-sort's running time in this case?
-If the algorithm uses Lumuto Partition, then running time is O(n^2)
-If the algorithm uses modified Hoare partition that stops scanning at equal case, then the worst case can be avoided.
-
-**b.** Write a 3-way partition algorithm
-**c.** Implement randomized-3-way-partition
-
-[implementation](./exercise_code/quickSortWithEqualElements.cpp)
-
-**d.** Using 3-way-partition-qsort, adjust the analysis in Section 7.4.2 to avoid the assumpton that all elements are equal.
-
-***
-Follow [@louis1992](https://github.com/gzc) on github to help finish this task.
-
+***
+One way to improve the RANDOMIZED-QUICKSORT procedure is to partition around a pivot that is chosen more carefully than by picking a random element from the subarray. One common approach is the **median-of-3** method: choose the pivot as the median (middle element) of a set of 3 elements randomly selected from the subarray. (See Exercise 7.4-6.) For this problem, let us assume that the elements in the input array A[1...n] are distinct and that n ≥ 3. We denote the sorted output array by A'[1...n]. Using the median-of-3 method to choose the pivot element x, define pi = Pr{x = A'[i]}.
+
+**a.** Give an exact formula for pi as a function of n and i for i=2,3,...,n-1.(Note that p1 = pn = 0.)
+
+**b.** By what amount have we increased the likelihood of choosing the pivot as x = A'[⌊(n
++ 1/2⌋], the median of A[1...n], compared to the ordinary implementation? Assume that n → ∞, and give the limiting ratio of these probabilities.
+
+**c.** If we define a "good" split to mean choosing the pivot as x = A'[i], where n/ ≤ i ≤ 2n/3, by what amount have we increased the likelihood of getting a good split compared to the ordinary implementation? (Hint: Approximate the sum by an integral.)
+
+**d.** Argue that in the Ω(n lg n) running time of quicksort, the median-of-3 method affects only the constant factor.
+
+### `Answer`
+**a.**
+
+\(n-i\)}{n\(n-1\)\(n-2\)} )
+
+**b.**
+
+\(n-i\)}{n\(n-1\)\(n-2\)}/\\frac{1}{n}
+ = \\lim_{n \\to \\infty}\\frac{6n\(n/2 - 1\)\(n/2\)}{\(n-1\)\(n-2\)} = 1.5 )
+
+**c.**
+
+\(n-i\)}{n\(n-1\)\(n-2\)} =
+\\lim_{n \\to \\infty}\\frac{6}{n\(n-1\)\(n-2\)}\\sum_{i=n/3}^{2n/3}\(i-1\)\(n-i\) =
+\\frac{13}{27})
+
+**d.**
+这种方法无法保证能取最优划分点,依然是概率问题,而且比普通的提升并没有很大.
+
+
+### Problems 6 : Fuzzy sorting of intervals
+Consider a sorting problem in which the numbers are not known exactly. Instead, for each number, we know an interval on the real line to which it belongs. That is, we are given n closed intervals of the form [ai, bi], where ai ≤ bi. The goal is to **fuzzy-sort** these intervals, i.e., produce a permutation [i1, i2,..., in] of the intervals such that there exist
+, satisfying c1 ≤c2 ≤···≤cn.
+
+
+**a.** Design an algorithm for fuzzy-sorting n intervals. Your algorithm should have the general structure of an algorithm that quicksorts the left endpoints (the ai 's), but it should take advantage of overlapping intervals to improve the running time. (As the intervals overlap more and more, the problem of fuzzy-sorting the intervals gets easier and easier. Your algorithm should take advantage of such overlapping, to the extent that it exists.)
+
+
+### `Answer`
+
+[implementation](./exercise_code/fuzzy_sort.py)
+
+
+类似于quicksort,只是当重叠区域越多,pivot内的区别就越多~~
+
+### Problems 2 (3rd Edition): Quicksort with equal element values
+The analysis of the expected running time of randomized quicksort in Section 7.4.2 assumes that all element values are distinct. In this problem, we examine what happens when they are not.
+
+**a.** Suppose that all element values are equal. What would be randomized quick-sort's running time in this case?
+If the algorithm uses Lumuto Partition, then running time is O(n^2)
+If the algorithm uses modified Hoare partition that stops scanning at equal case, then the worst case can be avoided.
+
+**b.** Write a 3-way partition algorithm
+**c.** Implement randomized-3-way-partition
+
+[implementation](./exercise_code/quickSortWithEqualElements.cpp)
+
+**d.** Using 3-way-partition-qsort, adjust the analysis in Section 7.4.2 to avoid the assumpton that all elements are equal.
+
+***
+Follow [@louis1992](https://github.com/gzc) on github to help finish this task.
+
diff --git a/C08-Sorting-in-Linear-Time/8.2.md b/C08-Sorting-in-Linear-Time/8.2.md
index 9d00b42b..99b1fe87 100644
--- a/C08-Sorting-in-Linear-Time/8.2.md
+++ b/C08-Sorting-in-Linear-Time/8.2.md
@@ -1,37 +1,38 @@
-### Exercises 8.2-1
-***
+### Exercises 8.2-1
+***
Using Figure 8.2 as a model, illustrate the operation of COUNTING-SORT on the array A = [6, 0, 2, 0, 1, 3, 4, 6, 1, 3, 2].
-### `Answer`
-
-
-
-### Exercises 8.2-2
-***
+### `Answer`
+
+
+
+### Exercises 8.2-2
+***
Prove that COUNTING-SORT is stable.
-
-### `Answer`
-COUNTING-SORT最后是从后面往前面扫,并且把遇到的每一个item放在同大小(key)的最后面的位子.所以不改变相对顺序.
-
-### Exercises 8.2-3
-***
-Suppose that the for loop header in line 9 of the COUNTING-SORT procedure is rewritten
-
-as 9 for j ← 1 to length[A]
-
Show that the algorithm still works properly. Is the modified algorithm stable?
-
-### `Answer`
-可以正常工作,只是不stable.
-
-### Exercises 8.2-4
-***
+
+### `Answer`
+COUNTING-SORT最后是从后面往前面扫,并且把遇到的每一个item放在同大小(key)的最后面的位子.所以不改变相对顺序.
+
+### Exercises 8.2-3
+***
+Suppose that the for loop header in line 9 of the COUNTING-SORT procedure is rewritten
+
+as 9 for j ← 1 to length[A]
+
+Show that the algorithm still works properly. Is the modified algorithm stable?
+
+### `Answer`
+可以正常工作,只是不stable.
+
+### Exercises 8.2-4
+***
Describe an algorithm that, given n integers in the range 0 to k, preprocesses its input and then answers any query about how many of the n integers fall into a range [a...b] in O(1) time. Your algorithm should use Θ(n + k) preprocessing time.
-
-### `Answer`
-利用数组C,[a,b]间的个数是C[b]-C[a-1].(C[-1] = 0)
-
-[implementation](./exercise_code/integerQuery.cpp)
-
-***
-Follow [@louis1992](https://github.com/gzc) on github to help finish this task.
-
+
+### `Answer`
+利用数组C,[a,b]间的个数是C[b]-C[a-1].(C[-1] = 0)
+
+[implementation](./exercise_code/integerQuery.cpp)
+
+***
+Follow [@louis1992](https://github.com/gzc) on github to help finish this task.
+
diff --git a/C08-Sorting-in-Linear-Time/problem.md b/C08-Sorting-in-Linear-Time/problem.md
index 4266f821..7c7e76e0 100644
--- a/C08-Sorting-in-Linear-Time/problem.md
+++ b/C08-Sorting-in-Linear-Time/problem.md
@@ -134,8 +134,8 @@ For the nubers, we can do this:
- Group the nunbers by number of digits and order groups.
- RADIX sort each group.
-We let the `Gi` be the group of numbers with i digits and ci = |Gi|, thus: we can multiply the `n*Ci` with `i`, then sum from `i = 1` to `i = highest digit`. We can get the
-
+We let the `Gi` be the group of numbers with i digits and ci = |Gi|, thus: we can multiply the `n*Ci` with `i`, then sum from `i = 1` to `i = highest digit`. We can get the
+
= \\sum_{i = 1}nc_i \\cdot i = n )
@@ -146,14 +146,14 @@ reverse所有的字符串,用RADIX-SORT.当当遍历到某一个字符串超
假设我们要排序字符串d,c,b,bcd,bc,bd,bdc.过程如下:
-0 | 1 | 2 | 3 | 4 | result | reverse
-:----: | :----: | :----: | :----: | :----: | :----: | :----:
-d | **b** | b | | | b | b
-c | dc**b** | d**c**b | cb | | cb | bc
-b | c**b** | **c**b | **d**cb | dcb | dcb | bcd
-dcb | d**b** | **d**b | db | | db | bd
-cb | cd**b** | c**d**b | **c**db | cdb |cdb | bdc
-db | **c** | c | | | c | c
+0 | 1 | 2 | 3 | 4 | result | reverse
+:----: | :----: | :----: | :----: | :----: | :----: | :----:
+d | **b** | b | | | b | b
+c | dc**b** | d**c**b | cb | | cb | bc
+b | c**b** | **c**b | **d**cb | dcb | dcb | bcd
+dcb | d**b** | **d**b | db | | db | bd
+cb | cd**b** | c**d**b | **c**db | cdb |cdb | bdc
+db | **c** | c | | | c | c
cdb | **d** | d | | | d | d
### Problems 4 : Water jugs
diff --git a/C09-Medians-and-Order-Statistics/9.3.md b/C09-Medians-and-Order-Statistics/9.3.md
index 8d79521c..383fd02e 100644
--- a/C09-Medians-and-Order-Statistics/9.3.md
+++ b/C09-Medians-and-Order-Statistics/9.3.md
@@ -1,111 +1,116 @@
-### Exercises 9.3-1
-***
-In the algorithm SELECT, the input elements are divided into groups of 5. Will the algorithm work in linear time if they are divided into groups of 7? Argue that SELECT does not run in linear time if groups of 3 are used.
-### `Answer`
-Assuming each group has k elements.
-
-The number that less(or greater) then median of median is at least n/4-k,In the worst case, next call to SELECT will recursive call 3n/4+k elements.
-
-so
-
-
-Assuming for all n,T(n) <= cn
-
-
-
-So according to 1/k+3/4 <= 1 wo get k >= 4
-
-### Exercises 9.3-2
-***
-Analyze SELECT to show that if n ≥ 140, then at least ⌈n/4⌉ elements are greater than the median-of-medians x and at least ⌈n/4⌉ elements are less than x.
-
-### `Answer`
-
-
-### Exercises 9.3-3
-***
Show how quicksort can be made to run in O() time in the worst case.
-
-### `Answer`
-比较直观,我们先利用线性时间去找到中位数,再用这个中位数去做partition.
-
-It is obvious, we first use O(n) time to find median, then use median to do partition.
-
-
-### Exercises 9.3-4
-***
-Suppose that an algorithm uses only comparisons to find the ith smallest element in a set of n elements. Show that it can also find the i - 1 smaller elements and the n - i larger elements without performing any additional comparisons.
-
+### Exercises 9.3-1
+***
+In the algorithm SELECT, the input elements are divided into groups of 5. Will the algorithm work in linear time if they are divided into groups of 7? Argue that SELECT does not run in linear time if groups of 3 are used.
+### `Answer`
+Assuming each group has k elements.
+
+The number that less(or greater) then median of median is at least n/4-k,In the worst case, next call to SELECT will recursive call 3n/4+k elements.
+
+so
+
+
+Assuming for all n,T(n) <= cn
+
+
+
+So according to 1/k+3/4 <= 1 wo get k >= 4
+
+### Exercises 9.3-2
+***
+Analyze SELECT to show that if n ≥ 140, then at least ⌈n/4⌉ elements are greater than the median-of-medians x and at least ⌈n/4⌉ elements are less than x.
+
+### `Answer`
+
+
+### Exercises 9.3-3
+***
+Show how quicksort can be made to run in O() time in the worst case.
+
### `Answer`
-只用比较来确定的话,就跟这讲的方法是一样的. 我们既然找到了第i小元素,那么比i大和比i小的都已经被我们分类分好了.
-
-We can find ith element, then the number greater than i and the number less than i have been already partitioned.
-
-### Exercises 9.3-5
-***
-Suppose that you have a "black-box" worst-case linear-time median subroutine. Give a simple, linear-time algorithm that solves the selection problem for an arbitrary order statistic.
-
+比较直观,我们先利用线性时间去找到中位数,再用这个中位数去做partition.
+
+It is obvious, we first use O(n) time to find median, then use median to do partition.
+
+
+### Exercises 9.3-4
+***
+Suppose that an algorithm uses only comparisons to find the ith smallest element in a set of n elements. Show that it can also find the i - 1 smaller elements and the n - i larger elements without performing any additional comparisons.
+
### `Answer`
-[code](./exercise_code/black-box.py) tells everything. Thanks original code [here](http://clrs.skanev.com/09/03/05.html).
-
-Find the median, and recursve half of the elements.
-
-
-### Exercises 9.3-6
-***
-The *k*th **quantiles** of an n-element set are the *k* - 1 order statistics that divide the sorted set into *k* equal-sized sets (to within 1). Give an O(n lg k)-time algorithm to list the *k*th quantiles of a set.
+只用比较来确定的话,就跟这讲的方法是一样的. 我们既然找到了第i小元素,那么比i大和比i小的都已经被我们分类分好了.
+
+We can find ith element, then the number greater than i and the number less than i have been already partitioned.
+
+### Exercises 9.3-5
+***
+Suppose that you have a "black-box" worst-case linear-time median subroutine. Give a simple, linear-time algorithm that solves the selection problem for an arbitrary order statistic.
+
### `Answer`
-[code](./exercise_code/k-quantile.py) tells everything. Thanks original code [here](http://clrs.skanev.com/09/03/06.html).
-
-- 如果k是偶数,那么取中间一个,再对左右两边进行递归.
-- 如果k是奇数,取最接近中间的两个数,再对左右两边进行递归.
-
-- If k is even, then choose the middle one, recursive call left part and right part.
-- If k is odd,choose two index numbers most close to median,then recursive call the left part and right part.
-
-
-
-### Exercises 9.3-7
-***
-Describe an O(n)-time algorithm that, given a set S of n distinct numbers and a positive
integer k ≤ n, determines the k numbers in S that are closest to the median of S.
-
### `Answer`
-[code](./exercise_code/k-close2median.py)
-
-1. 计算出中位数median
-2. 将所有数减去median,再取绝对值
-3. 用SELECT计算出第k小数字y
-4. 遍历数组,取出所有绝对值小于等于y的
-
-代码的局限性在于只能接收绝对值不含有相同元素的.
-
-1. Find the median
-2. For all the numbers, minus median then get the absolute value
-3. use SELECT to find the kth smallest number y
-4. Iterate the array choose all the absolute value that less than y
-
-### Exercises 9.3-8
-***
-Let [1 .. n] and Y [1 .. n] be two arrays, each containing n numbers already in sorted order. Give an O(lg n)-time algorithm to find the median of all 2n elements in arrays X and Y.
-### `Answer`
-Divide and conquer
-
+[code](./exercise_code/black-box.py) tells everything. Thanks original code [here](http://clrs.skanev.com/09/03/05.html).
+
+Find the median, and recursve half of the elements.
+
+
+### Exercises 9.3-6
+***
+The *k*th **quantiles** of an n-element set are the *k* - 1 order statistics that divide the sorted set into *k* equal-sized sets (to within 1). Give an O(n lg k)-time algorithm to list the *k*th quantiles of a set.
+### `Answer`
+[code](./exercise_code/k-quantile.py) tells everything. Thanks original code [here](http://clrs.skanev.com/09/03/06.html).
+
+- 如果k是偶数,那么取中间一个,再对左右两边进行递归.
+- 如果k是奇数,取最接近中间的两个数,再对左右两边进行递归.
+
+- If k is even, then choose the middle one, recursive call left part and right part.
+- If k is odd,choose two index numbers most close to median,then recursive call the left part and right part.
+
+
+
+### Exercises 9.3-7
+***
+Describe an O(n)-time algorithm that, given a set S of n distinct numbers and a positive
+integer k ≤ n, determines the k numbers in S that are closest to the median of S.
+
+### `Answer`
+[code](./exercise_code/k-close2median.py)
+
+1. 计算出中位数median
+2. 将所有数减去median,再取绝对值
+3. 用SELECT计算出第k小数字y
+4. 遍历数组,取出所有绝对值小于等于y的
+
+代码的局限性在于只能接收绝对值不含有相同元素的.
+
+1. Find the median
+2. For all the numbers, minus median then get the absolute value
+3. use SELECT to find the kth smallest number y
+4. Iterate the array choose all the absolute value that less than y
+
+### Exercises 9.3-8
+***
+Let [1 .. n] and Y [1 .. n] be two arrays, each containing n numbers already in sorted order. Give an O(lg n)-time algorithm to find the median of all 2n elements in arrays X and Y.
+### `Answer`
+Divide and conquer
+
[code](https://github.com/gzc/leetcode/blob/master/001-010/Median%20of%20Two%20Sorted%20Arrays.cpp)
-
### Exercises 9.3-9
+
+
+### Exercises 9.3-9
+***
+Professor Olay is consulting for an oil company, which is planning a large pipeline running east to west through an oil field of n wells. From each well, a spur pipeline is to be connected directly to the main pipeline along a shortest path (either north or south), as shown in [Figure 9.2](#oil). Given x- and y-coordinates of the wells, how should the professor pick the optimal location of the main pipeline (the one that minimizes the total length of the spurs)? Show that the optimal location can be determined in linear time.
+
+
+
+Figure 9.2: Professor Olay needs to determine the position of the east-west oil pipeline that minimizes the total length of the north-south spurs.
+### `Answer`
+
+Find the median of y.
+
+
***
-Professor Olay is consulting for an oil company, which is planning a large pipeline running east to west through an oil field of n wells. From each well, a spur pipeline is to be connected directly to the main pipeline along a shortest path (either north or south), as shown in [Figure 9.2](#oil). Given x- and y-coordinates of the wells, how should the professor pick the optimal location of the main pipeline (the one that minimizes the total length of the spurs)? Show that the optimal location can be determined in linear time.
-
-
-
-Figure 9.2: Professor Olay needs to determine the position of the east-west oil pipeline that minimizes the total length of the north-south spurs.
-###`Answer`
-
-Find the median of y.
-
-
-***
-Follow [@louis1992](https://github.com/gzc) on github to help finish this task.
-
-
-
-
-
-
+Follow [@louis1992](https://github.com/gzc) on github to help finish this task.
+
+
+
+
+
+
diff --git a/C09-Medians-and-Order-Statistics/problem.md b/C09-Medians-and-Order-Statistics/problem.md
index 4c3d0e47..e7f4cec5 100644
--- a/C09-Medians-and-Order-Statistics/problem.md
+++ b/C09-Medians-and-Order-Statistics/problem.md
@@ -1,93 +1,103 @@
-### Problems 1 : Largest i numbers in sorted order
-***
-
Given a set of *n* numbers, we wish to find the *i* largest in sorted order using a comparison- based algorithm. Find the algorithm that implements each of the following methods with the best asymptotic worst-case running time, and analyze the running times of the algorithms in terms of *n* and *i*.
-
a. Sort the numbers, and list the *i* largest.
b. Build a max-priority queue from the numbers, and call EXTRACT-MAX *i* times.
c. Use an order-statistic algorithm to find the *i*th largest number, partition around that number, and sort the *i* largest numbers.
-
-### `Answer`
-a和b的代码在这里.[code](./problems/i-largest.py).
-如果先排序的话,就需要O(nlgn)+O(i)的时间.
-用堆的话,需要O(n)+O(ilogn)的时间.
-
-对于c,[code](./problems/i-largest.cpp)
-利用顺序统计量的话,找到这个值需要O(n)的时间,然后需要对i个数排序需要O(ilogi)时间.
-
-
-### Problems 2 : Weighted median
-***
-For n distinct elements  with positive weights  such that
-
-,**the weighted (lower)** median is the element  satisfying
-
-
-
-and
-
-
-
-a. Argue that the median of x1, x2, ..., xn is the weighted median of the xi with weights wi = 1/n for i = 1,2, ..., n.
-
b. Show how to compute the weighted median of n elements in O(n lg n) worst-case time using sorting.
-
-c. Show how to compute the weighted median in Θ(n) worst-case time using a linear- time median algorithm such as SELECT from Section 9.3.
-
-The **post-office location problem** is defined as follows. We are given *n* points p1, p2, ..., pn with associated weights w1, w2, ..., wn. We wish to find a point p (not necessarily one of the input points) that minimizes the sum
-
-
-)
-where *d*(a, b) is the distance between points a and b.
d. Argue that the weighted median is a best solution for the 1-dimensional post-office location problem, in which points are simply real numbers and the distance between points a and b is *d*(a, b) = |a - b|.
-
e. Find the best solution for the 2-dimensional post-office location problem, in which the points are (x, y) coordinate pairs and the distance between points a = (x1, y1) and b = (x2, y2) is the **Manhattan distance** given by d(a, b) = |x1 - x2| + |y1 - y2|.
-
-
-### `Answer`
-a. 好显然TAT.
-
-b. 先排序,然后开始遍历,直到加上某一个数字的权重 ≥ 1/2.
-
-c. [code](./problems/weighted_median.py)
-
-d. 假设p是带权中位数,只要偏离p,距离就会越来越大
-
-e. 分别找x和y方向的带权中位数,因为用的是曼哈顿距离,x和y是独立的,所以可以分开对待.
-
-
-### Problems 3 : Small order statistics
-***
-The worst-case number T(n) of comparisons used by SELECT to select the ith order statistic from n numbers was shown to satisfy T(n) = Θ(n), but the constant hidden by the Θ-notation is rather large. When i is small relative to n, we can implement a different procedure that uses SELECT as a subroutine but makes fewer comparisons in the worst case.
-
-a. Describe an algorithm that uses Ui(n) comparisons to find the ith smallest of n elements, where
-(Hint: Begin with
-
-disjoint pairwise comparisons, and recurse on the set containing the smaller element from each pair.)
-
-b. Show that, if i < n/2, then Ui(n) = n + O(T (2i) lg(n/i)).
-
-c. Show that if i is a constant less than n/2, then Ui(n) = n + O(lg n).
-
d. Show that if *i* = n/k for k≥2,then Ui(n) = n+O(T(2n/k)lgk).
-
-### `Answer`
-a.
- 1. 如果i ≥ n/2,直接调用SELECT(n, i)
- 2. 如果i < n/2,那么就两个两个比较,生成了小的一组,同时也记录大的一组(比如可以用map映射起来)
- 3. 对小的一组递归调用这个函数
- 4. 当调用回来时,能找到这小的一组的第i个数,这个数左边都是比它小的数字,再从map中找到这i个数字对应的数字,这样总共有2i个数字,再对这2i个数字调用SELECT.
- 很tricky的一点是为什么最后要调用T(2i),看下面这个数字[1,2,3,4],order-2是2,可是分组完后较小元素是[1,3],所以还要对另外i个数字进行比较.
-
-b.  = \\lfloor n/2 \\rfloor + U_i\(\\lceil n/2 \\rceil\) + T\(2i\) \\nonumber \\\\ ~
-\\hspace{16 mm} = \\lfloor n/2 \\rfloor + \\lceil n/2 \\rceil + O\(T\(2i\)\\lg\(\\lfloor n/2 \\rfloor / i\)\)+T\(2i\) \\\\ ~
-\\hspace{16 mm} = n + O\(T\(2i\)\\lg\(n / i\)\) + T\(2i\) \\\\ ~
-\\hspace{16 mm}= n + O\(T\(2i\)\\lg\(n / i\)\)
-)
-
-c.  = n + O\(T\(2i\)\\lg\(n / i\)\) \\\\ ~\\hspace{16 mm} = n + O\(O\(1\)\\lg\(n / i\)\) \\\\ ~
-\\hspace{16 mm} = n + O\(\\lg\(n\) - \\lg\(i\)\) \\\\ ~
-\\hspace{16 mm} = n + O\(\\lg\(n\) - O\(1\)\) \\\\ ~
-\\hspace{16 mm} = n + O\(\\lg\(n\)\) \\\\ ~
-)
-
-d.  = n + O\(T\(2i\)\\lg\(n / i\)\) \\\\ ~\\hspace{16 mm} = n + O\(T\(2n/k\)\\lg\(n / \(n/k\)\)\) \\\\ ~
-\\hspace{16 mm} = n + O\(T\(2n/k\)\\lg\(k\)\)
-)
-
-
-***
-Follow [@louis1992](https://github.com/gzc) on github to help finish this task.
\ No newline at end of file
+### Problems 1 : Largest i numbers in sorted order
+***
+
+Given a set of *n* numbers, we wish to find the *i* largest in sorted order using a comparison- based algorithm. Find the algorithm that implements each of the following methods with the best asymptotic worst-case running time, and analyze the running times of the algorithms in terms of *n* and *i*.
+
+
+a. Sort the numbers, and list the *i* largest.
+b. Build a max-priority queue from the numbers, and call EXTRACT-MAX *i* times.
+c. Use an order-statistic algorithm to find the *i*th largest number, partition around that number, and sort the *i* largest numbers.
+
+### `Answer`
+a和b的代码在这里.[code](./problems/i-largest.py).
+如果先排序的话,就需要O(nlgn)+O(i)的时间.
+用堆的话,需要O(n)+O(ilogn)的时间.
+
+对于c,[code](./problems/i-largest.cpp)
+利用顺序统计量的话,找到这个值需要O(n)的时间,然后需要对i个数排序需要O(ilogi)时间.
+
+
+### Problems 2 : Weighted median
+***
+For n distinct elements  with positive weights  such that
+
+,**the weighted (lower)** median is the element  satisfying
+
+
+
+and
+
+
+
+a. Argue that the median of x1, x2, ..., xn is the weighted median of the xi with weights wi = 1/n for i = 1,2, ..., n.
+
+b. Show how to compute the weighted median of n elements in O(n lg n) worst-case time using sorting.
+
+c. Show how to compute the weighted median in Θ(n) worst-case time using a linear- time median algorithm such as SELECT from Section 9.3.
+
+The **post-office location problem** is defined as follows. We are given *n* points p1, p2, ..., pn with associated weights w1, w2, ..., wn. We wish to find a point p (not necessarily one of the input points) that minimizes the sum
+
+
+)
+where *d*(a, b) is the distance between points a and b.
+
+d. Argue that the weighted median is a best solution for the 1-dimensional post-office location problem, in which points are simply real numbers and the distance between points a and b is *d*(a, b) = |a - b|.
+
+e. Find the best solution for the 2-dimensional post-office location problem, in which the points are (x, y) coordinate pairs and the distance between points a = (x1, y1) and b = (x2, y2) is the **Manhattan distance** given by d(a, b) = |x1 - x2| + |y1 - y2|.
+
+
+### `Answer`
+a. 好显然TAT.
+
+b. 先排序,然后开始遍历,直到加上某一个数字的权重 ≥ 1/2.
+
+c. [code](./problems/weighted_median.py)
+
+d. 假设p是带权中位数,只要偏离p,距离就会越来越大
+
+e. 分别找x和y方向的带权中位数,因为用的是曼哈顿距离,x和y是独立的,所以可以分开对待.
+
+
+### Problems 3 : Small order statistics
+***
+The worst-case number T(n) of comparisons used by SELECT to select the ith order statistic from n numbers was shown to satisfy T(n) = Θ(n), but the constant hidden by the Θ-notation is rather large. When i is small relative to n, we can implement a different procedure that uses SELECT as a subroutine but makes fewer comparisons in the worst case.
+
+a. Describe an algorithm that uses Ui(n) comparisons to find the ith smallest of n elements, where
+(Hint: Begin with
+
+disjoint pairwise comparisons, and recurse on the set containing the smaller element from each pair.)
+
+b. Show that, if i < n/2, then Ui(n) = n + O(T (2i) lg(n/i)).
+
+c. Show that if i is a constant less than n/2, then Ui(n) = n + O(lg n).
+
+d. Show that if *i* = n/k for k≥2,then Ui(n) = n+O(T(2n/k)lgk).
+
+### `Answer`
+a.
+ 1. 如果i ≥ n/2,直接调用SELECT(n, i)
+ 2. 如果i < n/2,那么就两个两个比较,生成了小的一组,同时也记录大的一组(比如可以用map映射起来)
+ 3. 对小的一组递归调用这个函数
+ 4. 当调用回来时,能找到这小的一组的第i个数,这个数左边都是比它小的数字,再从map中找到这i个数字对应的数字,这样总共有2i个数字,再对这2i个数字调用SELECT.
+ 很tricky的一点是为什么最后要调用T(2i),看下面这个数字[1,2,3,4],order-2是2,可是分组完后较小元素是[1,3],所以还要对另外i个数字进行比较.
+
+b.  = \\lfloor n/2 \\rfloor + U_i\(\\lceil n/2 \\rceil\) + T\(2i\) \\nonumber \\\\ ~
+\\hspace{16 mm} = \\lfloor n/2 \\rfloor + \\lceil n/2 \\rceil + O\(T\(2i\)\\lg\(\\lfloor n/2 \\rfloor / i\)\)+T\(2i\) \\\\ ~
+\\hspace{16 mm} = n + O\(T\(2i\)\\lg\(n / i\)\) + T\(2i\) \\\\ ~
+\\hspace{16 mm}= n + O\(T\(2i\)\\lg\(n / i\)\)
+)
+
+c.  = n + O\(T\(2i\)\\lg\(n / i\)\) \\\\ ~\\hspace{16 mm} = n + O\(O\(1\)\\lg\(n / i\)\) \\\\ ~
+\\hspace{16 mm} = n + O\(\\lg\(n\) - \\lg\(i\)\) \\\\ ~
+\\hspace{16 mm} = n + O\(\\lg\(n\) - O\(1\)\) \\\\ ~
+\\hspace{16 mm} = n + O\(\\lg\(n\)\) \\\\ ~
+)
+
+d.  = n + O\(T\(2i\)\\lg\(n / i\)\) \\\\ ~\\hspace{16 mm} = n + O\(T\(2n/k\)\\lg\(n / \(n/k\)\)\) \\\\ ~
+\\hspace{16 mm} = n + O\(T\(2n/k\)\\lg\(k\)\)
+)
+
+
+***
+Follow [@louis1992](https://github.com/gzc) on github to help finish this task
diff --git a/C10-Elementary-Data-Structures/10.2.md b/C10-Elementary-Data-Structures/10.2.md
index c88eec17..59ee1cb0 100644
--- a/C10-Elementary-Data-Structures/10.2.md
+++ b/C10-Elementary-Data-Structures/10.2.md
@@ -1,72 +1,76 @@
-### Exercises 10.2-1
-***
-Can the dynamic-set operation INSERT be implemented on a singly linked list in O(1) time? How about DELETE?
-
-### `Answer`
-插入的话可以直接插入在开头,删除的话需要遍历.
-
-
-### Exercises 10.2-2
-***
-Implement a stack using a singly linked list L. The operations PUSH and POP should still
take O(1) time.
-
-### `Answer`
-PUSH插入到链表头,POP从链表头操作,都是O(1)的.
-
-### Exercises 10.2-3
-***
-Implement a queue by a singly linked list L. The operations ENQUEUE and DEQUEUE
should still take O(1) time.
-
-### `Answer`
-
-* ENQUEUE插入到链表尾
-* DEQUEUE从链表头取出
-
-### Exercises 10.2-4
-***
-As written, each loop iteration in the LIST-SEARC′ procedure requires two tests: one for x ≠
nil[L] and one for key[x] ≠ k. Show how to eliminate the test for x ≠ nil[L] in each iteration.
-
-### `Answer`
-
- LIST-SEARC′(L, k):
- key[nil[L]] = k
- x ← next[nil[L]]
- while(key[x] != k):
- x ← next[x]
- if x == nil[L]:
- return NULL
- return x
-
-
-### Exercises 10.2-5
-***
-Implement the dictionary operations INSERT, DELETE, and SEARCH using singly linked, circular lists. What are the running times of your procedures?
-
-### `Answer`
-[implementation](./exercise_code/dict.cpp)
-
-### Exercises 10.2-6
-***
+### Exercises 10.2-1
+***
+Can the dynamic-set operation INSERT be implemented on a singly linked list in O(1) time? How about DELETE?
+
+
+### `Answer`
+插入的话可以直接插入在开头,删除的话需要遍历.
+
+
+### Exercises 10.2-2
+***
+Implement a stack using a singly linked list L. The operations PUSH and POP should still
+take O(1) time.
+
+### `Answer`
+PUSH插入到链表头,POP从链表头操作,都是O(1)的.
+
+### Exercises 10.2-3
+***
+Implement a queue by a singly linked list L. The operations ENQUEUE and DEQUEUE
+should still take O(1) time.
+
+### `Answer`
+
+* ENQUEUE插入到链表尾
+* DEQUEUE从链表头取出
+
+### Exercises 10.2-4
+***
+As written, each loop iteration in the LIST-SEARC′ procedure requires two tests: one for x ≠
+nil[L] and one for key[x] ≠ k. Show how to eliminate the test for x ≠ nil[L] in each iteration.
+
+### `Answer`
+
+ LIST-SEARC′(L, k):
+ key[nil[L]] = k
+ x ← next[nil[L]]
+ while(key[x] != k):
+ x ← next[x]
+ if x == nil[L]:
+ return NULL
+ return x
+
+
+### Exercises 10.2-5
+***
+Implement the dictionary operations INSERT, DELETE, and SEARCH using singly linked, circular lists. What are the running times of your procedures?
+
+### `Answer`
+[implementation](./exercise_code/dict.cpp)
+
+### Exercises 10.2-6
+***
The dynamic-set operation UNION takes two disjoint sets S1 and S2 as input, and it returns a set S = S1 U S2 consisting of all the elements of S1 and S2. The sets S1 and S2 are usually destroyed by the operation. Show how to support UNION in O(1) time using a suitable list data structure.
-
-### `Answer`
-如果用链表实现,可以将第二个list连接到第一个list上.
-
-### Exercises 10.2-7
-***
-Give a Θ(n)-time nonrecursive procedure that reverses a singly linked list of n elements. The procedure should use no more than constant storage beyond that needed for the list itself.
-
-### `Answer`
-[solution](https://github.com/gzc/leetcode/blob/master/cpp/201-210/Reverse%20Linked%20List.cpp)
-
-### Exercises 10.2-8
-***
-Explain how to implement doubly linked lists using only one pointer value np[x] per item instead of the usual two (next and prev). Assume that all pointer values can be interpreted as k-bit integers, and define np[x] to be np[x] = next[x] XOR prev[x], the k-bit "exclusive-or" of next[x] and prev[x]. (The value NIL is represented by 0.) Be sure to describe what information is needed to access the head of the list. Show how to implement the SEARCH, INSERT, and DELETE operations on such a list. Also show how to reverse such a list in O(1) time.
-
-### `Answer`
-为了访问下一个元素,只需要XOR(np,prev).为了访问前一个元素,只需要XOR(np,next).
-
-
-***
-Follow [@louis1992](https://github.com/gzc) on github to help finish this task.
-
+
+### `Answer`
+如果用链表实现,可以将第二个list连接到第一个list上.
+
+### Exercises 10.2-7
+***
+Give a Θ(n)-time nonrecursive procedure that reverses a singly linked list of n elements. The procedure should use no more than constant storage beyond that needed for the list itself.
+
+### `Answer`
+[solution](https://github.com/gzc/leetcode/blob/master/cpp/201-210/Reverse%20Linked%20List.cpp)
+
+### Exercises 10.2-8
+***
+Explain how to implement doubly linked lists using only one pointer value np[x] per item instead of the usual two (next and prev). Assume that all pointer values can be interpreted as k-bit integers, and define np[x] to be np[x] = next[x] XOR prev[x], the k-bit "exclusive-or" of next[x] and prev[x]. (The value NIL is represented by 0.) Be sure to describe what information is needed to access the head of the list. Show how to implement the SEARCH, INSERT, and DELETE operations on such a list. Also show how to reverse such a list in O(1) time.
+
+### `Answer`
+为了访问下一个元素,只需要XOR(np,prev).为了访问前一个元素,只需要XOR(np,next).
+
+
+***
+Follow [@louis1992](https://github.com/gzc) on github to help finish this task.
+
diff --git a/C10-Elementary-Data-Structures/10.3.md b/C10-Elementary-Data-Structures/10.3.md
index 3cb6fb1c..d940bb72 100644
--- a/C10-Elementary-Data-Structures/10.3.md
+++ b/C10-Elementary-Data-Structures/10.3.md
@@ -1,80 +1,82 @@
-### Exercises 10.3-1
-***
-Draw a picture of the sequence[13, 4, 8, 19, 5, 11]stored as a doubly linked list using the multiple-array representation. Do the same for the single-array representation.
-
-### `Answer`
-
+---+
-| L |-------------------------------------------+
-+---+ V
- 1 2 3 4 5 6 7 8 9 10 11 12
- +----+----+----+----+----+----+----+----+----+----+----+----+
-next | | | 5 | 7 | 10 | | / | | 3 | 4 | | |
- +----+----+----+----+----+----+----+----+----+----+----+----+
- key | | | 4 | 5 | 8 | | 11 | | 13 | 19 | | |
- +----+----+----+----+----+----+----+----+----+----+----+----+
-prev | | | 8 | 10 | 2 | | 4 | | / | 5 | | |
- +----+----+----+----+----+----+----+----+----+----+----+----+
-
-
-
-+----+----+----+
-| key|next|prev|
-+----+----+----+
-
-13 is head.
-
- 1 2 3 4 5 6 7 8 9 10 11 12
-+----+----+----++----+----+----++----+----+----++----+----+----++--
-| 4 | 7 | 13 || 5 | 10 | 16 || 8 | 16 | 1 || 11 | / | 4 ||
-+----+----+----++----+----+----++----+----+----++----+----+----++--
-
- 13 14 15 16 17 18
- --++----+----+----++----+----+----+
- || 13 | 1 | / || 19 | 4 | 7 |
- --++----+----+----++----+----+----+
-
-
-
-
-### Exercises 10.3-2
-***
+### Exercises 10.3-1
+***
+Draw a picture of the sequence[13, 4, 8, 19, 5, 11]stored as a doubly linked list using the multiple-array representation. Do the same for the single-array representation.
+
+
+### `Answer`
++---+
+| L |-------------------------------------------+
++---+ V
+ 1 2 3 4 5 6 7 8 9 10 11 12
+ +----+----+----+----+----+----+----+----+----+----+----+----+
+next | | | 5 | 7 | 10 | | / | | 3 | 4 | | |
+ +----+----+----+----+----+----+----+----+----+----+----+----+
+ key | | | 4 | 5 | 8 | | 11 | | 13 | 19 | | |
+ +----+----+----+----+----+----+----+----+----+----+----+----+
+prev | | | 8 | 10 | 2 | | 4 | | / | 5 | | |
+ +----+----+----+----+----+----+----+----+----+----+----+----+
+
+
+
++----+----+----+
+| key|next|prev|
++----+----+----+
+
+13 is head.
+
+ 1 2 3 4 5 6 7 8 9 10 11 12
++----+----+----++----+----+----++----+----+----++----+----+----++--
+| 4 | 7 | 13 || 5 | 10 | 16 || 8 | 16 | 1 || 11 | / | 4 ||
++----+----+----++----+----+----++----+----+----++----+----+----++--
+
+ 13 14 15 16 17 18
+ --++----+----+----++----+----+----+
+ || 13 | 1 | / || 19 | 4 | 7 |
+ --++----+----+----++----+----+----+
+
+
+
+
+### Exercises 10.3-2
+***
Write the procedures ALLOCATE-OBJECT and FREE-OBJECT for a homogeneous collection of objects implemented by the single-array representation.
-
-### `Answer`
-[implementation](./exercise_code/af-obj.c)
-
-### Exercises 10.3-3
-***
-Why don't we need to set or reset the prev fields of objects in the implementation of the ALLOCATE-OBJECT and FREE-OBJECT procedures?
-
-### `Answer`
-
-因为这类似于一个栈,没有prev.
-
-### Exercises 10.3-4
-***
-It is often desirable to keep all elements of a doubly linked list compact in storage, using, for example, the first m index locations in the multiple-array representation. (This is the case in a paged, virtual-memory computing environment.) Explain how the procedures ALLOCATE>- OBJECT and FREE-OBJECT can be implemented so that the representation is compact. Assume that there are no pointers to elements of the linked list outside the list itself. (Hint: Use the array implementation of a stack.)
-
-### `Answer`
-
-* ALLOCATE:选free_list的head.
-* FREE-OBJECT:不是直接变成head,而是按sorted的方式插入.
-
-这样可以保证每次分配的时候取排在最前面的空位置
-
-
-
-### Exercises 10.3-5
-***
-Let L be a doubly linked list of length m stored in arrays key, prev, and next of length n. Suppose that these arrays are managed by ALLOCATE-OBJECT and FREE-OBJECT procedures that keep a doubly linked free list F. Suppose further that of the n items, exactly m are on list L and n-m are on the free list. Write a procedure COMPACTIFY-LIST(L, F) that, given the list L and the free list F, moves the items in L so that they occupy array positions 1, 2,..., m and adjusts the free list F so that it remains correct, occupying array positions m + 1, m + 2,..., n. The running time of your procedure should be Θ(n), and it should use only a constant amount of extra space. Give a careful argument for the correctness of your procedure.
-
-### `Answer`
-
-* 先遍历free_list,给每个item的prev标记一下,用来区别L中的item.
-* 两根指针,一根p1在array的头,另一个p2在尾巴.p1向右移动知道遇到free item,p2向左移动直到遇到used item,然后交换p1和p2的内容.遍历一直到p1,p2遇到为止.
-* 重新整理free_list中的元素.
-
-
-***
-Follow [@louis1992](https://github.com/gzc) on github to help finish this task.
-
+
+### `Answer`
+[implementation](./exercise_code/af-obj.c)
+
+### Exercises 10.3-3
+***
+Why don't we need to set or reset the prev fields of objects in the implementation of the ALLOCATE-OBJECT and FREE-OBJECT procedures?
+
+### `Answer`
+
+因为这类似于一个栈,没有prev.
+
+### Exercises 10.3-4
+***
+It is often desirable to keep all elements of a doubly linked list compact in storage, using, for example, the first m index locations in the multiple-array representation. (This is the case in a paged, virtual-memory computing environment.) Explain how the procedures ALLOCATE>- OBJECT and FREE-OBJECT can be implemented so that the representation is compact. Assume that there are no pointers to elements of the linked list outside the list itself. (Hint: Use the array implementation of a stack.)
+
+
+### `Answer`
+
+* ALLOCATE:选free_list的head.
+* FREE-OBJECT:不是直接变成head,而是按sorted的方式插入.
+
+这样可以保证每次分配的时候取排在最前面的空位置
+
+
+
+### Exercises 10.3-5
+***
+Let L be a doubly linked list of length m stored in arrays key, prev, and next of length n. Suppose that these arrays are managed by ALLOCATE-OBJECT and FREE-OBJECT procedures that keep a doubly linked free list F. Suppose further that of the n items, exactly m are on list L and n-m are on the free list. Write a procedure COMPACTIFY-LIST(L, F) that, given the list L and the free list F, moves the items in L so that they occupy array positions 1, 2,..., m and adjusts the free list F so that it remains correct, occupying array positions m + 1, m + 2,..., n. The running time of your procedure should be Θ(n), and it should use only a constant amount of extra space. Give a careful argument for the correctness of your procedure.
+
+### `Answer`
+
+* 先遍历free_list,给每个item的prev标记一下,用来区别L中的item.
+* 两根指针,一根p1在array的头,另一个p2在尾巴.p1向右移动知道遇到free item,p2向左移动直到遇到used item,然后交换p1和p2的内容.遍历一直到p1,p2遇到为止.
+* 重新整理free_list中的元素.
+
+
+***
+Follow [@louis1992](https://github.com/gzc) on github to help finish this task.
+
diff --git a/C11-Hash-Tables/11.2.md b/C11-Hash-Tables/11.2.md
index 0911eb10..b0b147d5 100644
--- a/C11-Hash-Tables/11.2.md
+++ b/C11-Hash-Tables/11.2.md
@@ -1,42 +1,46 @@
-### Exercises 11.2-1
-***
-Suppose we use a hash function h to hash n distinct keys into an array T of length m. Assuming simple uniform hashing, what is the expected number of collisions? More precisely, what is the expected cardinality of {{k, l} : k ≠ l and h(k) = h(l)}?
-
-### `Answer`
-
-
-
-### Exercises 11.2-2
-***
+### Exercises 11.2-1
+***
+Suppose we use a hash function h to hash n distinct keys into an array T of length m. Assuming simple uniform hashing, what is the expected number of collisions? More precisely, what is the expected cardinality of {{k, l} : k ≠ l and h(k) = h(l)}?
+
+
+### `Answer`
+
+
+
+### Exercises 11.2-2
+***
Demonstrate the insertion of the keys 5, 28, 19, 15, 20, 33, 12, 17, 10 into a hash table with collisions resolved by chaining. Let the table have 9 slots, and let the hash function be h(k) = k mod 9.
-
-### `Answer`
-
-
-### Exercises 11.2-3
-***
+
+### `Answer`
+
+
+### Exercises 11.2-3
+***
Professor Marley hypothesizes that substantial performance gains can be obtained if we modify the chaining scheme so that each list is kept in sorted order. How does the professor's modification affect the running time for successful searches, unsuccessful searches, insertions, and deletions?
-
-### `Answer`
-* successful searches:没有影响
-* unsuccessful searches:当数据量大可以加速,可以提前判断元素是否在区间内
-* insertions:降低了插入的速度,需要遍历链表插入在合适的位置
-* deletions:没有影响
-
-### Exercises 11.2-4
-***
-Suggest how storage for elements can be allocated and deallocated within the hash table itself by linking all unused slots into a free list. Assume that one slot can store a flag and either one element plus a pointer or two pointers. All dictionary and free-list operations should run in O(1) expected time. Does the free list need to be doubly linked, or does a singly linked free list suffice?
-
-### `Answer`
-需要双链表.每个slot有一个标识标识是否已分配,如果没有分配指针指向free list的属于自己的那个位置. 删除的时候将标志位清一下,将自己加入链表头,指针指向头;插入时根据指针去取.
-
-### Exercises 11.2-5
-***
-Show that if |U| > nm, there is a subset of U of size n consisting of keys that all hash to the
same slot, so that the worst-case searching time for hashing with chaining is Θ(n).
-
### `Answer`
+
+### `Answer`
+* successful searches:没有影响
+* unsuccessful searches:当数据量大可以加速,可以提前判断元素是否在区间内
+* insertions:降低了插入的速度,需要遍历链表插入在合适的位置
+* deletions:没有影响
+
+### Exercises 11.2-4
+***
+Suggest how storage for elements can be allocated and deallocated within the hash table itself by linking all unused slots into a free list. Assume that one slot can store a flag and either one element plus a pointer or two pointers. All dictionary and free-list operations should run in O(1) expected time. Does the free list need to be doubly linked, or does a singly linked free list suffice?
+
+### `Answer`
+需要双链表.每个slot有一个标识标识是否已分配,如果没有分配指针指向free list的属于自己的那个位置. 删除的时候将标志位清一下,将自己加入链表头,指针指向头;插入时根据指针去取.
+
+### Exercises 11.2-5
+***
+Show that if |U| > nm, there is a subset of U of size n consisting of keys that all hash to the
+same slot, so that the worst-case searching time for hashing with chaining is Θ(n).
+
+
+### `Answer`
如果|U| = nm,假设U的全集要均匀分到m个位置上,每个位置期望就有n个元素,因此至少有一个位置是有至少n个元素的,我们选取这个集合,查找操作需要的时间就是Θ(n).
-
-
-***
-Follow [@louis1992](https://github.com/gzc) on github to help finish this task.
-
+
+
+***
+Follow [@louis1992](https://github.com/gzc) on github to help finish this task.
+
diff --git a/C11-Hash-Tables/problem.md b/C11-Hash-Tables/problem.md
index f859142c..dd67c797 100644
--- a/C11-Hash-Tables/problem.md
+++ b/C11-Hash-Tables/problem.md
@@ -1,50 +1,62 @@
### Problems 1 : Longest-probe bound for hashing
-***
-A hash table of size m is used to store n items, with n ≤ m/2. Open addressing is used for collision resolution.
-
**a.**Assuming uniform hashing, show that for i=1,2,…,n, the probability is at most 2^−k that the ith insertion requires strictly more than k probes.
-
**b.**Show that for i=1,2,…,n, the probability is O(1/n^2) that the ith insertion requires more than 2lgn probes.
-
Let the random variable Xi denote the number of probes required by the ith insertion. You have shown in part (b) that
- )
. Let the random variable
-
denote the maximum number of probes required by any of the n insertions.
-
**c.**Show that Pr{X > 2lgn}=O(1/n).
-
**d.**Show that the expected length E[X] of the longest probe sequence is O(lgn).
-### `Answer`
-**a.**
-
-P = (n/m)^k < (1/2)^k = 2^-k
-
-**b.**
-
-代入a中的结论即可
-
-**c.**
-
- )
-
-**d.**
-
-该题可以参考5.4.2节的关于**球与盒子**的结论和5.4.3节的关于**序列**的结论.
-
-我们把每次的概率放大为1/2(实际上是≤ 1/2的)
-
-所以是O(lgn)
-
-
-
+***
+A hash table of size m is used to store n items, with n ≤ m/2. Open addressing is used for collision resolution.
+
+**a.**Assuming uniform hashing, show that for i=1,2,…,n, the probability is at most 2^−k that the ith insertion requires strictly more than k probes.
+
+
+**b.**Show that for i=1,2,…,n, the probability is O(1/n^2) that the ith insertion requires more than 2lgn probes.
+
+Let the random variable Xi denote the number of probes required by the ith insertion. You have shown in part (b) that
+ )
+. Let the random variable
+
+denote the maximum number of probes required by any of the n insertions.
+
+**c.**Show that Pr{X > 2lgn}=O(1/n).
+
+**d.**Show that the expected length E[X] of the longest probe sequence is O(lgn).
+
+### `Answer`
+**a.**
+
+P = (n/m)^k < (1/2)^k = 2^-k
+
+**b.**
+
+代入a中的结论即可
+
+**c.**
+
+ )
+
+**d.**
+
+该题可以参考5.4.2节的关于**球与盒子**的结论和5.4.3节的关于**序列**的结论.
+
+我们把每次的概率放大为1/2(实际上是≤ 1/2的)
+
+所以是O(lgn)
+
+
+
### Problems 2 : Slot-size bound for chaining
-***
-Suppose that we have a hash table with n slots, with collisions resolved by chaining, and suppose that n keys are inserted into the table. Each key is equally likely to be hashed to each slot. Let M be the maximum number of keys in any slot after all the keys have been inserted. Your mission is to prove an O(lg n/lg lg n) upper bound on E[M], the expected value of M.
-
-**a.**
-Argue that the probability Qk that exactly k keys hash to a particular slot is given by
-
- \(1-\\frac{1}{n}\)^{n-k} C_k^n)
-
-### `Answer`
-
-
-
-***
-Follow [@louis1992](https://github.com/gzc) on github to help finish this task.
-
+***
+Suppose that we have a hash table with n slots, with collisions resolved by chaining, and suppose that n keys are inserted into the table. Each key is equally likely to be hashed to each slot. Let M be the maximum number of keys in any slot after all the keys have been inserted. Your mission is to prove an O(lg n/lg lg n) upper bound on E[M], the expected value of M.
+
+**a.**
+Argue that the probability Qk that exactly k keys hash to a particular slot is given by
+
+ \(1-\\frac{1}{n}\)^{n-k} C_k^n)
+
+
+### `Answer`
+
+
+
+***
+Follow [@louis1992](https://github.com/gzc) on github to help finish this task.
+
diff --git a/C12-Binary-Search-Trees/12.3.md b/C12-Binary-Search-Trees/12.3.md
index 8fd927f7..0a33c3e5 100644
--- a/C12-Binary-Search-Trees/12.3.md
+++ b/C12-Binary-Search-Trees/12.3.md
@@ -1,59 +1,59 @@
-### Exercises 12.3-1
-***
-Give a recursive version of the TREE-INSERT procedure.
-
-### `Answer`
-
-
-
-### Exercises 12.3-2
-***
-Suppose that a binary search tree is constructed by repeatedly inserting distinct values into the tree. Argue that the number of nodes examined in searching for a value in the tree is one plus the number of nodes examined when the value was first inserted into the tree.
-
-### `Answer`
-多检查一次是否相等.
-
-We should check one more time of this node, obviously.
-
-### Exercises 12.3-3
-***
-We can sort a given set of n numbers by first building a binary search tree containing these numbers (using TREE-INSERT repeatedly to insert the numbers one by one) and then printing the numbers by an inorder tree walk. What are the worst-case and best-case running times for this sorting algorithm?
-
-### `Answer`
-退化成链表就是最坏情况.
-
-If it's Degenerated into a list, then the worst situation.
-
-
-### Exercises 12.3-4
+### Exercises 12.3-1
+***
+Give a recursive version of the TREE-INSERT procedure.
+
+### `Answer`
+
+
+
+### Exercises 12.3-2
+***
+Suppose that a binary search tree is constructed by repeatedly inserting distinct values into the tree. Argue that the number of nodes examined in searching for a value in the tree is one plus the number of nodes examined when the value was first inserted into the tree.
+
+### `Answer`
+多检查一次是否相等.
+
+We should check one more time of this node, obviously.
+
+### Exercises 12.3-3
+***
+We can sort a given set of n numbers by first building a binary search tree containing these numbers (using TREE-INSERT repeatedly to insert the numbers one by one) and then printing the numbers by an inorder tree walk. What are the worst-case and best-case running times for this sorting algorithm?
+
+### `Answer`
+退化成链表就是最坏情况.
+
+If it's Degenerated into a list, then the worst situation.
+
+
+### Exercises 12.3-4
Suppose that another data structure contains a pointer to a node y in a binary search tree, and suppose that y's predecessor z is deleted from the tree by the procedure TREE-DELETE. What problem can arise? How can TREE-DELETE be rewritten to solve this problem?
-### `Answer`
-当要删除的节点有两个子节点时,节点y会被删除. 如果指向的是节点y,那么就出问题的.在这种情况下应该修改成指向节点z.
-
-If the node being deleted has two child nodes, then y will be deleted. If the pointer point to y, there will be problem. Under such situation, the pointer should point to node z.
-
-
-### Exercises 12.3-5
-***
+### `Answer`
+当要删除的节点有两个子节点时,节点y会被删除. 如果指向的是节点y,那么就出问题的.在这种情况下应该修改成指向节点z.
+
+If the node being deleted has two child nodes, then y will be deleted. If the pointer point to y, there will be problem. Under such situation, the pointer should point to node z.
+
+
+### Exercises 12.3-5
+***
Is the operation of deletion "commutative" in the sense that deleting x and then y from a binary search tree leaves the same tree as deleting y and then x? Argue why it is or give a counterexample.
-
-### `Answer`
-NO.
-
-
-
-### Exercises 12.3-6
-***
+
+### `Answer`
+NO.
+
+
+
+### Exercises 12.3-6
+***
When node z in TREE-DELETE has two children, we could splice out its predecessor rather than its successor. Some have argued that a fair strategy, giving equal priority to predecessor and successor, yields better empirical performance. How might TREE-DELETE be changed to implement such a fair strategy?
-
-### `Answer`
-
-* Randomly choose one
-* Assign each node with an attribute **height**, choose the higher one.
-
-
-
-
-***
-Follow [@louis1992](https://github.com/gzc) on github to help finish this task.
-
+
+### `Answer`
+
+* Randomly choose one
+* Assign each node with an attribute **height**, choose the higher one.
+
+
+
+
+***
+Follow [@louis1992](https://github.com/gzc) on github to help finish this task.
+
diff --git a/C13-Red-Black-Trees/13.1.md b/C13-Red-Black-Trees/13.1.md
index 54d5a7dd..eae0f7ef 100644
--- a/C13-Red-Black-Trees/13.1.md
+++ b/C13-Red-Black-Trees/13.1.md
@@ -1,63 +1,64 @@
-### Exercises 13.1-1
-***
+### Exercises 13.1-1
+***
In the style of Figure 13.1(a), draw the complete binary search tree of height 3 on the keys {1, 2, ..., 15}. Add the NIL leaves and color the nodes in three different ways such that the black- heights of the resulting red-black trees are 2, 3, and 4.
-
-### `Answer`
-因为是一颗完全二叉树,超级平衡,所以填色很容易. 感谢[psu](http://test.scripts.psu.edu/users/d/j/djh300/cmpsc465/notes-4985903869437/solutions-to-some-homework-exercises-as-shared-with-students/3-solutions-clrs-13.pdf)提供的图片
-
-
-
-### Exercises 13.1-2
-***
-Draw the red-black tree that results after TREE-INSERT is called on the tree in Figure 13.1 with key 36. If the inserted node is colored red, is the resulting tree a red-black tree? What if it is colored black?
-
-### `Answer`
-插入后如何上红色,那么违反了红节点的儿子节点是黑色这个规则.
-
-
-插入后如何上黑色,那么违反了路径上包含相同黑节点数这个规则.
-
-
-
-### Exercises 13.1-3
-***
-Let us define a **relaxed red-black tree** as a binary search tree that satisfies red- black properties 1, 3, 4, and 5. In other words, the root may be either red or black. Consider a relaxed red-black tree *T* whose root is red. If we color the root of *T* black but make no other changes to *T*, is the resulting tree a red-black tree?
-
-### `Answer`
-当然还是~
-
-### Exercises 13.1-4
-***
-Suppose that we "absorb" every red node in a red-black tree into its black parent, so that the children of the red node become children of the black parent. (Ignore what happens to the keys.) What are the possible degrees of a black node after all its red children are absorbed? What can you say about the depths of the leaves of the resulting tree?
-### `Answer`
-* 2, 如果该节点的两个子结点都是黑的.
-* 3, 如果仅有一个子结点是红的.
-* 4, 如果两个子结点都是红的.
-
-所有的叶子节点都有相同的高度.
-
-### Exercises 13.1-5
-***
-Show that the longest simple path from a node *x* in a red-black tree to a descendant leaf has
length at most twice that of the shortest simple path from node x to a descendant leaf.
-### `Answer`
-根据性质5,最长和最短路径都有相同的黑节点数. 根据性质4可知路径上红节点数目不会超过黑节点,可得.
-
-### Exercises 13.1-6
-***
-What is the largest possible number of internal nodes in a red-black tree with black-height *k*? What is the smallest possible number?
-
-### `Answer`
-假如有一颗完美二叉树,如果每个节点都是黑的,那么总的节点个数是
-假如是一黑一红交替,那么总高度就是2k-1,总节点个数是
-
-
-### Exercises 13.1-7
-***
-Describe a red-black tree on *n* keys that realizes the largest possible ratio of red internal nodes to black internal nodes. What is this ratio? What tree has the smallest possible ratio, and what is the ratio?
-
-* 最大的比值是2,根节点是黑色,两个子节点是红色.
-* 最小的比值是0,比如只有一个黑色的根节点.
-
-***
-Follow [@louis1992](https://github.com/gzc) on github to help finish this task.
-
+
+### `Answer`
+因为是一颗完全二叉树,超级平衡,所以填色很容易. 感谢[psu](http://test.scripts.psu.edu/users/d/j/djh300/cmpsc465/notes-4985903869437/solutions-to-some-homework-exercises-as-shared-with-students/3-solutions-clrs-13.pdf)提供的图片
+
+
+
+### Exercises 13.1-2
+***
+Draw the red-black tree that results after TREE-INSERT is called on the tree in Figure 13.1 with key 36. If the inserted node is colored red, is the resulting tree a red-black tree? What if it is colored black?
+
+### `Answer`
+插入后如何上红色,那么违反了红节点的儿子节点是黑色这个规则.
+
+
+插入后如何上黑色,那么违反了路径上包含相同黑节点数这个规则.
+
+
+
+### Exercises 13.1-3
+***
+Let us define a **relaxed red-black tree** as a binary search tree that satisfies red- black properties 1, 3, 4, and 5. In other words, the root may be either red or black. Consider a relaxed red-black tree *T* whose root is red. If we color the root of *T* black but make no other changes to *T*, is the resulting tree a red-black tree?
+
+### `Answer`
+当然还是~
+
+### Exercises 13.1-4
+***
+Suppose that we "absorb" every red node in a red-black tree into its black parent, so that the children of the red node become children of the black parent. (Ignore what happens to the keys.) What are the possible degrees of a black node after all its red children are absorbed? What can you say about the depths of the leaves of the resulting tree?
+### `Answer`
+* 2, 如果该节点的两个子结点都是黑的.
+* 3, 如果仅有一个子结点是红的.
+* 4, 如果两个子结点都是红的.
+
+所有的叶子节点都有相同的高度.
+
+### Exercises 13.1-5
+***
+Show that the longest simple path from a node *x* in a red-black tree to a descendant leaf has
+length at most twice that of the shortest simple path from node x to a descendant leaf.
+### `Answer`
+根据性质5,最长和最短路径都有相同的黑节点数. 根据性质4可知路径上红节点数目不会超过黑节点,可得.
+
+### Exercises 13.1-6
+***
+What is the largest possible number of internal nodes in a red-black tree with black-height *k*? What is the smallest possible number?
+
+### `Answer`
+假如有一颗完美二叉树,如果每个节点都是黑的,那么总的节点个数是
+假如是一黑一红交替,那么总高度就是2k-1,总节点个数是
+
+
+### Exercises 13.1-7
+***
+Describe a red-black tree on *n* keys that realizes the largest possible ratio of red internal nodes to black internal nodes. What is this ratio? What tree has the smallest possible ratio, and what is the ratio?
+
+* 最大的比值是2,根节点是黑色,两个子节点是红色.
+* 最小的比值是0,比如只有一个黑色的根节点.
+
+***
+Follow [@louis1992](https://github.com/gzc) on github to help finish this task.
+
diff --git a/C13-Red-Black-Trees/problem.md b/C13-Red-Black-Trees/problem.md
index 6fd545d7..d7000b4f 100644
--- a/C13-Red-Black-Trees/problem.md
+++ b/C13-Red-Black-Trees/problem.md
@@ -1,98 +1,137 @@
-### Problems 1 : Persistent dynamic sets
-***
-
a. For a general persistent binary search tree, identify the nodes that need to be changed to insert a key k or delete a node y.
-
b. Write a procedure PERSISTENT-TREE-INSERT that, given a persistent tree T and a key k to insert, returns a new persistent tree T′ that is the result of inserting k into T.
-
c. If the height of the persistent binary search tree T is h, what are the time and space requirements of your implementation of PERSISTENT-TREE-INSERT? (The space requirement is proportional to the number of new nodes allocated.)
-
d. Suppose that we had included the parent field in each node. In this case, PERSISTENT-TREE-INSERT would need to perform additional copying. Prove that PERSISTENT-TREE-INSERT would then require Ω(n) time and space, where n is the number of nodes in the tree.
-
e. Show how to use red-black trees to guarantee that the worst-case running time and space are O(lg n) per insertion or deletion.
-
-### `Answer`
-a. 插入的时候,需要改变根节点到这个叶子节点路径上的所有节点. BST删除有3种情况,如果删除的是根节点,那么需要改变根节点到这个叶子节点路径上的所有节点; 如果删除的节点只有一个子节点,那么需要改变根节点到这个删除节点路径之间的所有节点; 如果删除的节点有两个子节点,那么也是需要修改被影响的路径.只有一条路径被影响,因为某个节点和其后继肯定是同一条路径的.
-
-b. 先定义两个操作. MAKE-NEW-NODE(k) 创建一个键值为k的新节点,左儿子和右儿子默认为NIL,并返回这个节点;COPY-NODE(x) 创建一个和x一模一样的节点并返回.
-r是根节点
-
-
-c. O(h)的时间和空间
-
-d. 很好理解,因为根节点变了,所以子节点要指向新的节点,必然也要创建新的子节点.
-
-e. 首先要认识到最重要的两点. 1. 根据d可知节点不能有parent属性,因此我们在RB-INSERT的时候需要用stack去保存从根到叶子节点,然后将这个stack作为参数传到RB-INSERT-FIXUP或者RB-DELETE. 2. 旋转或重新着色不会改变超过O(lgn)的节点. - 1 - - / | \ \ - 2 3 5 9 - - | / \ / / \ - 4 6 7 10 11 13 - - | | / \ - 8 12 14 15 - - | - 16 -- -### Exercises 21.3-2 -*** Write a nonrecursive version of FIND-SET with path compression. - -### `Answer` -[implementation](./uf.cpp) - -### Exercises 21.3-3 -*** +### Exercises 21.3-1 +*** +Do Exercise 21.2-2 using a disjoint-set forest with union by rank and path compression. + +### `Answer` + +
+ 1 + + / | \ \ + 2 3 5 9 + + | / \ / / \ + 4 6 7 10 11 13 + + | | / \ + 8 12 14 15 + + | + 16 ++ +### Exercises 21.3-2 +*** +Write a nonrecursive version of FIND-SET with path compression. + +### `Answer` +[implementation](./uf.cpp) + +### Exercises 21.3-3 +*** Give a sequence of m MAKE-SET, UNION, and FIND-SET operations, n of which are MAKE-SET operations, that takes Ω(m lg n) time when we use union by rank only. - -### `Answer` -[reference](http://www.cs.toronto.edu/~avner/teaching/263/A/4sol.pdf) - - - -### Exercises 21.3-4 * -*** + +### `Answer` +[reference](http://www.cs.toronto.edu/~avner/teaching/263/A/4sol.pdf) + + + +### Exercises 21.3-4 * +*** Show that any sequence of m MAKE-SET, FIND-SET, and LINK operations, where all the LINK operations appear before any of the FIND-SET operations, takes only O(m) time if both path compression and union by rank are used. What happens in the same situation if only the path-compression heuristic is used? - -### `Answer` -UNSOLVED -*** -Follow [@louis1992](https://github.com/gzc) on github to help finish this task. - +### `Answer` +UNSOLVED + +*** +Follow [@louis1992](https://github.com/gzc) on github to help finish this task. + diff --git a/C22-Elementary-Graph-Algorithms/22.2.md b/C22-Elementary-Graph-Algorithms/22.2.md index df915176..17ad22b7 100644 --- a/C22-Elementary-Graph-Algorithms/22.2.md +++ b/C22-Elementary-Graph-Algorithms/22.2.md @@ -1,86 +1,111 @@ -### Exercises 22.2-1 -*** -Show the d and π values that result from running breadth-first search on the directed graph of Figure 22.2(a), using vertex 3 as the source. -### `Answer` - - -### Exercises 22.2-2 -*** -Show the d and π values that result from running breadth-first search on the undirected graph of Figure 22.3, using vertex u as the source. - -### `Answer` - - -### Exercises 22.2-3 -*** -What is the running time of BFS if its input graph is represented by an adjacency matrix and the algorithm is modified to handle this form of input? - -### `Answer` +### Exercises 22.2-1 +*** +Show the d and π values that result from running breadth-first search on the directed graph of Figure 22.2(a), using vertex 3 as the source. + + +### `Answer` + + +### Exercises 22.2-2 +*** +Show the d and π values that result from running breadth-first search on the undirected graph +of Figure 22.3, using vertex u as the source. + +### `Answer` + + +### Exercises 22.2-3 +*** +What is the running time of BFS if its input graph is represented by an adjacency matrix and the algorithm is modified to handle this form of input? + + +### `Answer` 用邻接矩阵表示的话,遍历所有的边的时间由O(E)变为O(V^2),因此总运行时间是O(V+V^2) - -If the graph is represented by adjancency matrix, then the time of iterating all the edge becomes O(V^2), so the running time is O(V+V^2). - -### Exercises 22.2-4 -*** -Argue that in a breadth-first search, the value d[u] assigned to a vertex u is independent of the order in which the vertices in each adjacency list are given. Using Figure 22.3 as an example, show that the breadth-first tree computed by BFS can depend on the ordering within adjacency lists. - -### `Answer` -这些结论很明显,d[u]是最短路径的大小自然和顺序无关,而最短的路径可以有很多条自然和顺序有关. - -It's obvious, d[u] represents the distance, so it is of course has nothing to do with order. But the shortest path may be not unique, so it is relevant to order. - - -### Exercises 22.2-5 -*** -Give an example of a directed graph G = (V, E), a source vertex s in V , and a set of tree edges Eπ in E such that for each vertex v in V,the unique path in the graph(V,Eπ) from s to v is a shortest path in G, yet the set of edges Eπ cannot be produced by running BFS on G, no matter how the vertices are ordered in each adjacency list. - ### `Answer` -Following is the example, the bold edges are in Eπ. Then can not be produced by BFS. - - - -### Exercises 22.2-6 -*** -There are two types of professional wrestlers: "good guys" and "bad guys." Between any pair of professional wrestlers, there may or may not be a rivalry. Suppose we have n professional wrestlers and we have a list of r pairs of wrestlers for which there are rivalries. Give an O(n + r)-time algorithm that determines whether it is possible to designate some of the wrestlers as good guys and the remainder as bad guys such that each rivalry is between a good guy and a bad guy. If is it possible to perform such a designation, your algorithm should produce it. - -### `Answer` -Bipartite Graph problem. - -Run DFS or BFS, the start node we could mark as white. Each time, we encounter a node, if it is not currently colored, we should mark it as the opposite color of current node. Else, check the color with current node. If same, then it is not bipartite graph. - - -### Exercises 22.2-7 -*** -The **diameter** of a tree T =(V, E) is given by - - max d(u,v) that is, the diameter is the largest of all shortest-path distances in the tree. Give an efficient algorithm to compute the diameter of a tree, and analyze the running time of your algorithm. - ### `Answer` - -#####Solution 1 : - -For all node v, run BFS each, choose the longest shortest path. Or use floyd algorithm to calculate all p-p shortest path. BFS running time = O(V*(V+E)) floyd running time = O(V^3) - -
w(a1) <= w(a2) <= ... w(an)
w(b1) <= w(b2) <= ... w(bn)
w(a1) <= w(a2) <= ... w(an)
w(b1) <= w(b2) <= ... w(bn)