Assignment 03 Solution: TUESDAY 06/12/2022. Warning

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Department of Computer Science CSC-321:Design and Analysis of Algorithm

Bahria University Semester 05 (Fall 2022)

ASSIGNMENT 03
SOLUTION
Marks: 10

NAME: ______________________________________________
CLASS:_______________________________________________
REG. NO. ____________________________________________
COURSE:_____________________________________________
COURSE CODE:________________________________________

Marks Obtained: ______________________

Theoretical Assignment

Read Carefully:
• The deadline for this assignment is before or on TUESDAY 06/12/2022.

WARNING: This is an individual assignment; you must solve it by yourself. Any form of plagiarism
will result in receiving a zero on the assignment.

WARNING: Late submissions will not be accepted. Any assignment submitted after the cutoff
time will receive zero.

• You have to answer and submit your solution in the SOFTCOPY form on LMS.

Attribute Problem Solving Description


Depth of analysis required Has no obvious solution, and requires conceptual thinking and innovative analysis to
formulate suitable abstract models
Depth of knowledge A solution requires the use of in-depth computing or domain knowledge and an
required analytical approach that is based on well-founded principles

Level of problem Is outside problems encompassed by standards and standard practice for professional
computing
The solution must be designed by applying the following attributes/characteristics

1|Page
CS Department, BUKC 2 Semester 05 (Fall 2022)
CSC-321: DAA Assignment 03

Question (2.5+2.5=5 Marks)


(CLO-3, PLO-4, C-6)

Binary search is a classical algorithm. Given an array A[1..n] sorted in ascending order, binary search
can find whether an element b is in array A. The algorithm works as follows:

binary_search(A[1..n], b)
If n <= 2 then check whether b is in A by looking through all elements.
Let k = n/2
Partition A into B, C where B contains A [1...k-1], and C contains A[k+1..n]
If A[k] == b then b is in array A
If A[k] > b then call binary_search(B, b)
If A[k] < b then call binary_search(C, b)

(Note: you can also describe this algorithm as: When length of A is at least 3, compare b against the
middle element in A, if b is larger than search in the right half of A, if b is smaller than search in the
left half of A.)

a) Elaborate the running time of the binary search algorithm.


b) Using similar ideas, design a related problem. In this problem, you are given an array A [1..n]
that is first decreasing and then increasing. More precisely, there is a coordinate 1 ≤ p ≤ n such
that for all i < p, A[i] > A[i + 1], and for all i ≥ p, A[i] < A[i + 1]. Your goal is to find the
smallest element in this array. Please design an algorithm with the same asymptotic running
time as a binary search.

Answer:

Constant Time — O(1): Best Case Run Time

Given an arbitrary number of inputs (n), it will only require the algorithm to perform one action to

complete the function.

Logarithmic Time — O(log n): Mediocre Case Run Time

With (n) number of inputs the algorithm will still need to perform more than 1 step, however, with

each new step it performs, the ratio of inputs to remaining steps decreases, with the number of

remaining steps shrinking to 0, eventually. More, here.


CS Department, BUKC 3 Semester 05 (Fall 2022)
CSC-321: DAA Assignment 03

Linear Time — O(n): Worst Case Run Time

Taking in (n) number of inputs the algorithm will perform (n) steps to completion.

Part b will be different for every student.

***********************************

You might also like