In a flower arrangement, a gardener put n roses and n violets in a row. The firs
ID: 3675213 • Letter: I
Question
In a flower arrangement, a gardener put n roses and n violets in a row. The first n flowers are all roses and the next n are all violets, but the hostess wants the flowers to be put in alternating colors. Use a decrease-and-conquer algorithm to find the minimum number of moves that the gardener needs to make to form the wanted pattern. Topologically sort the nodes in the following digraph using The DFS-based algorithm. List the stack operations and the result of the sorting. Source removing algorithm. List the node removed at each step. Generate all permutation of (a. b, c, d} using the bottom-up minimal change algorithm. b. Generate all subsets of {a. b, c, d} using the binary reflected Gray code. Use the Russian peasant algorithm to compute 95 times 12. List your steps to receive credit. What is the time efficiency of the algorithm applied to n times m. Can you think of a way to improve the computation in part a? 5. Use the Lomuto's Partitioning and the Quick Select algorithms to find the median of the list of numbers 4, 18,5, 17,23,2, 9. List the pivot and the partitions after each step.Explanation / Answer
5. Lomuto's Method
The index variable jj scans the whole array and whenever we find an element A[j]A[j] smaller than pivot xx, we do a swap. Among the elements 1,…,n1,…,n, exactly x1x1 ones are smaller than xx, so we get x1x1 swaps if the pivot is xx.
The overall expectation then results by averaging over all pivots. Each value in {1,…,n}{1,…,n} is equally likely to become pivot (namely with prob. 1n1n), so we have
1nx=1n(x1)=n212.1nx=1n(x1)=n212.
swaps on average to partition an array of length nn with Lomuto's method. The Lomuto partitioning algorithm can be explained as,
example: algorithm partition(A, lo, hi) is
However, instead of recursing into both sides, as in quicksort, quickselect only recurses into one side – the side with the element it is searching for. This reduces the average complexity fromO(n log n) to O(n).
To find the median of 4,18,5,17,23,2,9 we use the following c++ code
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.