logo

Pseudocode Case Complexity

   

Added on  2022-08-22

3 Pages496 Words23 Views
Question 1:
Algorithm:
The algorithm is used to have two seperate loop which add an integer with m digits and integer with
n digits.
Pseudocode:
Procedure findSum ()
set sum=0
for (i : mInt) {
sum= sum +i
}
for (i : nInt) {
sum= sum +i
}
return sum
end procedure
The complexity of first loop is O(m) and second loop is O(n) then combine complexity is O(m) +
O(n) = O(m+n).
Question 2:
Algorithm:
For this mentioned the algorithm is Binary search, the best case complexity is O(1) the searched
element is present at mid-point, and worst case complexity is O(logn).
This algorithm is based on the Divide and conquer principle. Binary search compares the element
with the middle item in the collection if match is found then index is returned and if middle value is
greater than the element searched then it search to the sub array to the left of the middle term and if
middle term is small then it search in sub array to the left of middle term, this searching continues
until the sub array becomes to zero.
Pseudocode:
function binarySearch
T – Sorted array
n – Array size
i – Index to be searched
set lower=1
set upper =n
while i not found:
if upper < lower
EXIT : i does not exists

End of preview

Want to access all the pages? Upload your documents or become a member.

Related Documents
AVL Tree, Set Intersection Algorithms, Sloppy Inc. Communication, Array Search
|9
|2413
|78

Desklib SEO Suggestions
|9
|2414
|194

Linear Search Algorithm Analysis
|5
|1007
|121

Algorithm Analysis and Examples
|15
|1404
|218

Problem 2. C-11.37 Suppose we wish to support a new met
|2
|483
|264

Top-Down Approach for Palindrome Partitioning Problem
|3
|984
|331