DSA Worksheet One-2017EC
DSA Worksheet One-2017EC
2. Write the definition for Big-O and using the definition show that
a. √ b.
3. Arrange the following running times in increasing order of asymptotic growth. Justify your answer!
√
4. For each of the following six program fragments compute T(n) and Big-O
(1) sum = 0; (4) sum = 0;
for( i = 0; i < n; ++i ) for( i = 0; i < n; ++i )
++sum; for( j = 0; j < i; ++j )
++sum;
(2) sum = 0; (5) sum = 0;
for( i = 0; i < n; ++i ) for( i = 0; i < n; ++i )
for( j = 0; j < n; ++j ) for( j = 0; j < i * i; ++j )
++sum; for( k = 0; k < j; ++k )
++sum;
(3) sum = 0; (6) sum = 0;
for( i = 0; i < n; ++i ) for( i = 1; i < n; ++i )
for( j = 0; j < n * n; ++j ) for( j = 1; j < i * i; ++j )
++sum; if( j % i == 0 )
for( k = 0; k < j; ++k )
++sum;
5. Write the algorithm, find the running time T( ) and the Big-O class for the following problems.
a. Finding the minimum in list of numbers size .
2 2 2 2
b. Finding the sum: 1 +2 +3 +…+ where is a positive integer
th
c. Finding the largest number in list of numbers size N.
d. Copy an array of integers A in to an array B of same size
e. Adding two matrices A and B of size
f. Multiplying two matrices A and B of size
g. For determining a positive number is prime or not
6. An algorithm takes 0.5 ms for input size 100. How large a problem can be solved in 1 min if the running
time is the following:
3
a. T(n)=n b. T(n)=nlgn c. T(n)=n
7. What is searching? Give the algorithm for the following known searching algorithms and their efficiency in
best and worst case.
a. Sequential/ Linear Search
b. Binary Search(provide recursive and non recursive algorithms)
8. What is sorting? Give the algorithm for the following known sorting algorithms and their efficiency in best
and worst case.
a. Insertion sort b. Selection sort c. Bubble sort
d. For the following list show the partially sorted list after each iteration of the outer loop.
8, 9, -1, 5, 0,-2
9. Array is a popular data structure for storing list of items. What is an Array? Discuss its advantage and
limitations.
th th
10. The common operations performed on array include: Accessing i element, inserting at i position,
th
deleting the i element, searching, sorting , copying elements. Using c++:
a. Provide implementation for ADT “MyArray” that supports all the operations listed above. You
can use either struct or class for the ADT definition:
Hint: define the ADT as follows and provide implementation of the above operations
b. Using a static array like above may limit the maximum number of elements to be stored on the
array. Can you provide a dynamic array implementation that increases size when array is full?
Hint: define the ADT as follows using pointer and provide implementation for initialize,
increaseSize operation. increaseSize can be done by creating a new array of double size and
copying all elements.
struct MyDynamicArray{
int *data=NULL;
int n=-1;
}