80 likes | 85 Views
Learn about QuickSort and QuickSelect algorithms with random pivots, recursion, rejection sampling, and Monte Carlo simulations. Understand randomized algorithms for sorting and finding the k-th smallest number efficiently. Challenges like biased coin simulation and area computation using Monte Carlo are also explored.
E N D
Quicksort • Goal: Sort a list of numbersa[] = {4, 2, 8, 6, 3, 1, 7, 5} • Algorithm: • Pick a random pivot number (say 3) • Partition the array into numbers smaller and larger than the pivot ({2, 1}, {4, 8, 6, 7, 5}) • Recursively sort the two parts.
Recursion • Consider the possible choices for the first pivot • Let Xn be a random variable that represents the running time of QuickSort on n numbers. Split cost Right Part Left Part
QuickSelection • Goal: Given an array of numbersFind the k-th smallest number. • Example: • a[] = {4, 2, 8, 6, 3, 1, 7, 5}k = 3 • Output = 3
Recursion • Consider the possible choices for the first pivot • Let Xn be a random variable that represents the running time of QuickSelect on n numbers. Right Part Left Part Split cost
Rejection Sampling • Problem: Given a random variable X,Pr[X=i] = piwant to generate a random variable Y, such thatPr[Y=i] = qi • Rejection sampling: Sample X, then with probability , keep the sampleotherwise throw the sample away.
Example: Biased Coin • Suppose we only have a biased coin (the coin lands on heads with probability p > ½). How do we simulate a fair coin? • [Von Neumann] Toss the coin twice, if the result is HT, claim Heads;if the result is TH, claim Tails;if the result is HH or TT, retry. • Question: How many coin tosses do we need to do until we get the result?
Monte Carlo Algorithm • Problem: Compute the areaof a circle. • Monte-Carlo algorithm Count = 0 FORi = 1 to n Generate x, y from [-1,1] IF x2+y2 <= 1 THEN Count = Count + 1 RETURN 4.0*Count/n