Difference between revisions of "User:Sorting-searching-TADM2O"

From Algorithm Wiki
Jump to: navigation, search
(Adding link on task solution)
(Replaced content with "TaTaatatatattataa")
Line 1: Line 1:
= Sorting and Searching =
+
TaTaatatatattataa
 
 
 
 
 
 
'''Applications of Sorting'''
 
 
 
<br>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/she is
 
at the game.
 
He seeks to divide the players as ''unfairly''
 
as possible, so as to create the biggest possible talent imbalance between team
 
<math>A</math> and team <math>B</math>.
 
Show how the Grinch can do the job in <math>O(n \log n)</math> time.
 
''Grinch, The''
 
 
 
[[TADM2E 4.1|(Solution 4.1)]]
 
 
 
<br>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>, <math>19-3</math> maximizes the difference,
 
while <math>8-6</math> minimizes the difference.
 
<br>
 
(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.
 
<br>
 
(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.
 
<br>
 
(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.
 
<br>
 
(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.
 
 
 
[[TADM2E 4.2|(Solution 4.2)]]
 
 
 
<br>4-3.
 
Take a sequence 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.
 
 
 
[[TADM2E 4.3|(Solution 4.3)]]
 
 
 
<br>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).
 
 
 
[[TADM2E 4.4| (Solution 4.4)]]
 
 
 
<br>4-5.
 
The ''mode'' of a set of numbers is the number that occurs most frequently
 
in the set.
 
The set <math>(4,6,2,4,3,1)</math> has a mode of 4.
 
Give an efficient and correct algorithm to compute the mode of a set of
 
<math>n</math> numbers.
 
 
 
[[TADM2E 4.5|(Solution 4.5)]]
 
 
 
<br>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.)
 
 
 
[[TADM2E 4.6|(Solution 4.6)]]
 
 
<br>4-7.
 
Outline a reasonable method of solving each of the
 
following problems.
 
Give the order of the worst-case complexity of your
 
methods.
 
#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.
 
#You are given a list containing the title, author, call number and publisher of all the books in a school library and another list of 30 publishers. Find out how many of the books in the library were published by each company.
 
#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.
 
 
 
[[TADM2E 4.7|(Solution 4.7)]]
 
 
 
<br>4-8.
 
Given a set of <math>S</math> containing <math>n</math> real numbers, and a real number <math>x</math>.
 
We seek an algorithm to determine whether two elements
 
of <math>S</math> exist whose sum is exactly <math>x</math>.
 
#Assume that <math>S</math> is unsorted. Give an <math>O(n \log n)</math> algorithm for the problem.
 
#Assume that <math>S</math> is sorted. Give an <math>O(n)</math> algorithm for the problem.
 
 
 
[[TADM2E 4.8|(Solution 4.8)]]
 
 
 
<br>4-9.
 
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, such that
 
they appear exactly once in the union.
 
#Assume that <math>A</math> and <math>B</math> are unsorted. Give an <math>O(n \log n)</math> algorithm for the problem.
 
#Assume that <math>A</math> and <math>B</math> are sorted. Give an <math>O(n)</math> algorithm for the problem.
 
 
 
[[TADM2E 4.9|(Solution 4.9)]]
 
 
 
<br>4-10.
 
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>.
 
 
 
[[TADM2E 4.10|(Solution 4.10)]]
 
 
 
<br>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.
 
 
 
[[TADM2E 4.11|(Solution 4.11)]]
 
 
 
 
 
 
 
'''Heaps'''
 
 
 
<br>4-12.
 
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>.
 
 
 
[[TADM2E 4.12|(Solution 4.12)]]
 
 
 
<br>4-13.
 
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.
 
#Want to find the maximum element quickly.
 
#Want to be able to delete an element quickly.
 
#Want to be able to form the structure quickly.
 
#Want to find the minimum element quickly.
 
 
 
[[TADM2E 4.13|(Solution 4.13)]]
 
 
 
<br>4-14.
 
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 elementary <math>O(k n)</math>-time algorithm).
 
 
 
[[TADM2E 4.14|(Solution 4.14)]]
 
 
 
<br>4-15.
 
(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?
 
 
 
[[TADM2E 4.15|(Solution 4.15)]]
 
 
 
 
 
 
 
'''Quicksort'''
 
 
 
<br>4-16.
 
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?)
 
 
 
[[TADM2E 4.16|(Solution 4.16)]]
 
 
 
<br>4-17.
 
The ''median'' of a set of <math>n</math> values is the <math>\lceil n/2 \rceil</math>th
 
smallest value.
 
#Suppose quicksort always pivoted on the median of the current sub-array. How many comparisons would Quicksort make then in the worst case?
 
#Suppose quicksort were always to pivot on the <math>\lceil n/3 \rceil</math>th smallest value of the current sub-array. How many comparisons would be made then in the worst case?
 
 
 
[[TADM2E 4.17|(Solution 4.17)]]
 
 
 
<br>4-18.
 
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 operation permitted on the
 
keys are
 
#''Examine(A,i)'' -- report the color of the <math>i</math>th element of <math>A</math>.
 
#''Swap(A,i,j)'' -- 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.
 
This is also known as the Dutch national flag problem.
 
The simplest linear time solution performs two passes of the
 
partition operation from Quicksort.
 
The first pass treats the red and white elements as indistinguishable,
 
and separates them from the blue.
 
The second pass is just separates the elements within the red/white sub-array.
 
 
 
[[TADM2E 4.18|(Solution 4.18)]]
 
 
 
<br>4-19.
 
An ''inversion'' of a permutation is a pair of elements
 
that are out of order.
 
#Show that a permutation of <math>n</math> items has at most <math>n(n-1)/2</math> inversions. Which permutation(s) have exactly <math>n(n-1)/2</math> inversions?
 
#Let <math>P</math> be a permutation and <math>P^r</math> be the reversal of this permutation. Show that <math>P</math> and <math>P^r</math> have a total of exactly <math>n(n-1)/2</math> inversions.
 
#Use the previous result to argue that the expected number of inversions in a random permutation is <math>n(n-1)/4</math>.
 
 
 
[[TADM2E 4.19|(Solution 4.19)]]
 
 
 
<br>4-20.
 
Give an efficient algorithm to rearrange an array of <math>n</math> keys so
 
that all the negative keys precede all the nonnegative keys.
 
Your algorithm must be in-place, meaning you cannot allocate another
 
array to temporarily hold the items.
 
How fast is your algorithm?
 
 
 
[[TADM2E 4.20|(Solution 4.20)]]
 
 
 
 
 
'''Other Sorting Algorithms'''
 
 
 
<br>4-21.
 
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.
 
 
 
[[TADM2E 4.21|(Solution 4.21)]]
 
 
 
<br>4-22.
 
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 << n</math>.
 
 
 
<br>4-23.
 
We seek to sort 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.
 
 
 
[[TADM2E 4.23|(Solution 4.23)]]
 
 
 
<br>4-24.
 
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.
 
 
 
[[TADM2E 4.24|(Solution 4.24)]]
 
 
 
<br>4-25.
 
Assume that the array <math>A[1..n]</math> only has numbers
 
from <math>\{1,\ldots, 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>.
 
 
 
[[TADM2E 4.25|(Solution 4.25)]]
 
 
 
<br>4-26.
 
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</math>, <math>x = y</math>, or <math>x > y</math> holds.
 
#Give an algorithm to sort in <math>n-1</math> comparisons in the worst case. Show that your algorithm is optimal.
 
#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.
 
 
 
<br>4-27.
 
Let <math>P</math> be a simple, but not necessarily convex, 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.
 
 
 
[[TADM2E 4.27|(Solution 4.27)]]
 
 
 
 
 
 
 
'''Lower Bounds'''
 
 
 
<br>4-28.
 
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?
 
''lower bound''
 
 
 
<br>4-29.
 
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.)
 
 
 
[[TADM2E 4.29|(Solution 4.29)]]
 
 
 
 
 
 
 
'''Searching'''
 
 
 
<br>4-30.
 
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?
 
 
 
<br>4-31.
 
Suppose you are given an array <math>A</math> of <math>n</math> sorted numbers that has been
 
''circularly shifted'' <math>k</math> positions to the right.
 
For example, <math>\{35, 42, 5, 15, 27, 29\}</math> is a sorted array that has been
 
circularly shifted <math>k=2</math> positions, while <math>\{27, 29, 35, 42, 5, 15\}</math>
 
has been shifted <math>k=4</math> positions.
 
#Suppose you know what <math>k</math> is. Give an <math>O(1)</math> algorithm to find the largest number in <math>A</math>.
 
#Suppose you ''do not'' know what <math>k</math> is.  Give an <math>O(\lg n)</math> algorithm to find the largest number in <math>A</math>. For partial credit, you may give an <math>O(n)</math> algorithm.
 
 
 
[[TADM2E 4.31|(Solution 4.31)]]
 
 
 
<br>4-32.
 
Consider the numerical 20 Questions game.
 
In this game, Player 1 thinks of a number in the range 1 to <math>n</math>.
 
Player 2 has to figure out this number by asking the fewest number of
 
true/false questions.
 
Assume that nobody cheats.
 
#What is an optimal strategy if <math>n</math> is known?
 
#What is a good strategy if <math>n</math> is not known?
 
 
 
[[TADM2E 4.32|(Solution 4.32)]]
 
 
 
<br>4-33.
 
Suppose that you are given a sorted sequence of ''distinct'' integers
 
<math>\{a_1, a_2, \ldots, a_n\}</math>.
 
Give an <math>O(\lg n)</math> algorithm to determine whether there exists an <math>i</math> index
 
such as <math>a_i = i</math>.
 
For example, in <math>\{-10,-3,3,5,7\}</math>, <math>a_3 = 3</math>.
 
In <math>\{2,3,4,5,6,7\}</math>, there is no such <math>i</math>.
 
 
 
[[TADM2E 4.33|(Solution 4.33)]]
 
 
 
<br>4-34.
 
Suppose that you are given a sorted sequence of ''distinct'' integers
 
<math>\{a_1, a_2, \ldots, a_n\}</math>, drawn from <math>1</math> to <math>m</math> where <math>n < m</math>.
 
Give an <math>O(\lg n)</math> algorithm to find an integer <math>\leq m</math> that is not present in <math>a</math>.
 
For full credit, find the smallest such integer.
 
 
 
[[TADM2E 4.34|(Solution 4.34)]]
 
 
 
<br>4-35.
 
Let <math>M</math> be an <math>n \times m</math> integer matrix in which the entries
 
of each row are sorted in increasing order (from left to right)
 
and the entries in each column are
 
in increasing order (from top to bottom).
 
Give an efficient algorithm
 
to find the position of an integer <math>x</math> in <math>M</math>, or to determine that <math>x</math> is
 
not there.
 
How many comparisons of <math>x</math> with matrix entries does your
 
algorithm use in worst case?
 
 
 
[[TADM2E 4.35|(Solution 4.35)]]
 
 
 
 
 
 
 
'''Implementation Challenges'''
 
 
 
<br>4-36.
 
Consider an <math>n \times n</math> 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 zeroes in the same row or the same column.)
 
