Professional Documents
Culture Documents
int a = 0, b = 0;
for (i = 0; i < N; i++) { void recursion(int n)
a = a + rand(); {
} if(n == 1) return;
for (j = 0; j < M; j++) { recursion(n-1);
b = b + rand(); print(n);
} recursion(n-1);
}
1. O(N * M) time, O(1) space
2. O(N + M) time, O(N + M) space
3. O(N + M) time, O(1) space 7.
sum += i;
} 8. T(n) = T( n )+n
5.
int sum = 0, i;
for(i=0;i<n;i++)
int j = 1, i = 0; {
sum += i;
while(i<=n){ }
i = i+j;
j++;
}
1
15. Sort the following functions in the decreasing
10. What is the time complexity of following code:
order of their asymptotic (big-O) complexity:
Options:
1. O(N)
16. f(n) = 2^(2n)
2. O(N*log(N))
Which of the following correctly represents the
3. O(N * Sqrt(N))
above function?
4. O(N*N)
(a) O(2^n)
(b) Ω(2^n)
{ (a) O(n^2)
k = k + n / 2; (b) O(n^(3/2))
Options:
18.
1. O(n)
2. O(nLogn)
3. O(n^2)
int gcd(int n, int m){
4. O(n^2Logn)
Options:
while (m > 0){
1. X will always be a better choice for small n = n%m;
inputs
3. O(N / 2)
}
4. O(log N)
for (k = 0; k < N; k++){
(a) O(n^2.8)
(b) O(n^3)
(c) θ(n^2.8)
(d) θ(n^3)
2
Assignment-2
Q1. For this problem, assume that you are using Insertion Sort. Assume that
the fi rst, second, and third smallest elements are next to each other
(somewhere in the input), the fourth, fi fth, and sixth smallest elements are
next to each other, the 7th, 8th, and 9th elements are next to each other, etc.
For example, 80, 70, 90, 40, 50, 60, 30, 10, 20, 120, 110, 100
(The algorithm does not know this and executes without this extra
information.) Assume the
problem size n is a multiple of 3. For each part show your work.
(a) Assume each of the above triples are in reverse order (so that the third
smallest element
comes before the second smallest which comes before the smallest, the sixth
smallest comes
before the fifth smallest which comes before the fourth smallest, etc.). For
example,
90, 80, 70, 60, 50, 40, 30, 20, 10, 120, 110, 100
What is the exact number of comparisons in the best case?
(b) Assume each of the above triples are in order (so that the smallest
element comes before
the second smallest which comes before the third smallest, the fourth
smallest comes
before the fifth smallest which comes before the sixth smallest, etc.). For
example,
70, 80, 90, 40, 50, 60, 10, 20, 30, 100, 110, 120
What is the exact number of comparisons in the worst case?
(c) Assume each of the above triples are in reverse order (as in part (a)), and
n = 6. Calculate
the exact number of comparisons in the average case?
Bubble Sort
Selection Sort
Insertion Sort
Merge Sort
Quick Sort
Heap Sort
Counting Sort
DFS
BFS
Kruskal’s Algorithm
Q.3. What will happen if quick sort divide the array in 99% and 1% on each
partition call.
T(n) = T(99*n/100) + T(n/100) + n
T(1) = 1
What will be the time complexity? Solve the above equation step by step
Q.4.
PART 1:- Modify insertion sort to apply on linked list.
PART 2:- Can we sort the linked list while inserting a new node in the
list. If Yes,
then write the code for that also.
Q.5. Given two character strings x[1..n] and y[1..m], the edit distance is the
cost of transforming the string x to y using a minimum number of operations
from the set {replace, insert, delete}. Design an effi cient algorithm to fi nd the
minimum edit distance between two given strings.
What if there are specifi c costs associated with each of the operation and you
want to minimize the total cost ? This has direct application to DNA
sequencing problem, i.e. how close they are to each other.
Q.9. An element is common, if it occurs more than n/4 times in in a given set
of n elements. Design an O(n) algorithm to find a common element if one
exists.
Q.10. We are given a sequence of integers in the range [1, n] where each
value occurs at most once. An operation called EXTRACT-MIN, occurs at
arbitrary places in the sequence which detects the minimum element up to
that point in the sequence and deletes it.
For example
3,7,1,E,9,4,E,... the output is 1,3.