Skip to main content

Binary Search

/**
    Program Name: Binary Search

    Description: This Program Searches for an Item in an integer Array

    Author:  Tauqirul Haque
       
*/


   
#define SIZE 10

void displayElement(int data[SIZE]);
void binarySearch(int data[], int);


void displayElement(int data[SIZE])
{
    int i;
   
    for(i=0; i
    {
        printf("\n Data[%d] = %d ", i+1, data[i]);
    }
   
    printf("\n");
}



void main()
{
    int data[SIZE] = {12,24,56,58,78,90,91,99,100,107};
   
    int item;
    int choice;
   
    while(choice != 3)
    {
        printf("\n\t\t 1. Search an Item ");
        printf("\n\t\t 2. Display The Elements ");
        printf("\n\t\t 3. Quit the Program");
        printf("\n\n\t\t Enter Your Choice #  ");
       
        scanf("%d",&choice);
       
        switch(choice)
        {
            case 1:
                        printf("\nEnter the Element you want to Search #  ");
                        scanf("%d",&item);
   
                           binarySearch(data,item);
                           break;
             case 2:
                         displayElement(data);
                         break;
            case 3:

                        printf("Program Terminated ... \n");
                        getch();
                        exit(0);
        }
    }
}

void binarySearch(int data[SIZE], int item)
{
    int beg=0;
    int end=SIZE-1;
    int mid = (beg+end)/2;
   
    while(beg <= end && data[mid] != item)
    {
        if(item < data[mid])
        {
            end = mid -1 ;
        }
        else
        {
            beg = mid + 1;
        }
       
        mid = (beg+end)/2;
    }
   
    if(item == data[mid])
    {
        printf("\nSearch SUCCESSFULL #  Item Location = %d \n", mid+1);
    }
    else
    {
        printf("\nSearch UNSUCCESSFULL # Item not in the List ...\n");
    }
}
       

Comments

Popular posts from this blog

Singly Linked List

/**     Program Name: Singly Linked List     Description: This Program is for Implemeting Singly Linked List     Author:  Tauqirul Haque         */ struct Linklist {     int item;     struct Linklist *next; }; typedef struct Linklist node; void insertAtBeginning(node **); void createAppendNode(node **); void insertInMiddle(node **); void deleteNode(node **); void displayNode(node **); void countNode(node **);  void searchElement(node **); void reverse(node **); void main() {     node *head = NULL;     int choice = 0;     while(choice != 9)     {             printf("\n\t\t\t1. Add Node At The Beginning ");     printf("\n\t\t\t2. Insert Element in the Middle ");     printf("\n\t\t\t3. Append New Node "); ...

BUBBLE SORT

/**     Program Name: BUBBLE SORT     Description: This Program sorts a Given Array in Ascending Order     Author:  Tauqirul Haque         */ #define SIZE 10 void bubbleSort(int data[SIZE]); void displayElement(int data[SIZE]); void main() {     int data[SIZE] = { 45,34,66,89,45,34,76,22,79, 12};         printf("\nThe Original Array Before Sorting ... ");     displayElement(data);         printf("\nArray After Sorting ... ");     bubbleSort(data);     displayElement(data);     } void displayElement(int data[SIZE]) {     int i;     for(i=0; i     {         printf("\nData[%d]  =  %d",i,data[i]);     }     printf("\n"); } void ...

FINDING FACTORIAL OF A NUMBER

/**     Program Name: FINDING FACTORIAL     Description: This Program finds a Factorial of a Given Number     Author:  Tauqirul Haque         */ long fact(long n); void main() {     long n;     printf("Enter N : ");     scanf("%d",&n);         if(n < 0)   //if its a neg number then changing the sign     {         n = -n;     }         printf("The fact of %ld  =  %ld \n\n",n,fact(n)); } long fact(long n) {     if(n==0 || n==1)     {         return 1;     }     else     {         return n*fact(n-1);     } }