# Difference between revisions of "Chapter 4"

Jump to navigation
Jump to search

m (Protected "Chapter 4" ([Edit=Allow only administrators] (indefinite) [Move=Allow only administrators] (indefinite))) |
|||

(18 intermediate revisions by the same user not shown) | |||

Line 3: | Line 3: | ||

===Applications of Sorting: Numbers=== | ===Applications of Sorting: Numbers=== | ||

− | :[[4.1]] | + | :[[4.1]]. The Grinch is given the job of partitioning <math>2n</math> players into two teams of <math>n</math> players each. Each player has a numerical rating that measures how good he or she is at the game. The Grinch seeks to divide the players as ''unfairly'' as possible, so as to create the biggest possible talent imbalance between the teams. Show how the Grinch can do the job in <math>O(n log n)</math> time. |

+ | [[4.1|Solution]] | ||

− | :4.2 | + | :4.2. For each of the following problems, give an algorithm that finds the desired numbers within the given amount of time. To keep your answers brief, feel free to use algorithms from the book as subroutines. For the example, <math>S = {6, 13, 19, 3, 8}</math>, 19 - 3 maximizes the difference, while 8 - 6 minimizes the difference. |

+ | :(a) Let <math>S</math> be an ''unsorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''maximizes'' <math>|x-y|</math>. Your algorithm must run in <math>O(n)</math> worst-case time. | ||

+ | :(b) Let <math>S</math> be a ''sorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''maximizes'' <math>|x - y|</math>. Your algorithm must run in <math>O(1)</math> worst-case time. | ||

+ | :(c) Let <math>S</math> be an ''unsorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''minimizes'' <math>|x - y|</math>, for <math>x \neq y</math>. Your algorithm must run in <math>O(n log n)</math> worst-case time. | ||

+ | :(d) Let <math>S</math> be a ''sorted'' array of <math>n</math> integers. Give an algorithm that finds the pair <math>x, y \in S</math> that ''minimizes'' <math>|x - y|</math>, for <math>x \neq y</math>. Your algorithm must run in <math>O(n)</math> worst-case time. | ||

− | :[[4.3]] | + | :[[4.3]]. Take a list of <math>2n</math> real numbers as input. Design an <math>O(n log n)</math> algorithm that partitions the numbers into <math>n</math> pairs, with the property that the partition minimizes the maximum sum of a pair. For example, say we are given the numbers (1,3,5,9). The possible partitions are ((1,3),(5,9)), ((1,5),(3,9)), and ((1,9),(3,5)). The pair sums for these partitions are (4,14), (6,12), and (10,8). Thus, the third partition has 10 as its maximum sum, which is the minimum over the three partitions. |

+ | [[4.3|Solution]] | ||

− | :4.4 | + | :4.4. Assume that we are given <math>n</math> pairs of items as input, where the first item is a number and the second item is one of three colors (red, blue, or yellow). Further assume that the items are sorted by number. Give an <math>O(n)</math> algorithm to sort the items by color (all reds before all blues before all yellows) such that the numbers for identical colors stay sorted. |

+ | :For example: (1,blue), (3,red), (4,blue), (6,yellow), (9,red) should become (3,red), (9,red), (1,blue), (4,blue), (6,yellow). | ||

