Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. Consider the Algorithm ARRAYFIND, given below, which searches an array A for an element x. Input: An element x and an n-element array,

image text in transcribed 

1. Consider the Algorithm ARRAYFIND, given below, which searches an array A for an element x. Input: An element x and an n-element array, A[0,..,n - 1]. (Indices start from 0.) Output: The index i such that x =A[i] or -1 if no element of A is equal to x. ARRAYFIND(A,x) 1. i=0 2. r = -1 while in do if x == A[i] 3. 4. 5. r = i 6. break 7. else 8. 9. i = i + 1 return r A) [10 marks] Counting assignments, comparisons, additions, breaks, and returns only, calculate the exact worst-case and best-case running times of ARRAYFIND. Note that of these 5 operations are all simple and take exactly one time step. Do not count memory-access or other simple operations. As your answer, you need to write down two functions in terms of n; one for the best-case and one for the worst-case. Do not use asymptotic notations or parametric constants for this part. B) [5 marks] Use the Big-O definition to prove that the worst-case running time of ARRAYFIND (calculated in part A) is O(n). C) [5 marks] Use the Big-2 definition to prove that the best-case running time of ARRAYFIND (calculated in part A) is 2(1). D) [5 marks] Explain briefly why we CANNOT say that the running time of ARRAYFIND is (n). (Here the running time is not necessarily for the best-case or for the worst-case input.)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

A WorstCase Running Time In the worstcase scenario the element x is not present in the array or is t... 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_2

Step: 3

blur-text-image_3

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

Introduction to Algorithms

Authors: Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest

3rd edition

978-0262033848

More Books

Students also viewed these Programming questions