Monday, January 7, 2013

Data structure Lab Programming

 

 

                                 /* Stack Operation */

 

 

#include<stdio.h>

#include<conio.h>

#define MAX 10

 

void push(int);

void peek();

int pop();

int top=-1;

int stack[MAX];

 

void main()

{

       int i,n,choise,s;

       clrscr();

while(1)

{

      clrscr();

      printf("\t\t\t\t STACK OPERATION\n");

      printf(“\t\t\t\t~~~~~~~~~~~~~~~~~~~\n”);

      printf("\t\tEnter what you do you want to do??");

      printf("\n\tEnter 1 to push\n\tEnter 2 to pop");

      printf("\n\tEnter 3 to stack view \n\tEnter 4 to peek");

      printf("\n\tEnter 5 to quit \n");

      scanf("%d",&choise);

 

 

switch(choise)

{

  case 1:

             clrscr();

             printf("\n Enter any value to push:");

             scanf("%d",&s);

             push(s);

             printf("\n The stack value is:");

             for(i=0;i<=top;i++)

             printf("%d",stack[i]);

             getch();

             break;

 

 

 

 

case 2:

             clrscr();

             n=pop();

             printf("\n The removed value is: %d",n);

             printf("The stack value is:");

             for(i=0;i<=top;i++)

             printf("%d",stack[i]);

             getch();

             break;

 

 

case 3:

         clrscr();

          printf("The stack value is:");

          for(i=top;i>=0;i--)

         {

          printf("\n ! %3d !\n",stack[i]);

          printf("--------------------");

          }

          gotoxy(11,3);

          printf("<--- Top");

          getch();

          break;

 

 

case 4:

            clrscr();

            peek();

            getch();

            break;

            }

            if(choise==5)

            break;

            }

            }

 

            void peek()

           {

           if(top==-1)

           printf("the top value is:null");

           else

           printf("the top value is:%d",top);

           }

 

 

 

void push(int a)

{

if(top==MAX)

printf(" the stack is full");

else

{

top++;

stack[top]=a;

}

}

 

 

 

int pop()

