Question

PLEASE GIVE BRIEF EXPLANATIONS AND EXAMPLE CODES ON BINARY AND TARGET SEARCH AND HOW THIS COULD...

PLEASE GIVE BRIEF EXPLANATIONS AND EXAMPLE CODES ON BINARY AND TARGET SEARCH AND HOW THIS COULD BE APPLIED ON THE RF CIRCUIT

0 0
Add a comment Improve this question Transcribed image text
Answer #1

A binary search is a simplistic algorithm intended for finding the location of an item stored in a sorted list. There are a few variations to the binary search in C program, such as testing for equality and less-than at each step of the algorithm. Binary search in C is an example of a simple process that can be used to dissolve complex problems.

Working process:

Binary search algorithm applies to a sorted array for searching an element. The search starts with comparing target element with the middle element of the array. If value matches then the position of the element is returned.

In case target element is less than the middle element(considering the array follows an ascending order) of the array then the second half of the array is discarded and the search continues by dividing the first half.

The process is the same when the target element is greater than the middle element, only in this case the first half of the array is discarded before continuing with search. The iteration repeats until a match for the target element is found.

Code for Binary search in C:

#include<stdio.h>

int main()

{

int c, first, last, middle, n, search, array[100];

printf ("Enter the number of elements: \n");

scanf ("%d", &n);

printf ("Enter %d integers:\n", n);

for (c=0; c<n; c++)

scanf ("%d", &array[c]);

printf ("Enter the value to find: \n");

scanf ("%d", &search);

first = 0;

last = n-1;

middle = (first+last)/2;

while (first <= last) {

if (array[middle] < search)

first = middle+1;

else if (array[middle] == search) {

printf ("%d is present at index %d.\n", search, middle+1);

break;

}

else

last = middle-1;

middle = (first+last)/2;

}

if (first > last)

printf("Not found! %d is not present in the list.\n", search);

return 0;

}

Sample output:

5

Enter 5 integers:

1

9

22

24

46

Enter the value to find:

24

24 is present at index 4.

Add a comment
Know the answer?
Add Answer to:
PLEASE GIVE BRIEF EXPLANATIONS AND EXAMPLE CODES ON BINARY AND TARGET SEARCH AND HOW THIS COULD...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT