Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



What is the binary search in data structure

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 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.

Reply With Quote
  #2  
Old 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.
Reply With Quote
  #3  
Old 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.
Reply With Quote
  #4  
Old 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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "What is the binary search in data structure"
Thread Thread Starter Forum Replies Last Post
How to Upload Binary data using the API? Martineau Software Development 5 16-06-2011 08:44 PM
What is linear search in data structure? Venugopala Software Development 3 04-01-2011 08:11 AM
What is the difference between binary tree sort and heap sort in data structure sRIPRIYA Software Development 3 04-01-2011 01:25 AM
Transferring binary data through PHP Welsh Software Development 5 29-01-2010 05:21 PM
How binary data is passed to HTTP Gannon Software Development 4 29-01-2010 04:37 PM


All times are GMT +5.5. The time now is 09:59 PM.