Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The binary search algorithm that follows may be used to search an array when the elements are in order. This algorithm is analogous to the

The binary search algorithm that follows may be used to search an array

when the elements are in order. This algorithm is analogous to the following

approach for finding a name in a telephone book.

a. Open the book in the middle, and look at the middle name on the page.

b. If the middle name isnt the one youre looking for, decide whether it

comes before or after the name you want.

c. Take the appropriate half of the section of the book you were looking in

and repeat these steps until you land on the name.

ALGORITHM FOR BINARY SEARCH

1. Let bottom be the subscript of the initial array element.

2. Let top be the subscript of the last array element.

3. Let found be false.

4. Repeat as long as bottom isnt greater than top and the target has not been found

5. Let middle be the subscript of the element halfway between bottom and

top.

6. if the element at middle is the target

7. Set found to true and index to middle.

else if the element at middle is larger than the target

8. Let top be middle 1.

else

9. Let bottom be middle + 1.

Write and test a function binary_srch that implements this algorithm for an

array of integers. When there is a large number of array elements, which function

do you think is faster: binary_srch or the linear search function of Fig. 7.14 ?

Please write Code in C and provide comments for each action.

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

Concepts Of Database Management

Authors: Philip J. Pratt, Joseph J. Adamski

4th Edition

0619064625, 978-0619064624

More Books

Students also viewed these Databases questions

Question

13-1 How does building new systems produce organizational change?

Answered: 1 week ago