
Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

How many comparisons are done in Quicksort to combine the results from solving the subproblems? None!
In what sense does Partition solve a subproblem of the larger problem of sorting the entire array? In that it places the pivot element in the right position. (Partition also of course makes it trivial to combine subproblem solutions…)
What is the practical benefit of the fact that Partition put all items less than the pivot below the pivot and all items greater than the pivot above the pivot? We can recursively sort the sub- arrays and not have to do anything afterwards to combine these subproblem solutions.
Show the array after Lomuto’s partition is called: E = {5, 3, 2, 6, 4, 1, 3, 7} {3, 3, 2, 4, 1, 5, 6, 7}
Show the array after Hoare’s partition is called: E = {5, 3, 2, 6, 4, 1, 3, 7} {1, 3, 2, 3, 4, 5, 6, 7}
Count how many comparisons were done and how many swaps were done for the previous two questions. Lomuto’s: always n-1 so 7; 6 swaps (one for each item <5 plus final swap) Hoare’s: 9 comparisons; 2 swaps
What determines whether or not quicksort has worst-case performance? Is it the choice of pivot element? The initial arrangement of data? Both of these? Both! REMEMBER: worst-case happens when most unequal partition occurs for every recursive call of partition.
What’s an easy way to minimize the likelihood of getting quicksort’s worst-case? Randomized- partition method or median-of-three
We often don’t worry about fine-tuning an algorithm for efficiency, but why is this worthwhile for quicksort? It’s one of the best choices for sorting algorithms, so we won’t find another one that’s clearly better in practice. Thus it’s used in many libraries.
We use a different form of a decision tree to do a lower-bounds proof for sorting than we did for sorting. What’s the difference in what a node represents in the two algorithms? For sorting, the node represents a comparison between array-node i and array-node j. For searching, the node represents a comparison between array-node and the search-target. What’s the difference in what a leaf represents in the two algorithms? For sorting, each leaf is a permutation of the array elements. For searching, a leaf is no different than an internal node.
Your friend Alex has a sorting algorithm that you see (after careful study) can be traced by a decision tree that has fewer than n! leaves for a fixed value of n, say, n=3. What can you conclude about Alex’s algorithm? The algorithm doesn’t always work because there is an ordering (a permutation) of the input elements that Alex’s algorithm can never produce!
What property of trees do we use in doing the lower-bound proof for sorting? That a binary tree with height h can have at most 2h^ leaves. In this case h is W(n) and there are n! leaves.
True or false? Quicksort has a W(n) that is very close to optimal. False! Remember W(n) is (n^2 )
True or false? Quicksort and mergesort are asymptotically optimal in the average-case? True
True or false? Because mergesort is close to optimal, it will run faster than quicksort in practice. (Explain why or why not.) False. The W(n) for MS is better than that of QS, but remember within order-classes you can’t just compare measures of basic operations. QS has less “overhead” per comparison when you do an empirical comparison.