Unit 1 To 5 Qbank Ans

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Unit 1 : Fundamentals

1. (i) Describe briefly the notations used in the analysis of an algorithm.


(ii) Write an algorithm to perform linear search in an array of n numbers. Write its
asymptotic complexity

Linear search is a very simple search algorithm. In this type of search, a sequential search is
made over all items one by one. Every item is checked and if a match is found then that
particular item is returned, otherwise the search continues till the end of the data collection.

Algorithm
Linear Search ( Array A, Value x)

Step 1: Set i to 1
Step 2: if i > n then go to step 7
Step 3: if A[i] = x then go to step 6
Step 4: Set i to i + 1
Step 5: Go to Step 2
Step 6: Print Element x Found at index i and go to step 8
Step 7: Print element not found
Step 8: Exit

Pseudocode
procedure linear_search (list, value)

for each item in the list


if match item == value
return the item's location
end if
end for

end procedure

To know about linear search implementation in C programming language, please click-here.


2.(i) Discuss Conditional asymptotic notation with a neat diagram
https://www.bing.com/videos/search?
q=+Conditional+asymptotic+notation+with+a+neat+diagram&&view=detail&mid=0E5D2627
8515B0A6DC4A0E5D26278515B0A6DC4A&&FORM=VRDGAR&ru=%2Fvideos%2Fsearch
%3Fq%3D%2BConditional%2Basymptotic%2Bnotation%2Bwith%2Ba%2Bneat%2Bdiagram
%26FORM%3DHDRSC3
(ii) List the properties of Big oh-notation
Notes
3 (i) Explain the steps involved and various criteria used in analyzing algorithms

n this article, we will discuss why algorithm and its analysis is important?

In the analysis of the algorithm, it generally focused on CPU (time) usage, Memory usage,
Disk usage, and Network usage. All are important, but the most concern is about the CPU
time. Be careful to differentiate between:

 Performance: How much time/memory/disk/etc. is used when a program is run. This


depends on the machine, compiler, etc. as well as the code we write.
 Complexity: How do the resource requirements of a program or algorithm scale, i.e.
what happens as the size of the problem being solved by the code gets larger.

Note: Complexity affects performance but not vice-versa.

Algorithm Analysis:
Algorithm analysis is an important part of computational complexity theory, which provides
theoretical estimation for the required resources of an algorithm to solve a specific
computational problem. Analysis of algorithms is the determination of the amount of time
and space resources required to execute it.

Why Analysis of Algorithms is important?

 To predict the behavior of an algorithm without implementing it on a specific


computer.
 It is much more convenient to have simple measures for the efficiency of an algorithm
than to implement the algorithm and test the efficiency every time a certain parameter
in the underlying computer system changes.
 It is impossible to predict the exact behavior of an algorithm. There are too many
influencing factors.
 The analysis is thus only an approximation; it is not perfect.
 More importantly, by analyzing different algorithms, we can compare them to
determine the best one for our purpose.

Types of Algorithm Analysis:

1. Best case 
2. Worst case
3. Average case

 Best case: Define the input for which algorithm takes less time or minimum time. In
the best case calculate the lower bound of an algorithm. Example: In the linear search
when search data is present at the first location of large data then the best case occurs.
 Worst Case: Define the input for which algorithm takes a long time or maximum
time. In the worst calculate the upper bound of an algorithm. Example: In the linear
search when search data is not present at all then the worst case occurs.
 Average case: In the average case take all random inputs and calculate the
computation time for all inputs.
And then we divide it by the total number of inputs.
          Average case = all random case time / total no of case

4.What is Time-Space Tradeoff? Design a space efficient and time efficient algorithm
for swapping the numbers and reversing the contents of an array

Time Space Trade Off:

It is a way of solving a problem or calculation in less time by using more storage


space (or memory), or by solving a problem in very little space by spending a long
time. 

It is a case where an algorithm or program trades increased space usage with


decreased time. Here, space refers to the data storage consumed in performing a given
task (RAM, HDD, etc), and time refers to the time consumed in performing a given
task (computation time or response time)

Need of Time Space Tradeoff:

 1.) Less time by using more memory) 

2.) By solving a problem in very little space by spending a long time.

Example involving the concept of Time Space Tradeoff:

1.If data is stored uncompressed,it takes more space but less time.

2.Storing only the source and rendering it as an image everytime the page is requested
would be trading time for space. More time used but less space

5.Define recurrence equation. Describe the methods for solving recurrences with
time complexity of different algorithms
    
https://www.geeksforgeeks.org/how-to-solve-time-complexity-recurrence-relations-using-
recursion-tree-method/

You might also like