TechArena Community What is the binary search in data structure

# What is the binary search in data structure

## Software Development

#1
03-01-2011
 Member Join Date: Mar 2010 Posts: 393
What is the binary search in data structure

Hey friends, I wanted to what is the binary search in data structure. I don?t have any idea about data structure and binary search is in data structure section. I tried hard to study but it went above my head. Is it a type of sorting? Can anyone please let me know what the basic concept of searching in data structure is? If anyone having any information or knowledge then please let me know as soon as possible.

#2
03-01-2011
 Member Join Date: May 2009 Posts: 503
Re: What is the binary search in data structure

This method is very fast and efficient. This method requires that the list of the elements be in sorted order. In this method to search an element we compare it with the element present at the center of the list. If it matches then the search is successful. Otherwise the list is divided into two halves. One from 0th element to the center element (first half) and another from center element to last element(second half).As a result all the element in the first half are smaller than the center element whereas all the element in the second half are greater than the center element.
#3
03-01-2011
 Member Join Date: Apr 2009 Posts: 484
Re: What is the binary search in data structure

The searching will now proceed in either of the two halves depending upon whether the element is greater or smaller than the center element. If the element is smaller than the center element than the searching will be done in the first half, otherwise in second half. Same process of comparing the required element with the center element and if not found then dividing the element into two halves is repeated for the first half or second half. This process is repeated till the element is found or the division of half parts gives one element.
#4
03-01-2011
 Member Join Date: May 2009 Posts: 523
Re: What is the binary search in data structure

Code:
```   #include <stdio.h>
#include <conio.h>

void main( )
{
int arr[10] = { 1, 2, 3, 9, 11, 13, 17, 25, 57, 90 } ;
int mid, lower = 0 , upper = 9, num, flag = 1 ;
clrscr( ) ;
printf ( "Enter number to search: " ) ;
scanf ( "%d", &num ) ;
for ( mid = ( lower + upper ) / 2 ; lower <= upper ;mid = ( lower + upper ) / 2 )
{
if ( arr[mid] == num )
{
printf ( "The number is at position %d in the array.", mid ) ;
flag = 0 ;
break ;
}
if ( arr[mid] > num )
upper = mid - 1 ;
else
lower = mid + 1 ;
}

if ( flag )
printf ( "Element is not present in the array." ) ;
getch( ) ;}```
This is the perfect example of binary search in data structure. The above program is written in C programming language. It is showed in the program that how the array is declared and then how it is stored and finally searched with binary search. I think that when you refer this program and execute it you will definitely came to know the concept of binary search.

 Tags: