Difference between revisions of "Binary Search"

From Embedded Systems Learning Academy
Jump to: navigation, search
(Binary Search)
Line 22: Line 22:
 
int first = 0, last = size - 1, middle = (first+last)/2;
 
int first = 0, last = size - 1, middle = (first+last)/2;
  
   while (first <= last) //looping over the array to find the matching element till all elements in each half are compared
+
   while (first <= last) //looping over the array to find the matching element till all elements in each half are compared
 
   {
 
   {
       if (array[middle] < key) //if element is lesser than the key value
+
       if (array[middle] < key) //if element is lesser than the key value
 
       first = middle + 1;
 
       first = middle + 1;
 
       else if (array[middle] == key) { // if matched with key
 
       else if (array[middle] == key) { // if matched with key
Line 32: Line 32:
 
       else
 
       else
 
       {
 
       {
       last = middle - 1; // reducing the size of the halves
+
       last = middle - 1; // reducing the size of the halves
 
       }
 
       }
       middle = (first + last)/2; //re-calculating the middle value after each iteration
+
       middle = (first + last)/2; //re-calculating the middle value after each iteration
 
     }
 
     }
 
   return 0;
 
   return 0;
Line 46: Line 46:
  
 
   printf("Enter number of elements\n");
 
   printf("Enter number of elements\n");
   scanf("%d",&n); // defining the array size
+
   scanf("%d",&n); // defining the array size
  
 
   int array[n];
 
   int array[n];
Line 52: Line 52:
 
   for (int i = 0; i < n; i++)
 
   for (int i = 0; i < n; i++)
 
   {
 
   {
  scanf("%d",&array[i]); // Setting the array elements
+
  scanf("%d",&array[i]); // Setting the array elements
 
   }
 
   }
  
 
   printf("Enter value to find\n");
 
   printf("Enter value to find\n");
   scanf("%d", &key); // Keying in the value which has to be searched
+
   scanf("%d", &key); // Keying in the value which has to be searched
  
   pos= bin_search(array,n,key); // Calling the Binary search function
+
   pos= bin_search(array,n,key); // Calling the Binary search function
 
   if(pos==0)
 
   if(pos==0)
 
   {
 
   {

Revision as of 23:43, 2 December 2016

Binary Search

The Drawback of the Linear search was that, it was not efficient enough when the data set was huge. This Drawback is eliminated when Binary Search algorithm is used. But the drawback of this algorithm is that the data set has to be sorted. In this Algorithm the data set would be divided into 2 equal parts in every iteration till the required element is found or till the case when further equal division is not possible. This algorithm is similar to searching for a particular page in a book. Say for example a book is having 20 pages and 8th page is to be found. Now equal halves of 20 page book is 0-10 pages block and 11-20 pages block. So 8th page falls under 0-10 page block. This continues for few more iterations, till 8th page is reached.

This method needs [log2n]+1 comparisons, i.e O(log n) in Big O notation, i.e. for Thousands of records, it needs about five to six comparisons, which is way better than Linear search.

Below Image shows the formation of the algorithm in binary tree structure.

Binary Search Tree


Code Snippet


#include <stdio.h>

/*Binary search function to search for a key in the List*/
int bin_search(int array[],int size,int key)
{
	int first = 0, last = size - 1, middle = (first+last)/2;

   while (first <= last) 						//looping over the array to find the matching element till all elements in each half are compared
   {
      if (array[middle] < key)						//if element is lesser than the key value
    	  first = middle + 1;
      else if (array[middle] == key) {					// if matched with key
    	  return middle+1;
         break;
      }
      else
      {
    	  last = middle - 1;						// reducing the size of the halves
      }
      	 middle = (first + last)/2;					//re-calculating the middle value after each iteration
   	   }
   return 0;
   }



int main()
{
   int n, key, pos;

   printf("Enter number of elements\n");
   scanf("%d",&n);							// defining the array size

   int array[n];
   printf("Enter %d integers\n", n);
   for (int i = 0; i < n; i++)
   {
	   scanf("%d",&array[i]);					// Setting the array elements
   }

   printf("Enter value to find\n");
   scanf("%d", &key);							// Keying in the value which has to be searched

   pos= bin_search(array,n,key);					// Calling the Binary search function
   if(pos==0)
   {
	   printf("Search unsuccessful, %d not found.\n", key);	// If position of the searching stays at 0 element or beginning
   }
   else
   {
	   printf("%d found at location %d.\n", key, pos);		//Printing the value fetched
   }
   return 0;
}