# Sorting

### Applications of Sorting: Numbers

4.1. The Grinch is given the job of partitioning ${\displaystyle 2n}$ players into two teams of ${\displaystyle n}$ 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 ${\displaystyle O(nlogn)}$ 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, ${\displaystyle S={6,13,19,3,8}}$, 19 - 3 maximizes the difference, while 8 - 6 minimizes the difference.
(a) Let ${\displaystyle S}$ be an unsorted array of ${\displaystyle n}$ integers. Give an algorithm that finds the pair ${\displaystyle x,y\in S}$ that maximizes ${\displaystyle |x-y|}$. Your algorithm must run in ${\displaystyle O(n)}$ worst-case time.
(b) Let ${\displaystyle S}$ be a sorted array of ${\displaystyle n}$ integers. Give an algorithm that finds the pair ${\displaystyle x,y\in S}$ that maximizes ${\displaystyle |x-y|}$. Your algorithm must run in ${\displaystyle O(1)}$ worst-case time.
(c) Let ${\displaystyle S}$ be an unsorted array of ${\displaystyle n}$ integers. Give an algorithm that finds the pair ${\displaystyle x,y\in S}$ that minimizes ${\displaystyle |x-y|}$, for ${\displaystyle x\neq y}$. Your algorithm must run in ${\displaystyle O(nlogn)}$ worst-case time.
(d) Let ${\displaystyle S}$ be a sorted array of ${\displaystyle n}$ integers. Give an algorithm that finds the pair ${\displaystyle x,y\in S}$ that minimizes ${\displaystyle |x-y|}$, for ${\displaystyle x\neq y}$. Your algorithm must run in ${\displaystyle O(n)}$ worst-case time.

4.3. Take a list of ${\displaystyle 2n}$ real numbers as input. Design an ${\displaystyle O(nlogn)}$ algorithm that partitions the numbers into ${\displaystyle n}$ 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 ${\displaystyle n}$ 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 ${\displaystyle O(n)}$ 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 ${\displaystyle n}$ numbers.

4.6. Given two sets ${\displaystyle S_{1}}$ and ${\displaystyle S_{2}}$ (each of size ${\displaystyle n}$), and a number ${\displaystyle x}$, describe an ${\displaystyle O(nlogn)}$ algorithm for finding whether there exists a pair of elements, one from ${\displaystyle S_{1}}$ and one from ${\displaystyle S_{2}}$, that add up to ${\displaystyle x}$. (For partial credit, give a ${\displaystyle \Theta (n^{2})}$ 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 ${\displaystyle h}$-index. By definition, a scientist has index ${\displaystyle h}$ if ${\displaystyle h}$ of his or her ${\displaystyle n}$ papers have been cited at least ${\displaystyle h}$ times, while the other ${\displaystyle n-h}$ papers each have no more than ${\displaystyle h}$ 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 ${\displaystyle S}$ of ${\displaystyle n}$ integers and an integer ${\displaystyle T}$, give an ${\displaystyle O(n^{k-1}logn)}$ algorithm to test whether ${\displaystyle k}$ of the integers in ${\displaystyle S}$ add up to ${\displaystyle T}$.

4.10. We are given a set of ${\displaystyle S}$ containing ${\displaystyle n}$ real numbers and a real number ${\displaystyle x}$, and seek efficient algorithms to determine whether two elements of ${\displaystyle S}$ exist whose sum is exactly ${\displaystyle x}$.
(a) Assume that ${\displaystyle S}$ is unsorted. Give an ${\displaystyle O(nlogn)}$ algorithm for the problem.
(b) Assume that ${\displaystyle S}$ is sorted. Give an ${\displaystyle O(n)}$ algorithm for the problem.

4.11. Design an ${\displaystyle O(n)}$ algorithm that, given a list of ${\displaystyle n}$ elements, finds all the elements that appear more than ${\displaystyle n/2}$ times in the list. Then, design an ${\displaystyle O(n)}$ algorithm that, given a list of ${\displaystyle n}$ elements, finds all the elements that appear more than ${\displaystyle n/4}$ times.

### Applications of Sorting: Intervals and Sets

4.12. Give an efficient algorithm to compute the union of sets ${\displaystyle A}$ and ${\displaystyle B}$, where ${\displaystyle n=max(|A|,|B|)}$. The output should be an array of distinct elements that form the union of the sets.
(a) Assume that ${\displaystyle A}$ and ${\displaystyle B}$ are unsorted arrays. Give an ${\displaystyle O(nlogn)}$ algorithm for the problem.
(b) Assume that ${\displaystyle A}$ and ${\displaystyle B}$ are sorted arrays. Give an ${\displaystyle O(n)}$ algorithm for the problem.

4.13. A camera at the door tracks the entry time ${\displaystyle a_{i}}$ and exit time ${\displaystyle b_{i}}$ (assume ${\displaystyle b_{i}>a_{i}}$) for each of ${\displaystyle n}$ persons ${\displaystyle p_{i}}$ attending a party. Give an ${\displaystyle O(nlogn)}$ 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 ${\displaystyle I}$ of ${\displaystyle n}$ intervals, specified as ${\displaystyle (x_{i},y_{i})}$ pairs, return a list where the overlapping intervals are merged. For ${\displaystyle I={(1,3),(2,6),(8,10),(7,18)}}$ the output should be ${\displaystyle {(1,6),(7,18)}}$. Your algorithm should run in worst-case ${\displaystyle O(nlogn)}$ time complexity.

4.15. You are given a set ${\displaystyle S}$ of ${\displaystyle n}$ intervals on a line, with the ${\displaystyle i}$th interval described by its left and right endpoints ${\displaystyle (l_{i},r_{i})}$. Give an ${\displaystyle O(nlogn)}$ algorithm to identify a point ${\displaystyle p}$ on the line that is in the largest number of intervals.
As an example, for ${\displaystyle S={(10,40),(20,60),(50,90),(15,70)}}$ no point exists in all four intervals, but ${\displaystyle p=50}$ 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 ${\displaystyle S}$ of ${\displaystyle n}$ segments on the line, where segment ${\displaystyle S_{i}}$ ranges from ${\displaystyle l_{i}}$ to ${\displaystyle r_{i}}$. Give an efficient algorithm to select the fewest number of segments whose union completely covers the interval from 0 to ${\displaystyle m}$.

### Heaps

4.17. Devise an algorithm for finding the ${\displaystyle k}$ smallest elements of an unsorted set of ${\displaystyle n}$ integers in ${\displaystyle O(n+klogn)}$.

4.18. Give an ${\displaystyle O(nlogk)}$-time algorithm that merges ${\displaystyle k}$ sorted lists with a total of ${\displaystyle n}$ elements into one sorted list. (Hint: use a heap to speed up the obvious ${\displaystyle O(know)}$-time algorithm).

4.19. You wish to store a set of ${\displaystyle n}$ 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 ${\displaystyle n}$ keys. You can do better than ${\displaystyle 2n-3}$ comparisons.
(b) Then, give an efficient algorithm to find the third-largest key among ${\displaystyle n}$ 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?

4.21

4.22

4.23

4.24

4.25

4.26

4.27

4.28

4.29

4.30

4.31

4.32

4.33

4.34

4.35

4.36

4.37

4.38

4.39

4.40

4.41

4.42

4.43

4.44

4.45

4.46

### Interview Problems

4.47

4.48

4.49

4.50

4.51

4.52

4.53

Back to Chapter List