Sie sind auf Seite 1von 2

BITS, Pilani - K K Birla Goa Campus.

Sem II, 2015-16


Data Structures and Algorithms CS F211
Test 1(50/290 points)
Closed Book. Relax and solve puzzles. Each question must be solved on a new page.
Rough work must not appear on the pages containing the final solution.
Examiners reserve the right to penalise clumsiness.

1 (i). Given a sequence [ai ], i = 1..m of positive integers, suppose we replace any pair among them
by their pair sum, getting a sequence of length m − 1 now. We can continue like this until we are left
with a singleton. Now we make a sum of all the pair sums that we had obtained along this reduction
process. Find a way to minimize this final sum for all ways of pairings throughout, for
any such sequence of positive integers, for any m. Remember, this sequence need not be
sorted in any way. To get a handle on this, do actual counting for small example sequences. (This
is a “find an algorithm” problem but we do not want pseudocode or a program; instead we want a
succinct central idea of this algorithm.) [10]
Example pair-sum-iteration: [4, 6, 3, 10] => [10, 3, 10] => [13, 10] => [23], final sum 10 + 13 + 23 =
←→ ←−→
46.
Another way [← 4 , 6, ←
→ 3 , 10] => [7, 6, 10] => [13, 10] => [23], final sum 7 + 13 + 23 = 43.
→ ←→
(ii). Using the algorithm obtained in the previous part, give a way of efficiently sorting by merg-
ing thus: we first detect all maximum length subarrays that are nondecreasing. For example, in
[3, 5, 2, 7, 8, 10, 1, 9, 4] the subarrays fitting this description are [[3, 5], [2, 7, 8, 10], [1, 9], [4]]. Then we
merge them, in an order of pairings obtained by the idea in the part 1(i) of this question. (Again
this is a “find an algorithm” problem but we do not want pseudocode or a program; instead we want
a succinct central idea of this algorithm.) [10]

2. Show the working of QuickSortX on the array [10, 3, 5, 8, 6, 7, 1, 2, 4, 9] where the implemen-
tation of QuickSortX is as follows: For arrays of length not more than 3, sorting is done using
insertion. For larger arrays, the pivot is chosen as the median of the first three elements in the
array, and this is swapped first with the first element, which becomes the pivot. Then the recursive
QuickSort follows as usual, with Hoare Partitioning (: “closing in from the two ends”). Assuming
that sorting 3 or less elements by insertion happens like a single operation, show the full array
A[0..n − 1] each time the PrintArray line is executed. [15]

QuickSortX(A,l,r,n):
if(r>l+2) then:
FindAndSwapMedianOf3(A,l); p=HoarePartition(A,l,r)
PrintArray(A,0,n-1)
QuickSortX(A,l,p-1,n); QuickSortX(A,p+1,r,n)
else:
InsertionSort(A,l,r)
endif

3. Consider the following algorithm to find the minimum and the maximum of an array simultane-
ously:

FindMinMax(A,l,r):
a=A[l]; b=A[l]
for i=l+1 to r: do
if(a>A[i]) then a=A[i]
elseif(b<A[i]) then b=A[i]
done
return [a,b]
Give a best case and a worst case permutation each for this algorithm (corresponding to the least and
the most number of comparisons respectively) for an unsorted array of length n containing distinct
{1, 2, ..., n} numbers. [8]

4. Suppose we know that the minimum and the maximum in an array of n elements differ by d.
Can this knowledge be used to obtain a sorting algorithm that is faster than Ω(n log2 n) in the worst
case? Argue. [7]
END.

Das könnte Ihnen auch gefallen