Professional Documents
Culture Documents
1. Consider a function that reads a matrix of numbers and outputs the sum of all
the entries. What do you think could be the worst-case complexity (running
time) of this program?
The worst-case complexity is O (M*N) for a 2-Dimensional matrix with M-rows
and N-columns.
2. Make guesstimates of average-case complexity when the matrix represents the
consumption of various vegetables for each month of the year.
O(n) for 1-Dimensional matrix for a single month. For 12 months it would be
O(12n).
3. For the case when the matrix represents the number of days of the year for
which an Infoscion reports to another Infoscion directly. Did you need to
make any assumptions about internal data structures?
We need to plan for the matrix size and dimension for number of employees and
the number of days each one contacting to others.
4. Which of the following statements is/are true?
Q: A square matrix algorithm that is O(n2), where n is the “height” of the
matrix is O(m) where m is the total number of elements in the matrix-false
Q: An O(n2) algorithm is an O(n3) algorithm is an O(n4) algorithm-false
Q: An O(1034n) algorithm is an O(n) algorithm-false
Q: An O(log2n) algorithm is an O(logen) algorithm is an O(log10n)
Algorithm-false
Q: Beyond a threshold problem size, an O(n2) algorithm always performs
better than an O(n3) one-true
Q: Beyond a threshold problem size, an O(nk) algorithm always performs
better than an O(2n) one-false
5. A list of employee records sometimes needs to be accessed in increasing order
of salary, and sometimes in the reverse order. What is the complexity of
reversing a linked list? A doubly linked list?
Time complexity of reversing a doubly linked list is O(n)
Time complexity of reversing a linked list is also O(n)
6. What is the worst-case complexity of finding a given element in a binary
search tree?
worst-case complexity of finding a given element in a binary
search tree is n log n.
7. Compute the worst-case complexity of Euclid’s algorithm for computing the
greatest common divisor. (Note: The algorithm works by continually
computing remainders until 0 is reached. For example, if numbers given are
2340 and 1761, then the sequence of remainders is 579, 24, 3, and 0. Thus, 3
is the greatest common divisor)
worst-case complexity of Euclid’s algorithm for computing the
greatest common divisor is O(n).
8. Plot the actual running time of your implementation of bubble sort, insertion
and selection sort. Take an array size of 100 for the following kinds of input:
90% sorted array given, 90% reverse sorted array given, array with elements
alternating (10, 5, 8, 6, 20, 14,…), and array with randomly generated
elements.
Since the algorithm does not know whether the input is been sorted or not, it will
do the sorted inputs also in the same way like unsorted input.
Bubble Sort Insertion Sort Selection Sort
Worst Best Worst Best Worst Best
90% O(n )=10000 O(n)=10 O(n )=10000 O(n)=10 O(n )=10000 O(n2)=10000
2 2 2
sorted 0 0
array
90% O(n2)=10000 O(n)=10 O(n2)=10000 O(n)=10 O(n2)=10000 O(n2)=10000
reverse 0 0
sorted
array
Alternated O(n2)=10000 O(n)=10 O(n2)=10000 O(n)=10 O(n2)=10000 O(n2)=10000
elements 0 0
Random O(n2)=10000 O(n)=10 O(n2)=10000 O(n)=10 O(n2)=10000 O(n2)=10000
elements 0 0