Home » Code Snippets » C/C++ Data Structure programs

Binary Searching Data Structure Example in C - C program to find an element using Binary Searching from an Array.

Binary Searching is a half – interval Searching Algorithm in which a element can be searched in sorted array with minimum number of comparisons, in this algorithm key element compares with the middle index item.

Binary Searching Implementation using C program



/*
    program to implement Binary Searching,
    to find an element in array.
*/
 
#include <stdio.h>
 
/*  function    : BinaryrSearch() */
  
int BinaryrSearch(int x[],int n,int item) 
{ 
    int L=0;        /*LOWER LIMIT   */
    int U=n-1;      /*UPPER LIMIT   */
    int mid;        /*MIDDLE INDEX  */
  
    while( L< U) 
    { 
        mid=(L+U)/2; 
        if(x[mid]==item)     return mid; 
        else if(x[mid] < item)    L=mid+1; 
        else if(x[mid] > item)    U=mid-1; 
    } 
    return -1; 
} 
  
int main() 
{ 
    int num=0,pos=0; 
     
    int ar[10],i; 
  
    printf("\nEnter array elements (in ASC Order...):\n"); 
    for(i=0;i<10;i++) 
    { 
        printf("Enter element %02d :",i+1); 
        scanf("%d",&ar[i]); 
    } 
  
    printf("\nEnter element to be searched :"); 
    scanf("%d",&num); 
  
    pos=BinaryrSearch(ar,10,num); 
    if(pos!=-1) { printf("\nItem found @ %02d location.\n",pos);} 
    else        { printf("\nItem not found.\n"); } 
    printf("\n"); 
    return 0;
}

Output

    Enter array elements (in ASC Order...):
    Enter element 01 :10                                                                                                                                                            
    Enter element 02 :20                                                                                                                                                            
    Enter element 03 :30                                                                                                                                                            
    Enter element 04 :40                                                                                                                                                            
    Enter element 05 :50                                                                                                                                                            
    Enter element 06 :60                                                                                                                                                            
    Enter element 07 :70                                                                                                                                                            
    Enter element 08 :80                                                                                                                                                            
    Enter element 09 :90                                                                                                                                                            
    Enter element 10 :100                                                                                                                                                           
                                                                                                                                                                                
    Enter element to be searched :80                                                                                                                                                
                                                                                                                                                                                
    Item found @ 07 location. 





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.