− | :[[[4.5]] | + | :[[4.5]]. The ''mode'' of a bag of numbers is the number that occurs most frequently in the set. The set {4, 6, 2, 4, 3, 1} has a mode of 4. Give an efficient and correct algorithm to compute the mode of a bag of <math>n</math> numbers. |

+ | [[4.5|Solution]] | ||

− | :4.6 | + | :4.6. Given two sets <math>S_1</math> and <math>S_2</math> (each of size <math>n</math>), and a number <math>x</math>, describe an <math>O(n log n)</math> algorithm for finding whether there exists a pair of elements, one from <math>S_1</math> and one from <math>S_2</math>, that add up to <math>x</math>. (For partial credit, give a <math>\Theta(n^2)</math> algorithm for this problem.) |

− | :[[[4.7]] | + | :[[4.7]]. Give an efficient algorithm to take the array of citation counts (each count is a non-negative integer) of a researcher’s papers, and compute the researcher’s <math>h</math>-index. By definition, a scientist has index <math>h</math> if <math>h</math> of his or her <math>n</math> papers have been cited at least <math>h</math> times, while the other <math>n-h</math> papers each have no more than <math>h</math> citations. |

+ | [[4.7|Solution]] | ||

− | 4.8 | + | :4.8. Outline a reasonable method of solving each of the following problems. Give the order of the worst-case complexity of your methods. |

+ | :(a) You are given a pile of thousands of telephone bills and thousands of checks sent in to pay the bills. Find out who did not pay. | ||

+ | :(b) You are given a printed list containing the title, author, call number, and publisher of all the books in a school library and another list of thirty publishers. Find out how many of the books in the library were published by each company. | ||

+ | :(c) You are given all the book checkout cards used in the campus library during the past year, each of which contains the name of the person who took out the book. Determine how many distinct people checked out at least one book. | ||

− | [[4.9]] | + | :[[4.9]]. Given a set <math>S</math> of <math>n</math> integers and an integer <math>T</math>, give an <math>O(n^{k-1}log n)</math> algorithm to test whether <math>k</math> of the integers in <math>S</math> add up to <math>T</math>. |

+ | [[4.9|Solution]] | ||

− | :4.10 | + | :4.10. We are given a set of <math>S</math> containing <math>n</math> real numbers and a real number <math>x</math>, and seek efficient algorithms to determine whether two elements of <math>S</math> exist whose sum is exactly <math>x</math>. |

+ | :(a) Assume that <math>S</math> is unsorted. Give an <math>O(n log n)</math> algorithm for the problem. | ||

+ | :(b) Assume that <math>S</math> is sorted. Give an <math>O(n)</math> algorithm for the problem. | ||

− | :[[4.11]] | + | :[[4.11]]. Design an <math>O(n)</math> algorithm that, given a list of <math>n</math> elements, finds all the elements that appear more than <math>n/2</math> times in the list. Then, design an <math>O(n)</math> algorithm that, given a list of <math>n</math> elements, finds all the elements that appear more than <math>n/4</math> times. |

+ | [[4.11|Solution]] | ||

+ | ===Applications of Sorting: Intervals and Sets=== | ||

− | = | + | :4.12. Give an efficient algorithm to compute the union of sets <math>A</math> and <math>B</math>, where <math>n = max(|A|, |B|)</math>. The output should be an array of distinct elements that form the union of the sets. |

+ | :(a) Assume that <math>A</math> and <math>B</math> are unsorted arrays. Give an <math>O(n log n)</math> algorithm for the problem. | ||

+ | :(b) Assume that <math>A</math> and <math>B</math> are sorted arrays. Give an <math>O(n)</math> algorithm for the problem. | ||

− | |||

+ | :[[4.13]]. A camera at the door tracks the entry time <math>a_i</math> and exit time <math>b_i</math> (assume <math>b_i > a_i</math>) for each of <math>n</math> persons <math>p_i</math> attending a party. Give an <math>O(n log n)</math> algorithm that analyzes this data to determine the time when the most people were simultaneously present at the party. You may assume that all entry and exit times are distinct (no ties). | ||

+ | [[4.13|Solution]] | ||

− | |||

+ | :4.14. Given a list <math>I</math> of <math>n</math> intervals, specified as <math>(x_i, y_i)</math> pairs, return a list where the overlapping intervals are merged. For <math>I = {(1, 3),(2, 6),(8, 10),(7, 18)}</math> the output should be <math>{(1, 6),(7, 18)}</math>. Your algorithm should run in worst-case <math>O(n log n)</math> time complexity. | ||

− | |||

+ | :[[4.15]]. You are given a set <math>S</math> of <math>n</math> intervals on a line, with the <math>i</math>th interval described by its left and right endpoints <math>(l_i, r_i)</math>. Give an <math>O(n log n)</math> algorithm to identify a point <math>p</math> on the line that is in the largest number of intervals. | ||

+ | :As an example, for <math>S = {(10, 40),(20, 60),(50, 90),(15, 70)}</math> no point exists in all four intervals, but <math>p = 50</math> is an example of a point in three intervals. You can assume an endpoint counts as being in its interval. | ||

+ | [[4.15|Solution]] | ||

− | |||

− | |||

− | |||

− | |||

+ | :4.16. You are given a set <math>S</math> of <math>n</math> segments on the line, where segment <math>S_i</math> ranges from <math>l_i</math> to <math>r_i</math>. Give an efficient algorithm to select the fewest number of segments whose union completely covers the interval from 0 to <math>m</math>. | ||

===Heaps=== | ===Heaps=== | ||

− | :[[4.17]] | + | :[[4.17]]. Devise an algorithm for finding the <math>k</math> smallest elements of an unsorted set of <math>n</math> integers in <math>O(n + k log n)</math>. |

+ | [[4.17|Solution]] | ||

− | :4.18 | + | :4.18. Give an <math>O(n log k)</math>-time algorithm that merges <math>k</math> sorted lists with a total of <math>n</math> elements into one sorted list. (Hint: use a heap to speed up the obvious <math>O(know)</math>-time algorithm). |

− | :[[4.19]] | + | :[[4.19]]. You wish to store a set of <math>n</math> numbers in either a max-heap or a sorted array. For each application below, state which data structure is better, or if it does not matter. Explain your answers. |

+ | :(a) Find the maximum element quickly. | ||

+ | :(b) Delete an element quickly. | ||

+ | :(c) Form the structure quickly. | ||

+ | :(d) Find the minimum element quickly. | ||

+ | [[4.19|Solution]] | ||

− | :4.20 | + | :4.20. (a) Give an efficient algorithm to find the second-largest key among <math>n</math> keys. You can do better than <math>2n - 3</math> comparisons. |

− | + | :(b) Then, give an efficient algorithm to find the third-largest key among <math>n</math> keys. How many key comparisons does your algorithm do in the worst case? Must your algorithm determine which key is largest and second-largest in the process? | |

===Quicksort=== | ===Quicksort=== | ||

− | :[[4.21]] | + | :[[4.21]]. Use the partitioning idea of quicksort to give an algorithm that finds the median element of an array of <math>n</math> integers in expected <math>O(n)</math> time. (Hint: must you look at both sides of the partition?) |

+ | [[4.21|Solution]] | ||

− | :4.22 | + | :4.22. The ''median'' of a set of <math>n</math> values is the <math>[n/2]</math>th smallest value. |

+ | :(a) Suppose quicksort always pivoted on the median of the current sub-array. How many comparisons would quicksort make then in the worst case? | ||

+ | :(b) Suppose quicksort always pivoted on the <math>[n/3]</math>th smallest value of the current sub-array. How many comparisons would be made then in the worst case? | ||

− | :[[4.23]] | + | :[[4.23]]. Suppose an array <math>A</math> consists of <math>n</math> elements, each of which is ''red'', ''white'', or ''blue'. We seek to sort the elements so that all the ''reds'' come before all the ''whites'', which come before all the ''blues''. The only operations permitted on the keys are: |

+ | ::• ''Examine''(<math>A,i</math>) – report the color of the <math>i</math>th element of <math>A</math>. | ||

+ | ::• ''Swap''(<math>A,i,j</math>) – swap the <math>i</math>th element of <math>A</math> with the <math>j</math>th element. | ||

+ | :Find a correct and efficient algorithm for red–white–blue sorting. There is a linear-time solution. | ||

+ | [[4.23|Solution]] | ||

− | :4.24 | + | :4.24. Give an efficient algorithm to rearrange an array of <math>n</math> keys so that all the negative keys precede all the non-negative keys. Your algorithm must be in-place, meaning you cannot allocate another array to temporarily hold the items. How fast is your algorithm? |

− | :[[4.25]] | + | :[[4.25]]. Consider a given pair of different elements in an input array to be sorted, say <math>z_i</math> and <math>z_j</math> . What is the most number of times <math>z_i</math> and <math>z_j</math> might be compared with each other during an execution of quicksort? |

+ | [[4.25|Solution]] | ||

− | :4.26 | + | :4.26. Define the recursion depth of quicksort as the maximum number of successive recursive calls it makes before hitting the base case. What are the minimum and maximum possible recursion depths for randomized quicksort? |

− | :[[4.27]] | + | :[[4.27]]. Suppose you are given a permutation <math>p</math> of the integers 1 to <math>n</math>, and seek to sort them to be in increasing order <math>[1, . . . , n]</math>. The only operation at your disposal is ''reverse''<math>(p,i,j)</math>, which reverses the elements of a subsequence <math>p_i, . . . , p_j</math> in the permutation. For the permutation [1, 4, 3, 2, 5] one reversal (of the second through fourth elements) suffices to sort. |

+ | :• Show that it is possible to sort any permutation using <math>O(n)</math> reversals. | ||

+ | :• Now suppose that the cost of ''reverse''<math>(p,i,j)</math> is equal to its length, the number of elements in the range, <math>|j - i| + 1</math>. Design an algorithm that sorts <math>p</math> in <math>O(n log^2 n)</math> cost. Analyze the running time and cost of your algorithm and prove correctness. | ||

+ | [[4.27|Solution]] | ||

+ | ===Mergesort=== | ||

− | + | :4.28. Consider the following modification to merge sort: divide the input array into thirds (rather than halves), recursively sort each third, and finally combine the results using a three-way merge subroutine. What is the worst-case running time of this modified merge sort? | |

− | |||

+ | :[[4.29]]. Suppose you are given <math>k</math> sorted arrays, each with <math>n</math> elements, and you want to combine them into a single sorted array of <math>kn</math> elements. One approach is to use the merge subroutine repeatedly, merging the first two arrays, then merging the result with the third array, then with the fourth array, and so on until you merge in the <math>k</math>th and final input array. What is the running time? | ||

+ | [[4.29|Solution]] | ||

− | |||

− | |||

− | |||

− | |||

+ | :4.30. Consider again the problem of merging <math>k</math> sorted length-<math>n</math> arrays into a single sorted length-<math>kn</math> array. Consider the algorithm that first divides the <math>k</math> arrays into <math>k/2</math> pairs of arrays, and uses the merge subroutine to combine each pair, resulting in <math>k/2</math> sorted length-<math>2n</math> arrays. The algorithm repeats this step until there is only one length-<math>know</math> sorted array. What is the running time as a function of <math>n</math> and <math>k</math>? | ||

===Other Sorting Alogrithims=== | ===Other Sorting Alogrithims=== | ||

− | :[[4.31]] | + | :[[4.31]]. Stable sorting algorithms leave equal-key items in the same relative order as in the original permutation. Explain what must be done to ensure that mergesort is a stable sorting algorithm. |

+ | [[4.31|Solution]] | ||

− | :4.32 | + | :4.32. Wiggle sort: Given an unsorted array <math>A</math>, reorder it such that <math>A[0] < A[1] > A[2] < A[3] . . . .</math> For example, one possible answer for input [3, 1, 4, 2, 6, 5] is [1, 3, 2, 5, 4, 6]. Can you do it in <math>O(n)</math> time using only <math>O(1)</math> space? |

− | :[[4.33]] | + | :[[4.33]]. Show that <math>n</math> positive integers in the range 1 to <math>k</math> can be sorted in <math>O(n log k)</math> time. The interesting case is when <math>k \ll n</math>. |

+ | [[4.33|Solution]] | ||

− | :4.34 | + | :4.34. Consider a sequence <math>S</math> of <math>n</math> integers with many duplications, such that the number of distinct integers in <math>S</math> is <math>O(log n)</math>. Give an <math>O(n log log n)</math> worst-case time algorithm to sort such sequences. |

− | :[[4.35]] | + | :[[4.35]]. Let <math>A[1..n]</math> be an array such that the first <math>n - |

+ | \sqrt n</math> elements are already sorted (though we know nothing about the remaining elements). Give an algorithm that sorts <math>A</math> in substantially better than <math>n log n</math> steps. | ||

+ | [[4.35|Solution]] | ||

− | :4.36 | + | :4.36. Assume that the array <math>A[1..n]</math> only has numbers from <math>{1, . . . , n^2}</math> but that at most <math>log log n</math> of these numbers ever appear. Devise an algorithm that sorts <math>A</math> in substantially less than <math>O(n log n)</math>. |

− | :[[4.37]] | + | :[[4.37]]. Consider the problem of sorting a sequence of <math>n</math> 0’s and 1’s using comparisons. For each comparison of two values <math>x</math> and <math>y</math>, the algorithm learns which of <math>x < y, x = y</math>, or <math>x > y</math> holds. |

+ | :(a) Give an algorithm to sort in <math>n - 1</math> comparisons in the worst case. Show that your algorithm is optimal. | ||

+ | :(b) Give an algorithm to sort in <math>2n/3</math> comparisons in the average case (assuming each of the <math>n</math> inputs is 0 or 1 with equal probability). Show that your algorithm is optimal. | ||

+ | [[4.37|Solution]] | ||

− | :4.38 | + | :4.38. Let <math>P</math> be a simple, but not necessarily convex, <math>n</math>-sided polygon and <math>q</math> an arbitrary point not necessarily in <math>P</math>. Design an efficient algorithm to find a line segment originating from <math>q</math> that intersects the maximum number of edges of <math>P</math>. |

− | + | :In other words, if standing at point <math>q</math>, in what direction should you aim a gun so the bullet will go through the largest number of walls. A bullet through a vertex of <math>P</math> gets credit for only one wall. An <math>O(n log n)</math> algorithm is possible. | |

===Lower Bounds=== | ===Lower Bounds=== | ||

− | :[[4.39]] | + | :[[4.39]]. In one of my research papers [Ski88], I discovered a comparison-based sorting algorithm that runs in <math>O(n log(\sqrt n))</math>. Given the existence of an <math>\Omega(n log n)</math> lower bound for sorting, how can this be possible? |

− | + | [[4.39|Solution]] | |

− | |||

+ | :4.40. Mr. B. C. Dull claims to have developed a new data structure for priority queues that supports the operations ''Insert'', ''Maximum'', and ''Extract-Max''—all in <math>O(1)</math> worst-case time. Prove that he is mistaken. (Hint: the argument does not involve a lot of gory details—just think about what this would imply about the <math>\Omega (n log n)</math> lower bound for sorting.) | ||

===Searching=== | ===Searching=== | ||

− | :[[4.41]] | + | :[[4.41]]. A company database consists of 10,000 sorted names, 40% of whom are known as good customers and who together account for 60% of the accesses to the database. There are two data structure options to consider for representing the database: |

− | + | :• Put all the names in a single array and use binary search. | |

− | + | :• Put the good customers in one array and the rest of them in a second array. Only if we do not find the query name on a binary search of the first array do we do a binary search of the second array. | |

− | :4. | + | :Demonstrate which option gives better expected performance. Does this change if linear search on an unsorted array is used instead of binary search for both options? |

+ | [[4.41|Solution]] | ||

− | === | + | :4.42. A ''Ramanujan number'' can be written two different ways as the sum of two cubes—meaning there exist distinct positive integers <math>a, b, c</math>, and <math>d</math> such that <math>a^3 + b^3 = c^3 + d^3</math>. For example, 1729 is a Ramanujan number because <math>1729 = 1^3 + 12^3 = 9^3 + 10^3</math>. |

+ | :(a) Give an efficient algorithm to test whether a given single integer <math>n</math> is a Ramanujan number, with an analysis of the algorithm’s complexity. | ||

+ | :(b) Now give an efficient algorithm to generate all the Ramanujan numbers between 1 and <math>n</math>, with an analysis of its complexity. | ||

− | + | ===Implementaion Challenges=== | |

+ | :[[4.43]]. Consider an n×n array <math>A</math> containing integer elements (positive, negative, and zero). Assume that the elements in each row of <math>A</math> are in strictly increasing order, and the elements of each column of <math>A</math> are in strictly decreasing order. (Hence there cannot be two zeros in the same row or the same column.) Describe an efficient algorithm that counts the number of occurrences of the element 0 in <math>A</math>. Analyze its running time. | ||

+ | [[4.43|Solution]] | ||

− | |||

+ | :4.44. Implement versions of several different sorting algorithms, such as selection sort, insertion sort, heapsort, mergesort, and quicksort. Conduct experiments to assess the relative performance of these algorithms in a simple application that reads a large text file and reports exactly one instance of each word that appears within it. This application can be efficiently implemented by sorting all the words that occur in the text and then passing through the sorted sequence to identify one instance of each distinct word. Write a brief report with your conclusions. | ||

− | |||

+ | :[[4.45]]. Implement an external sort, which uses intermediate files to sort files bigger than main memory. Mergesort is a good algorithm to base such an implementation on. Test your program both on files with small records and on files with large records. | ||

+ | [[4.45|Solution]] | ||

− | |||

+ | :4.46. Design and implement a parallel sorting algorithm that distributes data across several processors. An appropriate variation of mergesort is a likely candidate. Measure the speedup of this algorithm as the number of processors increases. Then compare the execution time to that of a purely sequential mergesort implementation. What are your experiences? | ||

===Interview Problems=== | ===Interview Problems=== | ||

− | :[[4.47]] | + | :[[4.47]]. If you are given a million integers to sort, what algorithm would you use to sort them? How much time and memory would that consume? |

+ | [[4.47|Solution]] | ||

− | :4.48 | + | :4.48. Describe advantages and disadvantages of the most popular sorting algorithms. |

− | :[[4.49]] | + | :[[4.49]]. Implement an algorithm that takes an input array and returns only the unique elements in it. |

+ | [[4.49|Solution]] | ||

− | :4.50 | + | :4.50. You have a computer with only 4 GB of main memory. How do you use it to sort a large file of 500 GB that is on disk? |

− | :[[4.51]] | + | :[[4.51]]. Design a stack that supports push, pop, and retrieving the minimum element in constant time. |

+ | [[4.51|Solution]] | ||

− | :4.52 | + | :4.52. Given a search string of three words, find the smallest snippet of the document that contains all three of the search words—that is, the snippet with the smallest number of words in it. You are given the index positions where these words occur in the document, such as ''word1'': <math>(1, 4, 5)</math>, ''word2'': <math>(3, 9, 10)</math>, and ''word3'': <math>(2, 6, 15)</math>. Each of the lists are in sorted order, as above. |

− | :[[4.53]] | + | :[[4.53]]. You are given twelve coins. One of them is heavier or lighter than the rest. Identify this coin in just three weighings with a balance scale. |

+ | [[4.53|Solution]] | ||

Back to [[Chapter List]] | Back to [[Chapter List]] |

## Latest revision as of 18:06, 1 October 2020

## Contents

# Sorting

### Applications of Sorting: Numbers

- 4.1. The Grinch is given the job of partitioning players into two teams of players each. Each player has a numerical rating that measures how good he or she is at the game. The Grinch seeks to divide the players as
*unfairly*as possible, so as to create the biggest possible talent imbalance between the teams. Show how the Grinch can do the job in time.

- 4.2. For each of the following problems, give an algorithm that finds the desired numbers within the given amount of time. To keep your answers brief, feel free to use algorithms from the book as subroutines. For the example, , 19 - 3 maximizes the difference, while 8 - 6 minimizes the difference.
- (a) Let be an
*unsorted*array of integers. Give an algorithm that finds the pair that*maximizes*. Your algorithm must run in worst-case time. - (b) Let be a
*sorted*array of integers. Give an algorithm that finds the pair that*maximizes*. Your algorithm must run in worst-case time. - (c) Let be an
*unsorted*array of integers. Give an algorithm that finds the pair that*minimizes*, for . Your algorithm must run in worst-case time. - (d) Let be a
*sorted*array of integers. Give an algorithm that finds the pair that*minimizes*, for . Your algorithm must run in worst-case time.

- 4.3. Take a list of real numbers as input. Design an algorithm that partitions the numbers into pairs, with the property that the partition minimizes the maximum sum of a pair. For example, say we are given the numbers (1,3,5,9). The possible partitions are ((1,3),(5,9)), ((1,5),(3,9)), and ((1,9),(3,5)). The pair sums for these partitions are (4,14), (6,12), and (10,8). Thus, the third partition has 10 as its maximum sum, which is the minimum over the three partitions.

- 4.4. Assume that we are given pairs of items as input, where the first item is a number and the second item is one of three colors (red, blue, or yellow). Further assume that the items are sorted by number. Give an algorithm to sort the items by color (all reds before all blues before all yellows) such that the numbers for identical colors stay sorted.
- For example: (1,blue), (3,red), (4,blue), (6,yellow), (9,red) should become (3,red), (9,red), (1,blue), (4,blue), (6,yellow).

- 4.5. The
*mode*of a bag of numbers is the number that occurs most frequently in the set. The set {4, 6, 2, 4, 3, 1} has a mode of 4. Give an efficient and correct algorithm to compute the mode of a bag of numbers.

- 4.6. Given two sets and (each of size ), and a number , describe an algorithm for finding whether there exists a pair of elements, one from and one from , that add up to . (For partial credit, give a algorithm for this problem.)

- 4.7. Give an efficient algorithm to take the array of citation counts (each count is a non-negative integer) of a researcher’s papers, and compute the researcher’s -index. By definition, a scientist has index if of his or her papers have been cited at least times, while the other papers each have no more than citations.

- 4.8. Outline a reasonable method of solving each of the following problems. Give the order of the worst-case complexity of your methods.
- (a) You are given a pile of thousands of telephone bills and thousands of checks sent in to pay the bills. Find out who did not pay.
- (b) You are given a printed list containing the title, author, call number, and publisher of all the books in a school library and another list of thirty publishers. Find out how many of the books in the library were published by each company.
- (c) You are given all the book checkout cards used in the campus library during the past year, each of which contains the name of the person who took out the book. Determine how many distinct people checked out at least one book.

- 4.9. Given a set of integers and an integer , give an algorithm to test whether of the integers in add up to .

- 4.10. We are given a set of containing real numbers and a real number , and seek efficient algorithms to determine whether two elements of exist whose sum is exactly .
- (a) Assume that is unsorted. Give an algorithm for the problem.
- (b) Assume that is sorted. Give an algorithm for the problem.

- 4.11. Design an algorithm that, given a list of elements, finds all the elements that appear more than times in the list. Then, design an algorithm that, given a list of elements, finds all the elements that appear more than times.

### Applications of Sorting: Intervals and Sets

- 4.12. Give an efficient algorithm to compute the union of sets and , where . The output should be an array of distinct elements that form the union of the sets.
- (a) Assume that and are unsorted arrays. Give an algorithm for the problem.
- (b) Assume that and are sorted arrays. Give an algorithm for the problem.

- 4.13. A camera at the door tracks the entry time and exit time (assume ) for each of persons attending a party. Give an algorithm that analyzes this data to determine the time when the most people were simultaneously present at the party. You may assume that all entry and exit times are distinct (no ties).

- 4.14. Given a list of intervals, specified as pairs, return a list where the overlapping intervals are merged. For the output should be . Your algorithm should run in worst-case time complexity.

- 4.15. You are given a set of intervals on a line, with the th interval described by its left and right endpoints . Give an algorithm to identify a point on the line that is in the largest number of intervals.
- As an example, for no point exists in all four intervals, but is an example of a point in three intervals. You can assume an endpoint counts as being in its interval.

- 4.16. You are given a set of segments on the line, where segment ranges from to . Give an efficient algorithm to select the fewest number of segments whose union completely covers the interval from 0 to .

### Heaps

- 4.17. Devise an algorithm for finding the smallest elements of an unsorted set of integers in .

- 4.18. Give an -time algorithm that merges sorted lists with a total of elements into one sorted list. (Hint: use a heap to speed up the obvious -time algorithm).

- 4.19. You wish to store a set of numbers in either a max-heap or a sorted array. For each application below, state which data structure is better, or if it does not matter. Explain your answers.
- (a) Find the maximum element quickly.
- (b) Delete an element quickly.
- (c) Form the structure quickly.
- (d) Find the minimum element quickly.

- 4.20. (a) Give an efficient algorithm to find the second-largest key among keys. You can do better than comparisons.
- (b) Then, give an efficient algorithm to find the third-largest key among keys. How many key comparisons does your algorithm do in the worst case? Must your algorithm determine which key is largest and second-largest in the process?

### Quicksort

- 4.21. Use the partitioning idea of quicksort to give an algorithm that finds the median element of an array of integers in expected time. (Hint: must you look at both sides of the partition?)

- 4.22. The
*median*of a set of values is the th smallest value. - (a) Suppose quicksort always pivoted on the median of the current sub-array. How many comparisons would quicksort make then in the worst case?
- (b) Suppose quicksort always pivoted on the th smallest value of the current sub-array. How many comparisons would be made then in the worst case?

- 4.23. Suppose an array consists of elements, each of which is
*red*,*white*, or*blue'. We seek to sort the elements so that all the*reds*come before all the*whites*, which come before all the*blues*. The only operations permitted on the keys are:*- •
*Examine*() – report the color of the th element of . - •
*Swap*() – swap the th element of with the th element.

- •
- Find a correct and efficient algorithm for red–white–blue sorting. There is a linear-time solution.

- 4.24. Give an efficient algorithm to rearrange an array of keys so that all the negative keys precede all the non-negative keys. Your algorithm must be in-place, meaning you cannot allocate another array to temporarily hold the items. How fast is your algorithm?

- 4.25. Consider a given pair of different elements in an input array to be sorted, say and . What is the most number of times and might be compared with each other during an execution of quicksort?

- 4.26. Define the recursion depth of quicksort as the maximum number of successive recursive calls it makes before hitting the base case. What are the minimum and maximum possible recursion depths for randomized quicksort?

- 4.27. Suppose you are given a permutation of the integers 1 to , and seek to sort them to be in increasing order . The only operation at your disposal is
*reverse*, which reverses the elements of a subsequence in the permutation. For the permutation [1, 4, 3, 2, 5] one reversal (of the second through fourth elements) suffices to sort. - • Show that it is possible to sort any permutation using reversals.
- • Now suppose that the cost of
*reverse*is equal to its length, the number of elements in the range, . Design an algorithm that sorts in cost. Analyze the running time and cost of your algorithm and prove correctness.

### Mergesort

- 4.28. Consider the following modification to merge sort: divide the input array into thirds (rather than halves), recursively sort each third, and finally combine the results using a three-way merge subroutine. What is the worst-case running time of this modified merge sort?

- 4.29. Suppose you are given sorted arrays, each with elements, and you want to combine them into a single sorted array of elements. One approach is to use the merge subroutine repeatedly, merging the first two arrays, then merging the result with the third array, then with the fourth array, and so on until you merge in the th and final input array. What is the running time?

- 4.30. Consider again the problem of merging sorted length- arrays into a single sorted length- array. Consider the algorithm that first divides the arrays into pairs of arrays, and uses the merge subroutine to combine each pair, resulting in sorted length- arrays. The algorithm repeats this step until there is only one length- sorted array. What is the running time as a function of and ?

### Other Sorting Alogrithims

- 4.31. Stable sorting algorithms leave equal-key items in the same relative order as in the original permutation. Explain what must be done to ensure that mergesort is a stable sorting algorithm.

- 4.32. Wiggle sort: Given an unsorted array , reorder it such that For example, one possible answer for input [3, 1, 4, 2, 6, 5] is [1, 3, 2, 5, 4, 6]. Can you do it in time using only space?

- 4.33. Show that positive integers in the range 1 to can be sorted in time. The interesting case is when .

- 4.34. Consider a sequence of integers with many duplications, such that the number of distinct integers in is . Give an worst-case time algorithm to sort such sequences.

- 4.35. Let be an array such that the first elements are already sorted (though we know nothing about the remaining elements). Give an algorithm that sorts in substantially better than steps.

- 4.36. Assume that the array only has numbers from but that at most of these numbers ever appear. Devise an algorithm that sorts in substantially less than .

- 4.37. Consider the problem of sorting a sequence of 0’s and 1’s using comparisons. For each comparison of two values and , the algorithm learns which of , or holds.
- (a) Give an algorithm to sort in comparisons in the worst case. Show that your algorithm is optimal.
- (b) Give an algorithm to sort in comparisons in the average case (assuming each of the inputs is 0 or 1 with equal probability). Show that your algorithm is optimal.

- 4.38. Let be a simple, but not necessarily convex, -sided polygon and an arbitrary point not necessarily in . Design an efficient algorithm to find a line segment originating from that intersects the maximum number of edges of .
- In other words, if standing at point , in what direction should you aim a gun so the bullet will go through the largest number of walls. A bullet through a vertex of gets credit for only one wall. An algorithm is possible.

### Lower Bounds

- 4.39. In one of my research papers [Ski88], I discovered a comparison-based sorting algorithm that runs in . Given the existence of an lower bound for sorting, how can this be possible?

- 4.40. Mr. B. C. Dull claims to have developed a new data structure for priority queues that supports the operations
*Insert*,*Maximum*, and*Extract-Max*—all in worst-case time. Prove that he is mistaken. (Hint: the argument does not involve a lot of gory details—just think about what this would imply about the lower bound for sorting.)

### Searching

- 4.41. A company database consists of 10,000 sorted names, 40% of whom are known as good customers and who together account for 60% of the accesses to the database. There are two data structure options to consider for representing the database:
- • Put all the names in a single array and use binary search.
- • Put the good customers in one array and the rest of them in a second array. Only if we do not find the query name on a binary search of the first array do we do a binary search of the second array.
- Demonstrate which option gives better expected performance. Does this change if linear search on an unsorted array is used instead of binary search for both options?

- 4.42. A
*Ramanujan number*can be written two different ways as the sum of two cubes—meaning there exist distinct positive integers , and such that . For example, 1729 is a Ramanujan number because . - (a) Give an efficient algorithm to test whether a given single integer is a Ramanujan number, with an analysis of the algorithm’s complexity.
- (b) Now give an efficient algorithm to generate all the Ramanujan numbers between 1 and , with an analysis of its complexity.

### Implementaion Challenges

- 4.43. Consider an n×n array containing integer elements (positive, negative, and zero). Assume that the elements in each row of are in strictly increasing order, and the elements of each column of are in strictly decreasing order. (Hence there cannot be two zeros in the same row or the same column.) Describe an efficient algorithm that counts the number of occurrences of the element 0 in . Analyze its running time.

- 4.44. Implement versions of several different sorting algorithms, such as selection sort, insertion sort, heapsort, mergesort, and quicksort. Conduct experiments to assess the relative performance of these algorithms in a simple application that reads a large text file and reports exactly one instance of each word that appears within it. This application can be efficiently implemented by sorting all the words that occur in the text and then passing through the sorted sequence to identify one instance of each distinct word. Write a brief report with your conclusions.

- 4.45. Implement an external sort, which uses intermediate files to sort files bigger than main memory. Mergesort is a good algorithm to base such an implementation on. Test your program both on files with small records and on files with large records.

- 4.46. Design and implement a parallel sorting algorithm that distributes data across several processors. An appropriate variation of mergesort is a likely candidate. Measure the speedup of this algorithm as the number of processors increases. Then compare the execution time to that of a purely sequential mergesort implementation. What are your experiences?

### Interview Problems

- 4.47. If you are given a million integers to sort, what algorithm would you use to sort them? How much time and memory would that consume?

- 4.48. Describe advantages and disadvantages of the most popular sorting algorithms.

- 4.49. Implement an algorithm that takes an input array and returns only the unique elements in it.

- 4.50. You have a computer with only 4 GB of main memory. How do you use it to sort a large file of 500 GB that is on disk?

- 4.51. Design a stack that supports push, pop, and retrieving the minimum element in constant time.

- 4.52. Given a search string of three words, find the smallest snippet of the document that contains all three of the search words—that is, the snippet with the smallest number of words in it. You are given the index positions where these words occur in the document, such as
*word1*: ,*word2*: , and*word3*: . Each of the lists are in sorted order, as above.

- 4.53. You are given twelve coins. One of them is heavier or lighter than the rest. Identify this coin in just three weighings with a balance scale.

Back to Chapter List