Describe an efficient algorithm that counts the number of occurrences
 
of the element <math>0</math> in <math>A</math>.  Analyze its running time.
 
 
 
<br>4-37.
 
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.
 
 
 
[[TADM2E 4.37|(Solution 4.37)]]
 
 
 
<br>4-38.
 
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.
 
 
 
<br>4-39.
 
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.
 
Later, compare the execution time to that of a purely sequential mergesort
 
implementation.
 
What are your experiences?
 
 
 
[[TADM2E 4.39|(Solution 4.39)]]
 
 
 
'''Interview Problems'''
 
 
 
<br>4-40.
 
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?
 
 
 
[[TADM2E 4.40|(Solution 4.40)]]
 
 
 
<br>4-41.
 
Describe advantages and disadvantages of the most popular sorting
 
algorithms.
 
 
 
[[TADM2E 4.41|(Solution 4.41)]]
 
 
 
<br>4-42.
 
Implement an algorithm that takes an input array and returns only the
 
unique elements in it.
 
 
 
<br>4-43.
 
You have a computer with only 2Mb of main memory.
 
How do you use it to sort a large file of 500
 
Mb that is on disk?
 
 
 
[[TADM2E 4.43|(Solution 4.43)]]
 
 
 
<br>4-44.
 
Design a stack that supports push, pop, and retrieving the minimum
 
element in constant time.
 
Can you do this?
 
 
 
[[TADM2E 4.44|(Solution 4.44)]]
 
 
 
<br>4-45.
 
Given a search string of three words,
 
find the smallest snippet of the document that contains all three of the
 
search
 
words---i.e., the snippet with smallest number of words in it.
 
You are given the
 
index positions where these words occur in the document, such as
 
''word1: (1, 4, 5)'',  ''word2: (3, 9, 10)'', and ''word3: (2, 6, 15)''.
 
Each of the lists are in sorted order, as above.
 
 
 
[[TADM2E 4.45|(Solution 4.45)]]
 
 
 
<br>4-46.
 
You are given 12 coins. One of them is heavier or lighter than the rest.
 
Identify this coin in just three weighings.
 
 
 
Note - weighings are with a balance, where you can have a greater than, equal to, or less than result.  You can't do this with a digital scale.
 
 
 
[[TADM2E 4.46|(Solution 4.46)]]
 

Revision as of 01:43, 14 July 2020

TaTaatatatattataa