Computer Science‎ > ‎

Arrays and Searching: Binary Search ( with C Program source code)




To go through the C program / source-code, scroll down to the end of this page

Binary Search Algorithm


Search as the name suggests, is an operation of finding an item from the given collection of items. Binary Search algorithm is used to find the position of a specified value (an ‘Input Key’) given by the user in a sorted list.

Algorithm:


It starts with the middle element of the list.

1. If the middle element of the list is equal to the ‘input key’ then we have found the position the specified value.
2. Else if the ‘input key’ is greater than the middle element then the ‘input key’ has to be present in the last half of the list.
3. Or if the ‘input key’ is lesser than the middle element then the ‘input key’ has to be present in the first half of the list.

Hence, the search list gets reduced by half after each iteration.

First, the list has to be sorted in non-decreasing order.

[low, high] denotes the range in which element has to be present and [mid] denotes the middle element. Initially low = 0, high = number_of_elements and mid = floor((low+high)/2). In every iteration we reduce the range by doing the following until low is less than or equal to high(meaning elements are left in the array) or the position of the ‘input key’ has been found.

(i) If the middle element (mid) is less than key then key has to present in range [mid+1 , high], so low=mid+1, high remains unchanged and mid is adjusted accordingly
(ii) If middle element is the key, then we are done.
(iii) If the middle element is greater than key then key has to be present in the range [low,mid-1], so high=mid-1, low remains unchanged and mid is adjusted accordingly.

Properties:


1. Best Case performance – The middle element is equal to the ‘input key’ O(1).
2. Worst Case performance - The ‘input key’ is not present in the list O(logn).
3. Average Case performance – The ‘input key’ is present, but it’s not the middle element
O(logn).
4. The List should be sorted for using Binary Search Algorithm.
5. It is faster than Linear Search algorithm, and its performance increases in comaparison to
linear search as N grows.

Complete tutorial with an example :



Binary Search - C Program Source Code


#include<stdio.h>
/* Logic: [low, high] denotes the range in which element has to be present.
          Initially low = 0, high  = number_of_elements which covers entire range.
          In every step we reduce the range by doing the following
            (i) If the middle element (mid) is less than key then key has to present in range [mid+1 , high]
           (ii) If middle element is the key, then we are done.
          (iii) If the middle element is greater than key then key has to be present in the range[low,mid-1]
 */

int BinarySearch(int *array, int number_of_elements, int key)
{
        int low = 0, high = number_of_elements-1, mid;
        while(low <= high)
        {
                mid = (low + high)/2;
                if(array[mid] < key)
                {
                        low = mid + 1;
                }
                else if(array[mid] == key)
                {
                        return mid;
                }
                else if(array[mid] > key)
                {
                        high = mid-1;
                }

        }
        return -1;
}
int main()
{
        int number_of_elements;
        scanf("%d",&number_of_elements);
        int array[number_of_elements];
        int iter;
        /*Input has to be sorted in non decreasing order */
        for(iter = 0;iter < number_of_elements;iter++)
        {
                scanf("%d",&array[iter]);
        }
        /* Check if the input array is sorted */
        for(iter = 1;iter < number_of_elements;iter++)
        {
                if(array[iter] < array[iter - 1])
                {
                        printf("Given input is not sorted\n");
                        return 0;
                }
        }
        int key;
        scanf("%d",&key);
        /* Calling this functions searches for the key and returns its index. It returns -1
          if key is not found.*/

        int index;
        index = BinarySearch(array,number_of_elements,key);
        if(index==-1)
        {
                printf("Element not found\n");
        }
        else
        {

                printf("Element is at index %d\n",index);
        }
        return 0;
}

Related Tutorials :

Bubble Sort    

One of the most elementary sorting algorithms to implement - and also very inefficient. Runs in quadratic time. A good starting point to understand sorting in general, before moving on to more advanced techniques and algorithms. A general idea of how the algorithm works and a the code for a C program.

Insertion Sort

Another quadratic time sorting algorithm - an example of dynamic programming. An explanation and step through of how the algorithm works, as well as the source code for a C program which performs insertion sort. 

Selection Sort

Another quadratic time sorting algorithm - an example of a greedy algorithm. An explanation and step through of how the algorithm works, as well as the source code for a C program which performs selection sort. 

 Shell Sort

An inefficient but interesting algorithm, the complexity of which is not exactly known.

Merge Sort 

An example of a Divide and Conquer algorithm. Works in O(n log n) time. The memory complexity for this is a bit of a disadvantage.

Quick Sort

In the average case, this works in O(n log n) time. No additional memory overhead - so this is better than merge sort in this regard. A partition element is selected, the array is restructured such that all elements greater or less than the partition are on opposite sides of the partition. These two parts of the array are then sorted recursively.

Heap Sort

 Efficient sorting algorithm which runs in O(n log n) time. Uses the Heap data structure. 

Binary Search Algorithm

 Commonly used algorithm used to find the position of an element in a sorted array. Runs in O(log n) time. 



Testing Zone For Programmers- 

 

Try out our online Multiple-Choice-Question tests in Programming and Computer Science!


 

