Problem 20
Sort a list of strings by increasing length.
Problem 21
Sort a list of strings by increasing length, and so that strings of the same length are sorted lexicographically.
Problem 21
A stable sort does not change the order of elements with the same valuc. This is a desirable feature in many applications. Consider a sequence of e-mail messages. If you sort by date and then by sender, you'd like the second sort to preserve the relative order of the first, so that you can see all messages from the same sender in date order. Is selection sort stable? Insertion sort? Why or why not?
Problem 22
Give an \(O(n)\) algorithm to sort a list of \(n\) bytes (numbers between \(-128\) and 127 ). Hint: Use a list of counters.
Problem 23
You are given a sequence of lists of words, representing the pages of a book. Your task is to build an index (a sorted list of words), each clement of which has a list of sorted numbers representing the pages on which the word appears. Describe an algorithm for building the index and give its big-Oh running time in terms of the total number of words.
Problem 24
Given two lists of \(n\) integers cach, describe an \(O(n \log (n))\) algorithm for determining whether they have an element in common.
Problem 25
Given a list of \(n\) integers and a value \(v\), describe an \(O(n \log (n))\) algorithm to find whether there are two values \(x\) and \(y\) in the list with sum \(v\).
Problem 26
Given two lists of \(n\) integers each, describe an \(O(n \log (n))\) algorithm for finding all elements that they have in common.
Problem 27
Suppose we modify the quicksort algorithm from Special Topic 12.3, selecting the middle element instead of the first one as pivot. What is the running time on a list that is already sorted?