Data Structures Tutorial

Data Structures Tutorial Asymptotic Notation Structure and Union Array Data Structure Linked list Data Structure Type of Linked list Advantages and Disadvantages of linked list Queue Data Structure Implementation of Queue Stack Data Structure Implementation of Stack Sorting Insertion sort Quick sort Selection sort Heap sort Merge sort Bucket sort Count sort Radix sort Shell sort Tree Traversal of the binary tree Binary search tree Graph Spanning tree Linear Search Binary Search Hashing Collision Resolution Techniques

Misc Topic:

Priority Queue in Data Structure Deque in Data Structure Difference Between Linear And Non Linear Data Structures Queue Operations In Data Structure About Data Structures Data Structures Algorithms Types of Data Structures Big O Notations Introduction to Arrays Introduction to 1D-Arrays Operations on 1D-Arrays Introduction to 2D-Arrays Operations on 2D-Arrays Strings in Data Structures String Operations Application of 2D array Bubble Sort Insertion Sort Sorting Algorithms What is DFS Algorithm What Is Graph Data Structure What is the difference between Tree and Graph What is the difference between DFS and BFS Bucket Sort Dijkstra’s vs Bellman-Ford Algorithm Linear Queue Data Structure in C Stack Using Array Stack Using Linked List Recursion in Fibonacci Stack vs Array What is Skewed Binary Tree Primitive Data Structure in C Dynamic memory allocation of structure in C Application of Stack in Data Structures Binary Tree in Data Structures Heap Data Structure Recursion - Factorial and Fibonacci What is B tree what is B+ tree Huffman tree in Data Structures Insertion Sort vs Bubble Sort Adding one to the number represented an array of digits Bitwise Operators and their Important Tricks Blowfish algorithm Bubble Sort vs Selection Sort Hashing and its Applications Heap Sort vs Merge Sort Insertion Sort vs Selection Sort Merge Conflicts and ways to handle them Difference between Stack and Queue AVL tree in data structure c++ Bubble sort algorithm using Javascript Buffer overflow attack with examples Find out the area between two concentric circles Lowest common ancestor in a binary search tree Number of visible boxes putting one inside another Program to calculate the area of the circumcircle of an equilateral triangle Red-black Tree in Data Structures Strictly binary tree in Data Structures 2-3 Trees and Basic Operations on them Asynchronous advantage actor-critic (A3C) Algorithm Bubble Sort vs Heap Sort Digital Search Tree in Data Structures Minimum Spanning Tree Permutation Sort or Bogo Sort Quick Sort vs Merge Sort

Binary Search

In this searching technique, the given element is compared with the middle element of the list.

  • If both elements are equal, it returns the index value.
  • If both elements are not equal, we check whether the given element is larger or smaller than the middle element.
  • If the given element is smaller than the middle element, we will repeat the same process in the smaller part of the list.
  • If the given element is larger than the middle element, we will repeat the same process in the larger part of the list.
ComplexityBest caseAverage caseWorst case
TimeO(1)O(log n)O(log n)
Space  O(1)
Binary_Search( )
Step 1: SET BEG = lower_bound, END = upper_bound, FLAG = - 1
Step 2: Repeat Steps 3 and 4 while BEG <=END
Step 3: SET MID = (BEG + END)/2
Step 4: if A[MID] = VAL
             SET FLAG = MID
             print “index value of the element”
             Go to Step 6
             else if A[MID] > VAL
             else END = MID - 1
             else
             SET BEG = MID + 1
             // end of if
             // end of loop
Step 5: if FLAG = -1
             print " element is not found in this list "
             // end of if
Step 6: exit  

For example, suppose you have the following array, and you have to search 30 in it.

Iteration 1: BEG = 0           
          END = 8       
              MID = (BEG + END) / 2            
                     (0 + 8) / 2 = 4       
              A[MID] = VAL      
               A[4] = 30      
               35 ? 30         // not equal       
Iteration 2: BEG = 0                     END = MID – 1                              = 4 – 1 = 3                     MID = (0 + 3) / 2                              = 1                     A[1] = VAL                     27 ? 30         // not equal  
Iteration 3: BEG = MID + 1                              = 1 + 1                              = 2                     END = 3                     MID = (2 + 3) / 2                              = 2                      A[2] = VAL                      29 ? 30         // not equal
Iteration 4: BEG = MID + 1                              = 2 + 1                              = 3                     END = 3                     MID = (3 + 3) / 2                              = 6                     A(3) = VAL                       30 = 30            // equal                                       
Index value of 32 is 3.

Binary search program in C language  

#include<stdio.h>
   int binarySearch(int[], int, int, int);   
void main () 
  {     
 int arr[10] = {6,  9, 12, 23, 35, 46, 68, 90, 96, 120};  
     int item, location=-1;     
   printf("enter the search element ");    
   scanf("%d",&item);   
    location = binarySearch(arr, 0, 9, item); 
      if(location != -1)     
   {     
      printf("element index value is %d",location);  
     }      
 else      
 {         
  printf("element not found"); 
      }  
 }  
  int binarySearch(int a[], int beg, int end, int item) 
  {   
    int mid;   
    if(end >= beg)   
     {          
    mid = (beg + end)/2;  
         if(a[mid] == item)
           {         
      return mid+1;  
         }         
  else if(a[mid] < item)   
         {         
      return binarySearch(a,mid+1,end,item);   
        }      
    else       
     {         
      return binarySearch(a,beg,mid-1,item);  
         }         
    }    
   return -1; 
   }    

Output

enter the search element
35
element index value is 4



ADVERTISEMENT
ADVERTISEMENT