Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. State the size of the input in big-Oh. For example, if the input to an algorithm is an array of size k, then the

1. State the size of the input in big-Oh. For example, if the input to an algorithm is an array of size k, then the input size is O(k).

2. Derive the cost function for the algorithm. Be sure to show your work. You can use O(1) to denote all constants.

3. State the complexity of the algorithm in Big-Oh. The complexity MUST be stated in terms of input-size.

Problem: Root of the Problem

input: integer n

output: closest integer that is the square root of n

l = 1

h = n

while l < h

r = (l + h) / 2

s = r * r

if s == n

break

if s < n

l = r

else

h = r

return s

Problem: Last Resort?

input: A[k] // 1D array of size k, k is a power of 2

output: sorted array A

B = array(k)

size = 1

while size <= k / 2

count = (k / 2) / size

for i = 0 ... count

combine(A, i * size * 2, size, B)

for i = 0 ... k

A[i] = B[i]

size = size * 2

return A

def combine(A, start, size, B)

h = start

i = start

j = start + size

end = start + size

while (i < end) and (j < end + size) i

f A[i] <= A[j]

B[h] = A[i]

i = i + 1

else B[h] = A[j]

j = j + 1

h = h + 1

if j < end + size

i = j

end = end + size

while i < end

B[h] = A[i]

i = i + 1

h = h + 1

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions