Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

Given a sorted array of distinct integersA[1,...,n] , you want to find out whether there is an indexi for which A[i]=i. Give a divide-and-conquer algorithm that runs in time O(logn).

Short Answer

Expert verified

By using binary search, only certain that there is no such value to the left of the middle element if array[mid]<=startindex, where mid is index of middle element, and start index is the start of the array. A divide-and-conquer algorithm is constructed with run time O(logn).

Step by step solution

01

Binary search algorithm and Divide-and-conquer algorithm.

Binary Search is used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity toO(logn) .Binary Search also known as half-interval search, logarithmic search, or binary chop.Binary Search Algorithm can be implemented in the following two ways,

1.Iterative Method

2. Recursive Method

02

Sorting algorithm to be use in given situation

Use modification of binary search here :

Basically we have to find index such that a[i]=iinothera[i]-i=0.

Now as the array is sorted , so all the indices j which is less than i will satisfy the property

:a[j]-j<=0

as the array is sorted so a[j] will be at least one less than a[i] and left index will also be at least one less than i hence the conclusion.

Similarly to the right of the last index element .

a[j]-j>=0.

So the key idea is :

mid=left+right/2where left and right are the boundary indices and then check whether mid is the last index ,index we are looking for using the above mentioned property.

For this we compare only its immediate left and right neighbours.

Given that the array is โ€œSORTEDโ€. We can more use of this property to improve the fine complexity to O(logn).

-1

2

5

7

11

20

30

Ex: A

Letโ€™s say we to search โ€œ20โ€ in our Array. First we go for middle element i.e.,7

Search if 7>20?? False

If 7>20teen we will only search on the right hand side of 7.

Then, we have to search only in

112030

Here, we are dividing the large problem into a sub-problem with less sited array.

Mid=20

Apply same procedure.

Here, we found the mid value as required 20. This approach is called โ€œBinary Searchโ€

03

Algorithm

Algorithm BinarySearch(arr[],min,max,key)

{

If (max<min)then

Else return false.

Mid=(max+min)/2

If arr[mid]>keythen

Return BinarySearch(arr,min,midโˆ’1,key)

Else if arr[mid]<keythen

ReturnBinarySearch(arr,mid+1,max,key)

Else

Return

End if

End if

}

array[mid] <= start index

p[1โ€ฆ.n]is array of words frequencies of sizeโ€˜nโ€™.

for(s=1ton)for(i=0ton-s)j=i+sif(i=j)

T(i,j)=p[i]for(k=itoj+1)T(i,j)=T(i,j)+p[k]returnT(0,n-1)

This algorithm is constructed or take run time of O(logn).

Here, the analysis of algorithm is given below, binary search, only certain that there is no such value to the left of the middle element ifrole="math" localid="1659776120568" array[mid]<=startindex, where mid is index of middle element, and start index is the start of the array.And divide-and-conqueralgorithm is constructed with run timeO(logn)

Every fine we are dividing the array into half. i.e.,

Ifn=100, next call will be,

Calln=50,25,12,6,3,1base case.

We are performing logn operations.

Hence, time complexity: O(logn)

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

Consider the task of searching a sorted array A[1,โ€ฆ,n] for a given element x: a task we usually perform by binary search in time O(logn) . Show that any algorithm that accesses the array only via comparisons (that is, by asking questions of the form โ€œis A[i]โ‰คz 0?โ€), must take ฮฉ(logn) steps.

How many lines, as a function of n (in โŠ(.)form), does the following program print? Write a recurrence and solve it. You may assume is a power of . function f (n) if n > 1:

print_line (โ€˜โ€˜still goingโ€™โ€™)

f (n/2)

f (n/2)

Section 2.2 describes a method for solving recurrence relations which is based on analyzing the recursion tree and deriving a formula for the work done at each level. Another (closely related) method is to expand out the recurrence a few times, until a pattern emerges. For instance, letโ€™s start with the familiar T(n)=2T(n/2)+o(n). Think of o(n) as being role="math" localid="1658920245976" <cnfor some constant , so: T(n)<2T(n/2)+cn. By repeatedly applying this rule, we can bound T(n) in terms of T(n/2), then T(n/4), then T(n/8), and so on, at each step getting closer to the value of T(.) we do know, namely .

T(1)=0(1).

T(n)โ‰ค2T(n/2)+cnโ‰ค2[2Tn/4+cn/2]+cn=4T(n/4)+2cnโ‰ค4[2Tn/8+cn/4]+2cn=8T(n/8)+3cnโ‰ค8[2Tn/16+cn/8]+3cn=16T(n/16)+4cn

.

.

.

A pattern is emerging... the general term is

T(n)โ‰ค2kT(n/2k)+kcn

Plugging in k=log2n, we get T(n)โ‰คnT(1)+cnlog2n=0(nlogn).

(a)Do the same thing for the recurrence T(n)=3T(n/2)+0(n). What is the general kth term in this case? And what value of should be plugged in to get the answer?(b) Now try the recurrence T(n)=T(n-1)+0(1), a case which is not covered by the master theorem. Can you solve this too?

In justifying our matrix multiplication algorithm (Section 2.5), we claimed the following block wise property: if X and Y are nร—nn matrices, and

X=[ABCD],Y=[EFGH],

where A,B,C,D,E,F,G, and H are n/2ร—n/2 sub-matrices, then the product XY can be expressed in terms of these blocks:

XY=[ABCD][EFGH]=[AE+BGAF+BHCE+DGCF+DH]

Prove this property.

Question: On page 66 there is a high-level description of the quicksort algorithm.

(a) Write down the pseudocode for quicksort.

(b) Show that its worst - case running time on an array of size n is ฮ˜(n)2.

(c) Show that its expected running time satisfies the recurrence relation.

T(n)โ‰คO(n)+1nโˆ‘i=1n-1(Ti+Tn-i)

Then, show that the solution to this recurrence is O(nlogn).

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free