0% found this document useful (0 votes)
16 views

Algorithm and data structures question

Algorithm and data structures question

Uploaded by

himanshujhaa4262
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Algorithm and data structures question

Algorithm and data structures question

Uploaded by

himanshujhaa4262
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

DAA Assignment 2

Submission Date : 7 October 2024

Q. No Question

1. Suppose that we have an array of n data records to sort and that the key of each record
has the value 0 or 1. An algorithm for sorting such a set of records might possess some
subset of the following three desirable characteristics:
1. The algorithm runs in O(n) time.
2. The algorithm is stable.
3. The algorithm sorts in place, using no more than a constant amount of storage
space in addition to the original array.
a. Give an algorithm that satisfies criteria 1 and 2 above.
b. Give an algorithm that satisfies criteria 1 and 3 above.
c. Give an algorithm that satisfies criteria 2 and 3 above.

2. You are given an array of strings, where different strings may have different
numbers of characters, but the total number of characters over all the strings is n.
Show how to sort the strings in O(n)time.
(Note that the desired order here is the standard alphabetical order; for example, a < ab
< b.)

3. In the algorithm SELECT, the input elements are divided into groups of 5. Will the
algorithm work in linear time if they are divided into groups of 7? Argue that SELECT
does not run in linear time if groups of 3 are used.

4. Describe an O(n) time algorithm that, given a set S of n distinct numbers and a
positive integer k <=n, determines the k numbers in S that are closest to the median
of S.

5. Let X[1....n] and Y[1.....n] be two arrays, each containing n numbers already in sorted
order. Give an O(logn) time algorithm to find the median of all 2n elements of
concatenate arrays X and Y.

6. In merge-sort algorithm discussed in class, rewrite the MERGE procedure so that it does
not use sentinels, instead stopping once either array L or R has had all its elements
copied back to A and then copying the remainder of the other array back into A.

7. a. A sorting method is said to be stable if at the end of the method, identical


elements occur in the same order as in the original unsorted set. Is merge sort a
stable sorting method? Support your answer properly.
b. Write the pseudocode of merge sort algorithm and explain with suitable example
(using input array 1, 15, 24, 26, 2, 13, 27, 38). Also, give the worst time complexity
analysis of the algorithm.
c. Suppose we are comparing implementations of insertion sort and merge sort on
the same machine. For inputs of size n, insertion sort runs in 8n^2 steps, while
merge sort runs in 64n lg n steps. For which values of n does insertion sort beat
merge sort?

8. Banks often record transactions on an account in order of the times of the transactions,
but many people like to receive their bank statements with checks listed in order by
check numbers. People usually write checks in order by check number, and merchants
usually cash the with reasonable dispatch. The problem of converting time-of-transaction
ordering to check-number ordering is therefore the problem of sorting almost-sorted
input. Argue that the procedure INSERTION-SORT would tend to beat the procedure
QUICKSORT on this problem

9. How you handle repeating elements in merge sort and quick sort? Also justify a stable
version if not stable already

10. Given an unsorted array of 10^7 integers, what factors would affect the performance of
a linear search in practice, aside from the theoretical O(n) time complexity?

11. How would the expected number of comparisons change if you know that the element x
is more likely to be found in the first half of the array? Assume a probability distribution
where x is twice as likely to be in the first half compared to the second half.

12. Describe a strategy for parallelizing linear search to speed up the search process? What
challenges might arise in implementing such a strategy?

13. Recall the problem of finding the number of inversions. As in the text, we are given a
sequence of n numbers a1,..., an, which we assume are all distinct, and we define an
inversion to be a pair i < j such that ai > aj. We motivated the problem of counting
inversions as a good measure of how different two orderings are. However, one might feel
that this measure is too sensitive. Let’s call a pair a significant inversion if i < j and ai>
2aj. Give an O(n log n) algorithm to count the number of significant inversions between
two orderings.

14 A Bank has a collection of n bank cards that they’ve confiscated, suspecting them of
being used in fraud. Each bank card is a small plastic object, containing a magnetic
stripe with some encrypted data, and it corresponds to a unique account in the bank.
Each account can have many bank cards corresponding to it, and we’ll say that two bank
cards are equivalent if they correspond to the same account. It’s very difficult to read the
account number off a bank card directly, but the bank has a high-tech “equivalence
tester” that takes two bank cards and, after performing some computations, determines
whether they are equivalent. Their question is the following: among the collection of n
cards, is there a set of more than n/2 of them that are all equivalent to one another?
Assume that the only feasible operations you can do with the cards are to pick two of
them and plug them in to the equivalence tester. Show how to decide the answer to their
question with only O(n log n) invocations of the equivalence tester.

15 Let S be a set of n points in the plane.It is given that there is only a constant (say c)
number of points on the hull of S. Can you devise a Convex hull algorithm for S that
runs in time o(nlogn)? Conceive of Special algorithms for c = 3 and c = 4 first and then
generalize.

16 Assuming n is a power of 2, find the exact number of scalar additions and multiplications
needed by Strassen’s algorithm to multiply two nXn matrices.

17 Write an algorithm that multiplies two nxn matrices using O(n3) operations. Determine
the precise number of multiplications, additions, and array element accesses.

You might also like