{

int n;

if(top==-1)

{

printf("the stack is empty:");

return(0);

}

else

{

n=stack[top];

top--;

return(n);

}

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

OUTPUT:

                                 STACK OPERATION

                                 ~~~~~~~~~~~~~~~

                Enter what you do you want to do??

        Enter 1 to push

        Enter 2 to pop

        Enter 3 to stack view

        Enter 4 to peek

        Enter 5 to quit

1

Enter any value to push:78

The stack value is:65  89      78

 

                                 STACK OPERATION

                                 ~~~~~~~~~~~~~~~

                Enter what you do you want to do??

        Enter 1 to push

        Enter 2 to pop

        Enter 3 to stack view

        Enter 4 to peek

        Enter 5 to quit

3

The stack value is:

!  78 !  <--- Top

--------------------

!  89 !

--------------------

!  65 !

--------------------

                                 STACK OPERATION

                                 ~~~~~~~~~~~~~~~

                Enter what you do you want to do??

        Enter 1 to push

        Enter 2 to pop

        Enter 3 to stack view

        Enter 4 to peek

        Enter 5 to quit

4

The top value is:2

                                 STACK OPERATION

                                 ~~~~~~~~~~~~~~~

                Enter what you do you want to do??

        Enter 1 to push

        Enter 2 to pop

        Enter 3 to stack view

        Enter 4 to peek

        Enter 5 to quit

2

The removed value is: 78

The stack value is:65  89

 


 

/*  SINGLY LINKED LIST*/

 

#include<stdio.h>

#include<conio.h>

#include<malloc.h>

 

struct node

{

        int a;

        struct node *next;

};

 

struct node *create()

{

          struct node *p,*r,*n;

           int s,k;

          s=sizeof(struct node);

          printf("\n\t\t\tLINKED LIST\n");

          printf("\n\t\t\t-----------\n");

          printf("\n\n\tRead the element until -1 to exit:\n");

          scanf("%d",&k);

          p=r=NULL;

  

while(k!=-1)

   {

     n=(struct node *)(malloc(s));

     n->a=k;

     n->next=NULL;

     if(r==NULL)

       r=n;

else

       p->next=n;

       p=n;

       scanf("%d",&k);

   }

   return(r);

  }

 

 

  void display(r)

  struct node *r;

  {

     while(r!=NULL)

     {

     printf("\t%d",r->a);

     r=r->next;

 

     }

 

  return;

}

 

  void *insert(r)

   struct node **r;

   {

     struct node *temp,*z;

     int t,q,i;

     printf("\n\n\tRead the value and position to insert: \n\t");

     scanf("%d %d",&t,&q);

     temp=(struct node *)(malloc(sizeof(struct node)));

     temp->a=t;

     if((q==1)  || ((*r)==NULL))

       {

       temp->next=*r;

       *r=temp;

       }

     else

       {

       z=*r;

       i=2;

     while((i<q)&&(z->next !=NULL))

       {

            z=z->next;

            ++i;

       }

     temp->next=z->next;

     z->next=temp;

       }

   return(r);

   }

 

   void delete(r)

   struct node **r;

     {

            struct node *t;

            int v,p,i;

            printf("\n\n\t Enter the element to delete of its position \n\t");

            scanf("%d",&p);

            if(*r !=NULL)

              {

              if(p==1)

               *r=(*r)->next;

            else

              {

              t=*r;

              i=2;

              while(t!= NULL && i<p)

               {

                t=t->next;

                ++i;

               }

            if(t!=NULL)

              t->next=t->next->next;

            else

              printf("\n\t\tCould not delete\n");

       }

       }

   }

 

   void main()

     {

       int c=0;

       struct node *r;

       clrscr();

       while(c==0)

            {

            clrscr();

            printf("\n\t\t Linked List");

            printf("\n\t\t -----------");

            r=create();

            printf("\n \t List of Elements: \n");

            display(r);

            insert(&r);

            printf("\n\t After inserting elements are :\n");

            display(r);

            delete(&r);

            printf("\n\t After deleting :\n");

            display(r);

            printf("\n\t Do you want to continue yes(0) / No(1-9):\n\t");

            scanf("%d",&c);

            }

       getch();

     }

 

 

 

 

 

 

OUTPUT:-

 

                                                SINGLY LINKED LIST

                                                 - -------------------------------

 

Read the element until -1 to exit:

56    67  6  87  21  -1

 

 

List of Elements:

56              67        6          87        21

 

 

Read the value and position to insert:

90

 

4

 

After inserting elements are:

 

56      67     6     90     87     21

 

Enter the element to delete of its position:

3

 

After deleting:

56     67     90     87     21

 

Do you want to continue Yes (0) / No (1-9):   2

 

 

 


 

 

/* SHORTEST PATH */

 

 

#include<stdio.h>

#include<conio.h>

 

void shortestpath(int,int);

int graph,costs=32760,cost1,num,sor,dest,edge;

int from[20],to[20],cost[20],path[10];

 

void shortestpath(int graph,int z)

{

        int temp,k=0,i=-1,p[10];

        for(temp=z;temp<edge;temp++)

{

if(graph==from[temp])

{

        p[k]=graph;

        i++;

if(to[temp]==dest)

{

        cost1=cost1+cost[temp];

if(cost1<costs)

{

        costs=cost1;

        for(z=0;z<=i;z++)

        path[z]=p[z];

        num=temp;

        break;

     }

  }

else

{

         graph=to[temp];

         cost1=cost1+cost[temp];

         k++;

      }

    }

  }

}

 

 

 

 

 

 

void main()

{

        int i,z,cost2;

        clrscr();

        printf("\n\t\tSHORTEST PATH \n");

        printf("\n\t\t~~~~~~~~~~~~~~~");

        printf("\nEnter the number of edges:");

        scanf("%d",&edge);

        printf("\nEnter the source");

        scanf("%d",&sor);

        printf("\nEnter the destination");

        scanf("%d",&dest);

        printf("Source \t\t Destination \t\t Cost \n");

        fflush(stdin);

        for(i=0;i<edge;i++)

      {

         printf(" From:");

         scanf("%d",&from[i]);

         printf("\t To :");

         scanf("%d",&to[i]);

         printf("\t Cost:");

         scanf("%d",&cost[i]);

}

 

for(i=0;i<edge;i++)

{

         if(sor==from[i]);

{

         graph=to[i];

        cost2=cost[i];

 

for(z=i;z<edge;z++)

{

if(graph==from[z])

{

         cost1=cost2;

         shortestpath(graph,z);

}

 

 

 

 

 

 

 

 

 

 

 

else if(graph==dest)

{

         cost1=cost2;

if(cost1<costs)

{

         costs=cost1;

         num=0;

         break;

        }

      }

    }

  }

}

          printf("Cost:%d,Path:%d",costs,sor);

          for(z=0;z<num;z++)

         printf("-> %d",path[z]);

         printf("-> %d",dest);

         getch();

}

 

 

        

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

OUTPUT:-

 

       SHORTEST PATH

 

                Enter the number of edges:3

 

Enter the source1

 

Enter the destination3

Source           Destination             Cost

From:1

         To :2

                 Cost:7

From:1

         To :5

                 Cost:7

From:1

         To :3

                 Cost:78

Cost:78,Path:1-> 3

 

 



 

/*Quick Sort*/

 

 

#include<stdio.h>

#include<conio.h>

 

void main()

{

 

  int i,a[50],n,temp;

  clrscr();

  printf("\n\t Quick Sort \n");

  printf("\t   ~~~~~~~~~~\n");

  printf("Enter the no of elements in the array: ");

  scanf("%d",&n);

  for(i=0;i<n;i++)

{

          temp=i;

          printf("\n Enter the %d element:",++temp);

          scanf("%d",&a[i]);

  }

 

  quicksort(a,n);

  printf("\n The sorted list is:");

  for(i=0;i<n;i++)

  printf("\t%d",a[i]);

  getch();

  }

 

 

quicksort(int n[],int a)

  {

          q_sort(n,0,a-1);

           return(0);

   }

 

 

 

q_sort(int n[],int left,int right)

{

           int pivot,l_hold,r_hold;

           l_hold=left;

           r_hold=right;

           pivot= n[left];

 

 

 

          while(left<right)

{

          while((n[right]>=pivot)&&(left<right))

          right--;

          if(left!=right)

{

         n[left]=n[right];

         left++;

}

while((n[left]<=pivot)&&(left<right))

left++;

if(left!=right)

{

         n[right]=n[left];

         right--;

 

}

}

 

n[left]=pivot;

pivot=left;

left=l_hold;

right=r_hold;

if(left<pivot)

q_sort(n,left,pivot-1);

if(right>pivot)

q_sort(n,pivot+1,right);

return(0);

}

 

 

 

   

 

 

 

 

 

 

 

 

 

 

 

 

Output:

 

     Quick Sort

      ~~~~~~~

 

Enter the no of elements in the array: 6

 

Enter the 1 element:  34

 

Enter the 2 element:   0

 

Enter the 3 element: -1

 

Enter the 4 element: -2

 

Enter the 5 element:  20

 

Enter the 6 element:   8

 

The sorted list is:    -2      -1      0       8       20      34


 

                        /* Heap Sort */

 

#include<stdio.h>

#include<conio.h>

 

void main()

{

int i,a[50],n,temp;

clrscr();

printf("\n\t\t Heap Sort Output\n");

printf("\t\t   ~~~~~~~~~~~~~~~~~\n");

printf("Enter the number of elements in the array:");

scanf("%d",&n);

for(i=0;i<n;i++)

{

              temp=i;

              printf("Enter the %d element:",++temp);

              scanf("%d",&a[i]);

}

heapsort(a,n);

printf("\n The Sorted list is");

for(i=0;i<n;i++)

printf("\t%d",a[i]);

getch();

}

 

 

int heapsort(int n[],int a)

{

int i,temp;

for(i=(a/2)-1;i>=0;i--)

            siftdown(n,i,a);

for(i=a-1;i>=1;i--)

{

             temp=n[0];

             n[0]=n[i];

             n[i]=temp;

             siftdown(n,0,i-1);

}

return(0);

}

 

 

 

 

 

 

int siftdown(int n[],int root,int bottom)

{

 

        int done,maxchild,temp;

       done=0;

       while((root*2<=bottom)&&(!done))

       {

         if(root*2==bottom)

         maxchild=root*2;

         else if(n[root*2]>n[root*2+1])

         maxchild=root*2;

         else

         maxchild=root*2+1;

         if(n[root]<n[maxchild])

        {

           temp=n[root];

           n[root]=n[maxchild];

           n[maxchild]=temp;

           root=maxchild;

         }

         else

         done=1;

     }

     return(0);

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Output:

 

                   Heap Sort

 

Enter the number of elements in the array:6

 

Enter the 1 element:89

 

Enter the 2 element:-78

 

Enter the 3 element:78

 

Enter the 4 element:0

 

Enter the 5 element:34

 

Enter the 6 element:1

 

The Sorted list is     -78     0       1       34      78      89

 


 

/* DOUBLY LINKED LIST */

 

#include<stdio.h>

#include<conio.h>

 

struct dllist

{

        int number;

        struct dllist *next;

        struct dllist *prev;

};

 

struct dllist *head, *tail;

 

void append_node(struct dllist *inode);

void insert_node(struct dllist *inode,struct dllist *after);

void remove_node(struct dllist *inode);

 

int main(void)

{

        struct dllist *inode;

        int i=0;

        clrscr();

        /*add some numbers to the double linked list*/

       for(i=0;i<=5;i++)

      {

        inode=(struct dllist*)malloc(sizeof(struct dllist));

        inode->number=i;

        append_node(inode);

      }

 

                /*print  the dllist*/

     for(inode=head;inode!=NULL;inode=inode->next)

      printf("\n%d\n",inode->number);

   

               /*destroy the dllist*/

      while(head!=NULL)

      remove_node(head);

      getch();

      return 0;

}

 

 

 

 

void append_node(struct dllist *inode)

{

      if(head==NULL)

     {

       head=inode;

       inode->prev=NULL;

     }

     else

    {

      tail->next=inode;

      inode->prev=tail;

    }

      tail=inode;

      inode->next=NULL;

}

 

void insert_node(struct dllist *inode,struct dllist *after)

{

      inode->next=after->next;

      inode->prev=after;

      if(after->next!=NULL)

       after->next->prev=inode;

     else

      tail=inode;

      after->next=inode;

}

 

void remove_node(struct dllist *inode)

{

     if(inode->prev==NULL)

     head=inode->next;

     else

     inode->prev->next=inode->next;

     if(inode->next==NULL)

     tail=inode->prev;

     else

     inode->next->prev=inode->prev;

}

 

 

 

 

 

 

 

 

OUTPUT:-

DOUBLY LINKED LIST

 

 

 

0

1

2

3

4

5

 

 

 


 

                                               

/* DEPTH FIRST SEARCH */

 

 

 

#include<stdio.h>

#include<conio.h>

 

int a[10][10],visited[10],n;

void search_from(int);

 

void main()

{

int i,j;

clrscr();

printf("Enter the number of nodes:");

scanf("%d",&n);

printf("Enter the adjancency matrix:\n");

for(i=1;i<=n;i++)

for(j=1;j<=n;j++)

if(i!=j)

{

printf("Enter the value of %d %d element",i,j);

scanf("%d",&a[i][j]);

}

printf("Nodes are visited in the order\n");

for(i=1;i<=n;i++)

if(visited[i]==0)

search_from(i);

getch();

}

 

void search_from(int k)

{

int i,j;

printf("-> %d",k);

visited[k]=1;

for(i=1;i<=n;i++)

if(visited[i]==0)

if(a[k][i]!=0)

search_from(i);

getch();

}

                                               

 

 

 

 

 

OUTPUT

------------

DEPTH FIRST SEARCH

-------------------------------

 

Enter the number of nodes: 5

Enter the adjacency matrix:

Enter the value of 1 2 element 0

Enter the value of 1 3 element 1

Enter the value of 1 4 element 0

Enter the value of 1 5 element 0

Enter the value of 2 1 element 0

Enter the value of 2 3 element 0

Enter the value of 2 4 element 0

Enter the value of 2 5 element 1

Enter the value of 3 1 element 0

Enter the value of 3 2 element 1

Enter the value of 3 4 element 1

Enter the value of 3 5 element 0

Enter the value of 4 1 element 0

Enter the value of 4 2 element 0

Enter the value of 4 3 element 0

Enter the value of 4 5 element 0

Enter the value of 5 1 element 0

Enter the value of 5 2 element 0

Enter the value of 5 3 element 0

Enter the value of 5 4 element 0

 

Nodes are visited in the order

-> 1-> 3-> 2-> 5-> 4

 

 

 

 


 

/* CIRCULAR LINKED LIST */

 

#include<stdio.h>

#include<conio.h>

 

struct node

{

     int data;

     struct node *link;

};

 

void addcirq(struct node **,struct node **);

void display(struct node *);

void deleteq(struct node **,struct node **);

 

//typedef struct node *first,*last;

void main()

{

   struct node *front,*rear;

   int ch;

   front=rear=NULL;

 

while(1)

{

   clrscr();

   printf("\n\t CIRCULAR LINKED LIST:");

   printf("\n\t ~~~~~~~~~~~~~~~~~~~~~");

   printf("\n\t1.Addnode");

   printf("\n\t2.List elements");

   printf("\n\t3.Delete_end");

   printf("\n\t4.Exit");

   printf("\n\t Enter your choise:");

   scanf("%d",&ch);

switch(ch)

{

 

case 1:

       clrscr();

       addcirq(&front,&rear);

       break;

 

case 2:

       display(front);

       break;

 

 

 

 

 

 

case 3:

       deleteq(&front,&rear);

       break;

}

if(ch==4)

break;

}

}

/* adds a new elements at the of queue*/

void addcirq(struct node **f,struct node **r)

{

    int a;

   struct node *q;

clrscr();

/* create new node */

   printf("\n Enter any elements to push:");

   scanf("%d",&a);

   q=malloc(sizeof(struct node));

   q->data=a;

/* if the queue is empty*/

if(*f==NULL)

   *f=q;

   else

   (*r)->link=q;

   *r=q;

   (*r)->link=*f;

}

 

void deleteq(struct node **f,struct node **r)

{

    struct node *q;

    int item;

    clrscr();

    if(*f==NULL)

    printf("The first list is empty");

else

{

 

 

 

 

 

 

 

 

if(*f==*r)

{

    item=(*f)->data;

    free(*f);

   *f=NULL;

   *r=NULL;

}

else

{

     q=*f;

     item=q->data;

    *f=(*f)->link;

   (*r)->link=*f;

free(q);

}

}

printf("\n the item is deleted");

getch();

}

 

//diplay operation

void display(struct node *f)

{

struct node *q=f,*p=NULL;

clrscr();

printf("The elements are:");

while(q!=p)

{

printf("%3d",q->data);

q=q->link;

p=f;

}

getch();

}

 

 

 

 

 

 

 

 

 

 

OUTPUT:

               

 CIRCULAR LINKED LIST:

        1. Addnode

        2. List elements

        3. Delete_end

        4. Exit

        

           Enter your choise: 1

Enter any elements to push: 23

Enter your choise: 1

Enter any elements to push: 55

Enter your choise: 1

Enter any elements to push: 44

Enter your choise: 2

The elements are: 23 55 44

Enter your choise: 3

The item is deleted

Enter your choise: 4

 


 

                        /*BREADTH FIRST SEARCH*/

 

 

#include<stdio.h>

#include<conio.h>

 

int a[10][10],visited[10],n;

void search_from1(int);

 

void main()

{

    int i,j;

    clrscr();

    printf("Enter the number of nodes:");

    scanf("%d",&n);

    printf("Enter the adjancency matrix:\n");

    for(i=1;i<=n;i++)

    for(j=1;j<=n;j++)

    if(i!=j)

    {

     printf("Enter the value of %d %d element ",i,j);

     scanf("%d",&a[i][j]);

    }

    printf("\nNodes are visited in the order\n");

    for(i=1;i<=n;i++)

    if(visited[i]==0)

    search_from1(i);

    getch();

}

 

void search_from1(int z)

{

    int i,j;

    printf("-> %d",z);

    visited[z]=1;

    for(i=1;i<=n;i++)

    for(j=1;j<=n;j++)

    {

     if(visited[i]==0)

     if(a[z][j]!=0)

     search_from1(i);

     break;

    }

    getch();

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

OUTPUT

------------

 

BREADTH FIRST SEARCH

-------------------------------------

 

 

Enter the number of nodes:4

Enter the adjancency matrix:

Enter the value of 1 2 element 1

Enter the value of 1 3 element 0

Enter the value of 1 4 element 0

Enter the value of 2 1 element 1

Enter the value of 2 3 element 0

Enter the value of 2 4 element 1

Enter the value of 3 1 element 1

Enter the value of 3 2 element 0

Enter the value of 3 4 element 0

Enter the value of 4 1 element 0

Enter the value of 4 2 element 1

Enter the value of 4 3 element 1

 

 

Nodes are visited in the order

-> 1-> 2-> 3-> 4

 

 


 

/* Binary Tree Traversal */

 

 

#include<stdio.h>

#include<conio.h>

#include<malloc.h>

struct bnode

{

      int data;

      struct bnode *left,*right;

}*bt;

 

void create()

{

        struct bnode *temp,*p,*pre;

        int flag,key,d;

        bt=NULL;

        printf("\n\t Read the value until -1 to exit:\n\t");

        scanf("%d",&d);

 

while(d!=-1)

{

        temp=(struct bnode *)(malloc(sizeof(struct bnode)));

        temp->data=d;

        temp->left=NULL;

        temp->right=NULL;

        if(bt==NULL)

        bt=temp;

else

{

         p=bt;

         pre=bt;

while(p!=NULL)

{

         key=p->data;

         if(d<key)

{

         flag=0;

         pre=p;

         p=p->left;

}

 

 

else if(d>key)

{

         flag=1;

         pre=p;

          p=p->right;

     }

}

          if(flag==0)

          pre->left=temp;

else

          pre->right=temp;

}

           scanf("%d",&d);

     }

}

 

void preord(struct bnode *p)

{

          if(p==NULL)

          return;

          printf("\t %d",p->data);

          if(p->left !=NULL)

         preord(p->left);

         if(p->right)

         preord(p->right);

   }

void postord(struct bnode *p)

{

           if(p==NULL)

           return;

           if(p->left)

           postord(p->left);

           if(p->right)

           postord(p->right);

           printf("\t %d",p->data);

   }

 

 

 

 

 

 

 

void inord(struct bnode *p)

{

             if(p==NULL)

             return;

             if(p->left)

             inord(p->left);

             printf("\t %d",p->data);

             if(p->right)

             inord(p->right);

 

  }

 

 

 

void main()

 

{

 

        clrscr();

        printf("\n\t Binary Tree Traversal");

        printf("\n\t~~~~~~~~~~~~~~~~~~~~~~~\n");

        create();

       printf("\n\t Inorder Traversal \n\t");

        inord(bt);

        printf("\n\t Preorder Traversal \n\t");

        preord(bt);

        printf("\n\t Postorder Traversal \n\t");

        postord(bt);

        getch();

 

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

OUTPUT:-

 

 

 

         Binary Tree Traversal

      

 

 

         Read the value until -1 to exit:

 

          45 67 89 34 12 78 110 -1

 

         Inorder Traversal

 

                 12      34      45      67      78      89      110

 

 

        Preorder Traversal

 

                45      34      12      67      89      78      110

 

 

        Postorder Traversal

 

                 12      34      78      110     89      67      45

 

 


 

/* BINARY SEARCH*/

 

 

 

#include<stdio.h>

#include<conio.h>

#include<ctype.h>

#include<math.h>

#include<string.h>

#define MAX 1000

 

void main()

{

          int i,n,v1,k[MAX];

          int bsearch(int k[MAX],int n);

          clrscr();

          printf("\n\t\t Binary search:");

          printf("\n\t\t ~~~~~~~~~~~~~~")

          printf("\n Read the total no of elements:");

          scanf("%d",&n);

          printf("\n Read the elements:");

 

for(i=1;i<=n;i++)

          scanf("%d",&k[i]);

          v1=bsearch(k,n);

          printf("\n OUTPUT");

 

if(v1!=-1)

{

           printf("\n\n Binary search is possible");

           printf("\n\n\n Search position of elements:%d",v1);

}

 

else

{

           printf("\n Elemnt is not found");

      }

  }

 

 

 

 

 

 

 

 

 

 

 

int bsearch(int b[MAX],int l)

{

             int i,elem;

             int low,high,mid;

             printf("\n Read the elements to be searched:");

             scanf("%d",&elem);

             low=0;high=l;

            while(low<=high)

         {

            mid=(low+high)/2;

            if(elem<b[mid])

            high=mid-1;

           else if(elem>b[mid])

           low=mid+1;

 

else

           return(mid);

   }

    return(-1);

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

OUTPUT:-

 

 

                 Binary search:

                 ~~~~~~~~~~~~

Read the total no of elements: 5

 

Read the elements:

                    78

                    89

                    92

                    94

                    99

 

Read the elements to be searched: 92

 

OUTPUT

 

Binary search is possible

 

 

Search position of elements: 3

 

 

 

 


 


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


 

 

 

 

0 comments:

Post a Comment