You are on page 1of 2

Design and Analysis of Algorithms

Assignment 1

Due: Thursday, April 4, 2019

Submit Hand-Written Assignments only

Question 1
In class, we discussed a recursive algorithm (MergeSort) to sort an array of n numbers
Now you are going to design an iterative algorithm to do the same task
Suppose we have 8 numbers

3 1 7 2 4 5 8 9
We will pick group of one element each as follows

3 1 7 2 4 5 8 9
Then merge two groups at a time. As a result, we will have sorted groups of size 2. [You
have to think carefully how you will deal with it].

3 1 7 2 4 5 8 9
Now we will merge groups of size 2 and the result be a follows

3 1 7 2 4 5 8 9
After that, we will merge groups of size 4, then 8 and so on, until all the elements are in
one group.

Note: Remember that the Algorithm for merging two sorted arrays into one sorted
array will be the same as discussed in class. You have to change the recursive
implementation to iterative implementation. [Consider that count of elements in array
in in exact power of 2]
Question 2
Given an N element array, that array has unique integer values from within a range of 1
to N+1. [Only one number is missing from the range]. Your solution should find the
missing number in lgn time [consider that the given array is in sorted format]. You have
to think that how you will change the already available binary search to solve this
problem.
Question 3
Measure Entropy (Disorderness) in the data: [It is the measure, with which we can find
out how far is the data from being in order]. Formally speaking two elements make a
disorderness pair if A[i] > A[j] for i < j (if an element A[i] which appear first at i’th index in
array is greater than an element A[j] which comes at an index that is after i’th index).
Example: Let us say if we have an array A = {3, 10, 2, 5, 15}, it has the following
disordered pairs Ps = {(3, 2), (10, 2), (10, 5)} resulting in an entropy value of 3.
Write an algorithm, which finds the entropy value for a given array having N numbers.
Your Algorithm must run in O (N Log N) times.
In class, we discussed a recursive algorithm (MergeSort) to sort an array of n numbers
that runs in O (N Log N), how can we modify algorithm (MergeSort) that will help to
count the number of possible disordered pairs during the sorting work of the algorithm.
-----------------------
You can discuss and ask any question from me related to your solution process.

You might also like