answersLogoWhite

0

Algorithm for binary search in c?

Updated: 8/11/2023
User Avatar

Wiki User

11y ago

Best Answer

#include<stdio.h>

#include<conio.h>

void main()

{

int a[6],i,data,pos;

clrscr();

printf("enter the element of the array");

for(i=0;i<6;i++)

{

scanf("%d",&a[i]);

}

printf("enter the data for search");

scanf("%d",&data);

for(i=0;i<6;i++)

{

if(a[i]==data)

{

pos=i+1;

break;} }

if(pos==i+1)

printf("position of the element is %d",pos);

else

printf("this is not element in this array ");

getch();

}

( form giriver bisht (future hacker and networking engineer;; if any problem persist u can call me at this no. .. (dehradun 8439881423 )

User Avatar

Wiki User

12y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

9y ago

#include<iostream>

#include<vector>

// returns the index of the given value, or -1 if it does not exist

int bin_search (int val, std::vector<int>& a, int min, int max)

{

// check range for validity

if (max<min)

return -1;

// locate mid-point of sub-array

int mid = min + (max-min) / 2;

// check for equality

if (a[mid] == val)

return mid;

// recurse with appropriate half of sub-array

return val < a[mid] ? bin_search (val, a, min, mid-1) : bin_search (val, a, mid+1, max);

}

// Initiate a binary search. Returns -1 if the value does not exist, otherwise

// returns the index of the given value.

int bin_search(int val, std::vector<int>& a)

{

return bin_search (val, a, 0, a.size()-1);

}

int main()

{

std::vector<int> a = {0, 2, 4, 6, 8, 10, 12, 14, 16, 18};

for (int val=0; val<20; ++val)

{

int i = bin_search (val, a);

if (i==-1)

std::cout << "The value " << val << " does not exist in the array\n";

else

std::cout << "The value " << val << " exists in the array at index " << i << '\n';

}

std::cout << std::endl;

}

This answer is:
User Avatar

User Avatar

Wiki User

11y ago
  1. get the middle element;
  2. if the middle element equals to the searched value, the algorithm stops;
  3. otherwise, two cases are possible:
    • searched value is less, than the middle element. In this case, go to the step 1 for the part of the array, before middle element.
    • searched value is greater, than the middle element. In this case, go to the step 1 for the part of the array, after middle element.
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Algorithm for binary search in c?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Which search algorithm requires that the arrays contents be sorted?

Binary Search Algorithm


Give you the algorithm of creating a new binary search tree using c?

i want to know how to give the algorithm password in a computer ?


The order of binary search algorithm is?

n log n - G.Parthiban, SRM


How do you print all data in a Binary Search Tree?

By using Depth First Search or Breadth First search Tree traversal algorithm we can print data in Binary search tree.


How can one perform a binary search?

One can perform a binary search easily in many different ways. One can perform a binary search by using an algorithm specifically designed to test the input key value with the value of the middle element.


What is GCD in C Plus Plus?

A C++ implementation of the Binary GCD (Stern's) algorithm is shown in the Related Link below.


Which are the searching algorithm always compare the middle element with the searching elements in the given array?

binary search system


What search algorithm locates an item in an array by repeatedly dividing the array in half?

half means 1/2 from the whole (previous), which means 2 of 1/2, and 2 derived into binary. Ha, Binary Search is the term.


How do you do binary search in file?

A binary search on a random-access file is performed much in the same way as a binary search in memory is performed, with the exception that instead of pointers to items in memory file seek operations are used to locate individual items within the file, then load into memory for further examination. The key aspects of the binary search algorithm do not depend on the specifics of the set of searchable items: the set is expected to be sorted, and it must be possible to determine an order between any two items A and B. Finally, the binary search algorithm requires that the set of searchable items is finite in size, and of a known size.


How do you search a particular element from the vector?

To search a particular element from the vector, use the find() algorithm. If the vector is sorted, you can use the binary_search() algorithm to improve efficiency. Both algorithms can be found in the &lt;algorithm&gt; header in the C++ standard library.


Prove by mathematical induction that the complexity of binary search algorithm is log n?

The complexity of the binary search algorithm is log(n)...If you have n items to search, you iteratively pick the middle item and compare it to the search term. Based on that comparision, you then halve the search space and try again. The number of times that you can halve the search space is the same as log2n. This is why we say that binary search is complexity log(n).We drop the base 2, on the assumption that all methods will have a similar base, and we are really just comparing on the same basis, i.e. apples against apples, so to speak.


Implementation of binary search algorithm using ASM assembler in 8086?

Hi, I hope this is useful http://www.indiastudychannel.com/projects/2748-Assembly-language-program-for-Binary-search.aspx good luck!