Partition(A, p, r) x = A[r] i = p - 1 for j = p to r - 1 if A[j] <= x i = i + 1 exchange A[i] with A[j] exchange A[i + 1] with A[r] return i + 1

1.0 - Average Case Analysis

1.1 - Hire Assistant Example

hire_assistant(n) best = 0 for j = 1 to n interview candidate j // at cost ci if candidate j is better than the best candidate best = j hire candidate j // at cost ch

Let n:n: total number of candidates

  $m:$ number of candidates hired

Our actual cost is nci+mchn\cdot c_i+m\cdot c_h

Our worst case is n(ci+ch)n(c_i+c_h) is when m=nm=n

This occurs when the candidates ordered from worst to best - they are all hired and then replaced

Our best case occurs when the candidates are ordered from best to worst

1.1.1 - Average Case Analysis

1.2 - Randomised Algorithms

randomised-hire-assistant(n) // randomly permute the list of candidates for j = 1 to n interview candidate j // at cost ci if candidate j is better than the best candidate best = j hire candidate j // at cost ch
permute-by-sort(A) n = A.length let P[1..n] be a new array for i = 1 to n P[i] = random(1, n^3) sort A, using P as the sort keys

2.0 - Analysis of Comparison Based Sorting Algos

2.1 - Quick-Sort

2.1.1 - Partition

2.2.2 - Analysis of Quick Sort

2.2.3 - Intuition for “Constant Ratio Split”

2.2.4 - Average Case Analysis for Quick-Sort

{1,2,3} and {5,6,7,8,9,10} \{1, 2, 3\}\text{ and } \{5, 6, 7, 8, 9, 10\}

**********************Probability ziz_i compared with zjz_j

Analysis of Quick Sort

Number of Comparisons over all calls to Partition

i=1n1j=i+1nPr{zi is compared with zj}=i=1n1j=i+1n2ji+1=i=1n1j=i+1n2k+1,Let k=ji<i=1n1k=1n2k=i=1n1O(lgn),Harmonic Series=O(nlgn) \begin{aligned} &\sum_{{\color{lightblue}i}=1}^{n-1}\sum_{{\color{lightgreen}j}=i+1}^{n}\text{Pr\{}z_i\text{ is compared with } z_j\}\\ =&\sum_{{\color{lightblue}i}=1}^{n-1}\sum_{{\color{lightgreen}j}=i+1}^{n}\frac{2}{j-i+1}\\ =&\sum_{{\color{lightblue}i}=1}^{n-1}\sum_{{\color{lightgreen}j}=i+1}^{n}\frac{2}{k+1},&\text{Let } k = j-i\\ <&\sum_{{\color{lightblue}i}=1}^{n-1}\sum_{k=1}^{n}\frac{2}{k}\\ =&\sum_{{\color{lightblue}i}=1}^{n-1}O(\lg n ),&\text{Harmonic Series}\\ =&O(n \lg n) \end{aligned}

2.2.5 - Randomised Quick-Sort

2.2 - Merge Sort

3.0 - Randomised Algorithm Example

💡 Given a procedure, biased-random() that returns 0 with probability pp and 11 with probability 1p1-p, where 0<p<10<p<1, how can you implement a procedure random\text{random} that returns 0 or 1 with equal probability

3.1 - Algorithm Construction

random() a = biased-random()

3.2 - Expected Running Time