Photo-credits: www.istockphoto.com

 
 Quizzes on Basic Object Oriented Programming with C++   Quizzes on Java Programming
  
 Quizzes on C Programming- Arrays, Strings and Pointers
  1. C Programming MCQ Quiz #1:  Strings- 1
  2. C Programming MCQ Quiz #2: Strings (2)
  3. C Programming MCQ Quiz #3: Strings (3)
  4. C Programming MCQ Quiz #4: Arrays(1)
  5. C Programming MCQ Quiz #5: Arrays (2)
  6. C Programming MCQ Quiz #6: Arrays (3)
  7. C Programming MCQ Quiz #7: Pointers (1)
  8. C Programming MCQ Quiz #8: Pointers (2)
 Quizzes on Data Structures, Algorithms and Complexity
  1. MCQ Quiz #1: The Basics of Sorting Algorithms- Quadratic Sorts
  2. MCQ Quiz #2: Efficient Sorting Algorithms- Quick sort, Merge Sort, Heap Sort
  3. MCQ Quiz #3- The Radix Sort
  4. MCQ Quiz #4: Divide and Conquer Techniques- Binary Search, Quicksort, Merge sort, Complexities
  5. MCQ Quiz #5- Dynamic Programming
  6. MCQ Quiz #6- Complexity of Algorithms
  7. MCQ Quiz #7- Application of Master's Theorem
  8. MCQ Quiz #8: Binary Search Trees
  9. MCQ Quiz #9: B-Trees
  10. 10 MCQ Quiz #9: AVL-Trees
  11. 11 MCQ Quiz #10: Representing Graphs as Data Structures
  12. 12 MCQ Quiz #11: Spanning Trees
  13. 13 MCQ Quiz #12: Algorithms - Graphs: Spanning Trees - Kruskal and Prim Algorithms
  14. 14 MCQ Quiz #13: Algorithms - Graphs: Depth and Breadth First Search


Recommended books for learning Computer Science, learning high quality programming, and preparing for programming interviews:

These are the standard sources of the knowledge expected from candidates interviewing at Google, Microsoft, Facebook, Amazon and other startups and top-tier technology companies. The books by Cormen or Sedgewick (a standard part of the undergraduate curriculum) are sufficient for this part of the preparation. Google specially, loves to focus on algorithmic questions.


 1. A good knowledge of Data Structures, Algorithm Design and Analysis of Complexity
The books by Cormen or Sedgewick (a standard part of the undergraduate curriculum) are sufficient for this part of the preparation. Google specially, loves to focus on algorithmic questions.

Introduction to Algorithms 3r...
List Price: Rs.525
Our Price: Rs.357
Buy from FlipKart


   Introduction to Algorithms (CLRS) 



Algorithms 4th Edition
Our Price: Rs.599
Buy from FlipKart


 Algorithms (By Robert Sedgewick) 


An Introduction to the Analys...
List Price: Rs.550
Our Price: Rs.500
Buy from FlipKart


Analysis of Algorithms (By Robert Sedgewick) 

 
 2. Standard and established sources of Programming Interview Questions
A lot of interview questions are re-used and re-cycled by interviews again and again. Chances are, that question you're going to be asked, has been asked a hundred times during the last week, across a number of companies!
Google, Facebook, Microsoft, Amazon- all of them frequently ask questions on the lines of those in these books.

 
Cracking The Coding Interview...
List Price: Rs.2293
Our Price: Rs.2178
Buy from FlipKart

Cracking the Coding Interview: 150 Programming Questions and Solutions [Paperback] 

 
Programming Interviews Exposed
List Price: Rs.299
Our Price: Rs.284
Buy from FlipKart

Programming Interviews Exposed 
 
 
Ace the Programming Interview
Our Price: Rs.1733
Buy from FlipKart





 
Ace the Programming Interview 
 
 
Ace the IT Interview
Our Price: Rs.1300
Buy from FlipKart

 

Ace the IT Interview 

 3. Puzzles and Brainteasers (often asked, good to be prepared)
Questions from Mount Fuji are specially popular with Microsoft recruiters.

 
How Would You Move Mount Fuji?
List Price: Rs.860
Our Price: Rs.800
Buy from FlipKart


How would you move Mount Fuji? 
 
How to Ace the Brain Teaser Interview 

  
 4. Good Software Engineering practices. 
Writing clean and secure code, an awareness about Design Patterns and Object Oriented Programming techniques. Some of these books are frequently recommended by Microsoft recruiters. Take care of buffer overflows!
Code Complete is a recommended book for Microsoft Interviews.

Code Complete
List Price: Rs.629
Our Price: Rs.611
Buy from FlipKart
 

Code Complete 2 

Writing Secure Code 2nd Edition
Our Price: Rs.699
Buy from FlipKart

 Writing Secure Code

 
Design Patterns
List Price: Rs.499
Our Price: Rs.409
Buy from FlipKart

 

Design Patterns 

 
 In case your primary language is C++, make sure you know its "ins-and-outs". You should know about destructors and constructors, virtual destructors, polymorphism, function overloading. Some of these books might help you revise C++. 
The C++ Programming Language...
List Price: Rs.775
Our Price: Rs.736
Buy from FlipKart

The C++ Programming Language (Bjarne Stroustrup)

Beginning C++ Through Game Pr...
Our Price: Rs.2008
Buy from FlipKart

 Beginning C++ through Game Programming 

 
C++ How to Program
List Price: Rs.695
Our Price: Rs.632
Buy from FlipKart

 
C++: How to Program (Deitel and Deitel)
 
 In case your primary language is C, make sure to be well versed with pointers, system calls and how they work. How does Malloc work, how does calloc work? All of these are questions which some interviewers could potentially ask, to test the depth of your knowledge and your understanding of the system "under the hood". 
The C Programming Language
List Price: Rs.175
Our Price: Rs.137
Buy from FlipKart


The C Programming Language (Kernighan)
 
C How To Program 6th  Edition
List Price: Rs.650
Our Price: Rs.624
Buy from FlipKart


C How to Program (Deitel and Deitel)

  
Here's a great book for the Java lovers as well.
It normally doesn't matter which programming language you know, but make sure to know about it in detail.
Java
List Price: Rs.595
Our Price: Rs.541
Buy from FlipKart

 
Java: A Beginner's Guide (Herb Schildt)