Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

We want to code a C program to find a value using binary search method. Binary search method is used as following. First of all,

We want to code a C program to find a value using binary search method. Binary search method is used as following. First of all, you must sort the elements of arrays and then you should divide the search interval in half. Begin with an interval covering the whole array. If the value of the search key is less than the item in the middle of the interval, narrow the interval to the lower half. Otherwise, narrow it to the upper half. Repeatedly check until the value is found or the interval is empty. In the following example we are searching 23 in the array. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(Log n).
We basically ignore half of the elements just after one comparison.
1. Compare x with the middle element.
2. If x matches with the middle element, we return the mid index.
3. Else If x is greater than the mid element, then x can only lie in the right half subarray after the mid element. So we recur for the right half.
4. Else (x is smaller) recur for the left half.
USE C PROGRAMING!!
image text in transcribed
QUESTION We want to code a C program to find a value using binary search method. Binary search method is used as following. First of all, you must sort the elements of arrays and then you should divide the search interval in half. Begin with an interval covering the whole array. If the value of the search key is less than the item in the middle of the interval, narrow the interval to the lower half. Otherwise, narrow it to the upper half. Repeatedly check until the value is found or the interval is empty. In the following example we are searching 23 in the array. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(Log n) Binary Search 0 1 2 3 4 5 6 9 Search 23 2 5 8 12 16 23 38 56 72 91 -un 2 2316 Take 2 hall LO 2 M4 5 6 7 8 HE 12 16 23 38 56 72 91 5 8 NONEN NO NMNMN 23 > 56 lake hall CON 2 4 L5 G Mo7 8 H9 16 23 38 56 72 91 5 8 12 3 4 Found 23. Returns 1 5 2 8 EAS.M5 H6 7 8 9 16 23 38 56 72 91 2 00 12 We basically ignore half of the elements just after one comparison. 1. Compare x with the middle element. 2. If x matches with the middle element, we return the mid index. 3. Else if x is greater than the mid element, then x can only lie in the right half subarray after the mid element. So we recur for the right half. 4. Else (x is smaller) recur for the left half

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

Intelligent Databases Technologies And Applications

Authors: Zongmin Ma

1st Edition

1599041219, 978-1599041216

More Books

Students also viewed these Databases questions

Question

Why do mergers and acquisitions have such an impact on employees?

Answered: 1 week ago

Question

2. Describe the functions of communication

Answered: 1 week ago