Saturday, 9 August 2014

Quick sort

The following is a program to implement quick sort. If you have any doubts, please let me know.
#include<stdio.h>
void quicksort(int [50],int,int);
main()
{
  int a[50],n,i;
  printf("\nEnter size of the array: ");
  scanf("%d",&n);
  printf("Enter the elements: ");
  for(i=0;i<n;i++)
    scanf("%d",&a[i]);
  printf("\nBefore Sorting:\n");
    for(i=0;i<n;i++)
    printf(" %d ",a[i]);
quicksort(a,0,n-1);
  printf("\nSorted elements:\n ");
  for(i=0;i<n;i++)
    printf(" %d ",a[i]);
  printf("\n");
}
void quicksort(int a[50],int left,int right)
{    int pivot,j,temp,i;
     if(left<right)
 {      pivot=left;
         i=left;
         j=right;
         while(i<j)
            {
             while(a[i]<=a[pivot]&&i<right)
                 i++;
             while(a[j]>a[pivot])
                 j--;
             if(i<j)
            {   temp=a[i];
                 a[i]=a[j];
                 a[j]=temp;
             }
         }
         temp=a[pivot];
         a[pivot]=a[j];
         a[j]=temp;
         quicksort(a,left,j-1);
         quicksort(a,j+1,right);
    }

}

Insertion, selection, and bubble sort

The following is a program for selection, insertion and bubble sort. If you have any doubts please let me know.
#include<stdio.h>
void basd(int[],int);
void insasd(int[],int);
void selasd(int[],int);
int i,j,k,temp;
main()
{
  int ch,a[100],i,j,n;
   printf("Enter the number of elements:");
 scanf("%d",&n);
 printf("Enter the array elements:");
 for(i=0;i<n;i++)
 {
  scanf("%d",&a[i]);
 }
 do
 {
  printf("\nMenu:\n1.bubble\n2.selection\n3.insertion\n4.Exit\n");
  printf("Enter your choice:");
  scanf("%d",&ch);
  switch(ch)
  {
   case 1:
        basd(a,n);
        break;
   case 2:
        selasd(a,n);
        break;
   case 3:
        insasd(a,n);
        break;
    case 4:break;
    default:printf("Wrong choice!");
          break;
  }
}while(ch!=4);
}

void selasd(int b[],int n)
{
 int i,j,temp,a[50],k;
  printf("\nSELECTION SORT\n");
  printf("Unsorted array:\n");
 for(i=0;i<n;i++)
 printf("%d ",b[i]);
 printf("\n");
 for(i=0;i<n;i++)
  a[i]=b[i];

  for(i=0;i<n;i++)
  {
   for(j=i+1;j<n;j++)
   {
     if(a[j]<a[i])
     {
       temp=a[j];
       a[j]=a[i];
       a[i]=temp;
     }
   }

  }
  printf("\nAfter Selection Sort:");
  for(i=0;i<n;i++)
   printf(" %d",a[i]);
}

void insasd(int b[],int n)
{
  int a[50];
 printf("\nINSERTION SORT\n");
  printf("Unsorted array:\n");
 for(i=0;i<n;i++)
 printf("%d ",b[i]);
 printf("\n");
  for(i=0;i<n;i++)
  a[i]=b[i];
 for(i=1;i<n;i++)
 {
  temp=a[i];
  j=i-1;
  while(temp<a[j])
  {
   a[j+1]=a[j];
   j=j-1;
  }
  a[j+1]=temp;
  printf("\n");
 }
 printf("\nSorted array is:");
 for(i=0;i<n;i++)
 {
  printf(" %d ",a[i]);
 }
}
void basd(int b[],int n)
{
 int i,j,temp,a[50],k;
 printf("\nBUBBLE SORT\n");
  printf("Unsorted array:\n");
 for(i=0;i<n;i++)
 printf("%d ",b[i]);
 printf("\n");
  for(i=0;i<n;i++)
  a[i]=b[i];
 for(i=0;i<n;i++)
 {
  for(j=0;j<(n-i)-1;j++)
  {
    if(a[j]>a[j+1])
    {
     temp=a[j];
     a[j]=a[j+1];
     a[j+1]=temp;
    }
  }
 }
 printf("\nAfter Bubble Sort");
  for(i=0;i<n;i++)
   printf(" %d",a[i]);
   printf("\n");
}







Insertion, deletion and reversal of link list

This is a program to implement the various functions of a link list. If you have any doubts please let me know.
#include<stdio.h>
#include<stdlib.h>
struct node
{
 int data;
 struct node *next;
};
struct node *newn,*start=NULL,*ptr=NULL,*prev=NULL,*rev,*temp=NULL;
void insbeg();
void insend();
void inspos();
void insaft();
void delbeg();
void delend();
void delelement();
void display();
main()
{
 int ch1,ch,ch2;
 do
 {
  printf("\nMenu:\n1.Insertion\n2.Deletion\n3.Reversal\n4.Display\n5.Exit\n");
  printf("Enter your choice:");
  scanf("%d",&ch);
  switch(ch)
  {
   case 1:
    do
    {
     printf("\nSubmenu\n1.Insertion at beginning\n2.Insetion at End\n3.Insert at a specified position\n4.Insert after a specified node\n5.Exit from menu\n");

     printf("Enter your choice:");
     scanf("%d",&ch1);
     switch(ch1)
     {
      case 1:insbeg();
             break;
      case 2:
             insend();
             break;
      case 3:
             inspos();
             break;
      case 4:
             insaft();
             break;
      case 5:
             break;
      default:printf("Wrong choice\n");
              break;
     }
    }while(ch1!=5);
    break;
   case 2:
    do
    {
     printf("\nSubmenu\n1.Deletion at beginning\n2.Deletion at end\n3.Deletion of specific node\n4.Exit from this menu\n");
     printf("Enter your choice:");
     scanf("%d",&ch2);

     switch(ch2)
     {
      case 1:delbeg();
             break;
      case 2:delend();
             break;
      case 3:delelement();
             break;
      case 4:
             break;
      default:printf("Wrong choice\n");
              break;
     }
    }while(ch2!=4);
    break;
  case 3:
    if(start==NULL)
    {
     printf("\nUnderflow\n");
    }
    else
    {
     ptr=start;
     prev=NULL;
     while(ptr->next!=NULL)
     {
      rev=prev;
      prev=ptr;
      ptr=ptr->next;
      prev->next=rev;
     }
     start=prev;
     display();
   }
   break;
  case 4:
     display();
    break;
  case 5:
         break;
  default:printf("\nWrong Choice\n");
          break;
   }
  }while(ch!=5);
}

void insbeg()
{
 int num;
 printf("Enter the element:");
 scanf("%d",&num);
 newn=(struct node *)malloc(sizeof(struct node));
 if(start==NULL)
 {
  newn->data=num;
  newn->next=NULL;
  start=newn;
 }
 else
 {
  newn->data=num;
  newn->next=start;
  start=newn;
 }
}

void insend()
{
 int num;
 printf("Enter the element:");
 scanf("%d",&num);
 newn=(struct node *)malloc(sizeof(struct node));
  if(start==NULL)
 {
  newn->data=num;
  newn->next=NULL;
  start=newn;
 }
 else
 {
  ptr=start;
  while((ptr->next)!=NULL)
    ptr=ptr->next;
  newn->data=num;
  ptr->next=newn;
  newn->next=NULL;
 }
}

void inspos()
{
 int num,k=0,n;
 ptr=start;
 printf("Enter the the position at which the element must be inserted:");
 scanf("%d",&n);
 if(n==1)
 {
  insbeg();
 }
 else
 {
   printf("Enter the element:");
   scanf("%d",&num);
   while(k!=(n-1))
  {
    prev=ptr;
    ptr=ptr->next;
    k++;
   }
  newn=(struct node *)malloc(sizeof(struct node));
  newn->data=num;
  newn->next=ptr;
  prev->next=newn;
 }
}

void insaft()
{
 int n,num,c=0;
 ptr=start;
 printf("Enter the element:");
 scanf("%d",&num);
 printf("Enter the element after which insertion must be done:");
 scanf("%d",&n);
 while(ptr!=NULL)
 {
         if(ptr->data!=n)
         {
        ptr=ptr->next;
         }
         else
         {
                 c++;
                 {

                        newn=(struct node *)malloc(sizeof(struct node));
                        newn->data=num;
                        newn->next=ptr->next;
                        ptr->next=newn;
                        ptr=ptr->next;
                        break;
                }
         }
 }
 if(c==0)
         printf("NODE NOT FOUND");

}



void delbeg()
{
        if(start==NULL)
        {
                printf("\nUnderflow\n");
        }
        else
        {
                ptr=start;
                start=start->next;
                free(ptr);
        }
}

void delend()
{
        if(start==NULL)
        {
                printf("\nUnderflow\n");
        }
        else if(start->next==NULL)
          delbeg();
        else
        {
         ptr=start;
         while(ptr->next!=NULL)
        {
                prev=ptr;
                ptr=ptr->next;
        }
        prev->next=NULL;
        free(ptr);
        }
}
void delelement()
{
 int num,c=0;
 if(start==NULL)
 {
  printf("\nUnderflow\n");
 }
 else
 {
 printf("Enter the element to be deleted:");
 scanf("%d",&num);
 ptr=start;
 while(ptr!=NULL)
 {
        if(start->data==num)
        {
                 c++;
                delbeg();
                ptr=start;
                break;

        }
        else if(ptr->data==num)
        {
                c++;
          temp=ptr;
          prev->next=temp->next;
          ptr=ptr->next;
          free(temp);
          break;

        }

                prev=ptr;
                ptr=ptr->next;

 }

if(c==0)
 {
         printf("NODE NOT FOUND");
 }
}
}
 void display()
{
   if(start==NULL)
    {
     printf("\nUnderflow\n");
    }
    else
        {
    ptr=start;
    while(ptr->next!=NULL)
    {
     printf("=>%d  ",ptr->data);
     ptr=ptr->next;
    }
    printf("=>%d  ",ptr->data);
    }
}




Insertion and deletion of elements in a queue

The following program is the insertion, deletion of elements in a queue. If you have any doubts please let me know.
#include<stdio.h>
#include<stdlib.h>
struct node
{
        int data;
        struct node *next;
}*newn,*f=NULL,*r=NULL,*temp=NULL;
void ins();
void del();
void display();
main()
{
   int ch;
   do
   {
       printf("\nMenu:\n1.Insert\n2.Delete\n3.Display\n4.Exit\n");
       printf("Enter your choice:");
       scanf("%d",&ch);
       switch(ch)
       {
          case 1:
                  ins();
                  break;
          case 2:
                  del();
                  break;
          case 3:
                  display();
                  break;
           case 4:
                   break;
           default:
                    printf("Wrong Choice!");
                    break;
       }
   }while(ch!=4);
}
void ins()
{
        int x;
        printf("Enter the value to be inserted:");
        scanf("%d",&x);
        newn=(struct node *)malloc(sizeof(struct node));
        newn->data=x;
        if((f==NULL)&&(r==NULL))
        {
                newn->next=NULL;
                f=newn;
                r=newn;
        }
        else
        {
                r->next=newn;
                newn->next=NULL;
                r=r->next;
        }
}
void del()
{
        if((f==NULL)&&(r==NULL))
                printf("\nQueue Underflow\n");
        else if(f==r)
       {
         temp=f;
         f=NULL;
         r=NULL;
         free(temp);
       }
        else
        {
                temp=f;
                f=f->next;
                free(temp);
        }
}
void display()
{
        if((f==NULL)&&(r==NULL))
                printf("\nQueue Underflow\n");
        else
        {
                temp=f;
                while(temp!=r)
                {
                        printf("=>%d  ",temp->data);
                        temp=temp->next;
                }
                printf("=>%d  ",r->data);
        }
}


Insertion, Deletion, Peep, Isempty of elements.

The following is a program for ins,del,peep, isempty of elements. If you have any doubts please let me know.
#include<stdio.h>
#include<stdlib.h>
struct node
{
   int data;
   struct node *next;
};
struct node *top=NULL,*newn,*ptr=NULL;
void ins();
void del();
void peep();
void display();
void isempty();
main()
{
   int ch;
   do
   {
      printf("\nMenu:\n1.Insert\n2.Delete\n3.Peep\n4.Display\n5.Is empty\n6.Exit\n");
      printf("Enter your choice:");
      scanf("%d",&ch);
      switch(ch)
     {
         case 1:
            ins();
            break;
         case 2:
            del();
            break;
         case 3:
            peep();
            break;
         case 4:
            display();
            break;
         case 5:
            isempty();
            break;
         case 6:
            break;
         default:
             printf("Wrong choice");
             break;
      }
   }while(ch!=6);
}

void ins()
{
  int num;
  printf("Enter the element to be inserted:");
  scanf("%d",&num);
  newn=(struct node *)malloc(sizeof(struct node));
  newn->data=num;
  if(top==NULL)
 {
  newn->next=NULL;
  top=newn;
 }
 else
 {
  newn->next=top;
  top=newn;
 }
}

void del()
{
 if(top==NULL)
  printf("\nUndeflow\n");
 else
  {
   ptr=top;
   top=top->next;
   free(ptr);
  }
}

void peep()

{
 if(top==NULL)
 {
  printf("\nUnderflow\n");
 }
 else
 {
 printf("top: %d ",top->data);
 }
}
void display()
{
 ptr=top;
 if(ptr==NULL)
 {
  printf("\nUnderflow\n");
 }
 else
 {
 while(ptr->next!=NULL)
 {
  printf("=>%d ",ptr->data);
  ptr=ptr->next;
 }
 printf(" =>%d ",ptr->data);
 }
}

void isempty()
{
 if(top==NULL)
 {
  printf("\nStack is empty\n");
 }
 else
 {
  printf("\nStack is not empty\n");
 }
}


















Polynomial Addition

The following is a C program to implement polynomial addition. If you have any doubts please let me know.
#include<stdio.h>
#include<stdlib.h>
struct node
{
 int d;
 int coef;
 struct node *next;
}*newn,*n=NULL,*p=NULL,*q=NULL,*r=NULL,*start1=NULL,*start2=NULL,*start3=NULL,*ptr=NULL,*prev=NULL;
void display(struct node *,struct node *);
void display1();
main()
{
 int a,b,i;
 i=0;
 printf("\nEnter values for polynomial1:\n");
 do
  {
 printf("Enter the coeff of x^%d:",i);
      newn=(struct node *)malloc(sizeof(struct node));
      scanf("%d",&newn->coef);
      newn->d=i;
      newn->next=p;
      p=newn;
      i++;
 printf("\n Do you wish to add more elements in polynomial 1:");
      printf("\nPress 1 to continue");
    scanf("%d",&a);
  }while(a==1);
 start1=p;
 printf("POLYNOMIAL 1:");
 display(start1,p);
 i=0;
 printf("\n\nEnter values for polynomial2:\n");
 do
 {
  printf("Enter the coef of x^%d:",i);
  newn=(struct node *)malloc(sizeof(struct node));
  scanf("%d",&newn->coef);
  newn->d=i;
  newn->next=q;
  q=newn;
  i++;
  printf("\n Do you wish to add more elements in polynomial 2:");
  printf("\nPress 1 to continue");
  scanf("%d",&b);
 }while(b==1);
 start2=q;
 printf("POLYNOMIAL 2:");
 display(start2,q);
 printf("\n\nSUM:");
 p=start1;
 q=start2;
 while((p!=NULL)&&(q!=NULL))
 {
if(p->d==q->d)
{
newn=(struct node *)malloc(sizeof(struct node));
newn->next=r;
newn->coef=p->coef+q->coef;
newn->d=p->d;
p=p->next;
q=q->next;
r=newn;
}
else if(p->d>q->d)
{
newn=(struct node *)malloc(sizeof(struct node));
newn->next=r;
newn->coef=p->coef;
newn->d=p->d;
p=p->next;
r=newn;
}
else if(p->d<q->d)
{
newn=(struct node *)malloc(sizeof(struct node));
newn->next=r;
newn->coef=q->coef;
newn->d=q->d;
q=q->next;
r=newn;
}
 }
 if(p==NULL)
 {
while(q!=NULL)
{
newn=(struct node *)malloc(sizeof(struct node));
newn->next=r;
newn->coef=q->coef;
newn->d=q->d;
q=q->next;
r=newn;
}
 }
 else if(q==NULL)
 {
while(p!=NULL)
{
newn=(struct node *)malloc(sizeof(struct node));
newn->next=r;
newn->coef=p->coef;
newn->d=p->d;
p=p->next;
r=newn;
}
 }

 start3=r;
 display1();
}

void display(struct node *start,struct node *ptr)
{
 ptr=start;
 while(ptr!=NULL)
 {
   printf("%dx^%d",ptr->coef,ptr->d);
   if(ptr->next!=NULL)
  printf("+");
   ptr=ptr->next;
 }
}
void display1()
{
 r=start3;
 prev=NULL;
 while(r!=NULL)
 {
   ptr=prev;
   prev=r;
   r=r->next;
   prev->next=ptr;
 }
 start3=prev;
 r=start3;
 while(r!=NULL)
 {
   printf("%dx^%d",r->coef,r->d);
   if(r->next!=NULL)
  printf("+");
   r=r->next;
 }
}

The various functions on BST

Now this length program is the Insertion, Deletion , Traversal, etc of the Binary Search Tree. If you have doubts, please let me know.
#include<stdio.h>
#include<stdlib.h>
#define max(A,B) (((A)>(B))?(A):(B))
struct node
{
        int data;
        struct node *rc,*lc;
}*newn,*ptr=NULL,*pre=NULL,*root=NULL,*succ=NULL;
void ins(int);
void del(int);
void inorder(struct node *temp);
void preorder(struct node *temp);
void postorder(struct node *temp);
void display(struct node*);
int height(struct node*);

main()
{
        int ch,r,x,h,l,d;
        do
        {
        printf("\nMenu:\n1.Isertion\n2.Deletion\n3.Traversal\n4.Level\n5.Height\n6.Display\n7.Exit\n");
        printf("\nEnter your choice:");
        scanf("%d",&ch);
        switch(ch)
        {
        case 1:
                printf("Enter the element to be inserted:");
                scanf("%d",&d);
                scanf("%d",&d);
                ins(d);
                break;
        case 2:
                printf("Enter the element to be deleted:");
                scanf("%d",&x);
                del(x);
                break;
        case 3: display(root);
                break;
        case 4:
                h=height(root);
                l=h-1;
                printf("\nLEVEL:%d",l);
                break;
        case 5:h=height(root);
                printf("\nHEIGHT:%d",h);
                break;
        case 6:
                display(root);
                break;
        case 7:break;
        default:
                printf("\nWrong choice\n");
                break;
        }
        }while(ch!=7);
}
void ins(int d)
{
        int flag=0;
         ptr=root;
         newn=(struct node*)malloc(sizeof(struct node));
        newn->data=d;
        newn->lc=NULL;
        newn->rc=NULL;
        while((ptr!=NULL)&&(flag==0))
        {
                if(d<ptr->data)
                {
                        pre=ptr;
                        ptr=ptr->lc;
                }
                else if(d>ptr->data)
                {
                        pre=ptr;
                        ptr=ptr->rc;
                }
                else
                {
                        flag=1;
                        printf("Element already exists!");
                }
        }
        if(ptr==NULL)
        {
           if(root==NULL)
           {
            root=newn;
            ptr=root;
           }
           else if(pre->data<d)
           {
             pre->rc=newn;
             ptr=pre->rc;
           }
           else if(pre->data>d)
           {
             pre->lc=newn;
             ptr=pre->lc;
           }
        }

}

void del(int x)
{
        ptr=root;
        while(ptr!=NULL)
        {
                if(x<ptr->data)
                {
                        pre=ptr;
                        ptr=ptr->lc;
                }
                else if(x>ptr->data)
                {
                        pre=ptr;
                        ptr=ptr->rc;
                }
                else if(ptr->data==x)
                {
                        if((ptr->lc==NULL)&&(ptr->rc==NULL))
                        {
                                if(root==ptr)
                                {
                                 free(root);
                                 root=NULL;
                                 break;
                                }
                                else if(pre->lc==ptr)
                                        pre->lc=NULL;
                                else if(pre->rc==ptr)
                                        pre->rc=NULL;
                                free(ptr);
                            break;

                        }

                        else if(ptr->lc!=NULL&&ptr->rc==NULL)
                        {
                        if(pre->lc==ptr)
                                pre->lc=ptr->lc;
                        else if(pre->rc==ptr)
                                pre->rc=ptr->lc;
                        free(ptr);
                        break;
                        }
                        else if(ptr->lc==NULL&&ptr->rc!=NULL)
                        {
                        if(pre->lc==ptr)
                                pre->lc=ptr->rc;
                        else if(pre->rc==ptr)
                                pre->rc=ptr->rc;
                        free(ptr);
                        break;
                        }
                        else if(ptr->lc!=NULL&&ptr->rc!=NULL)
                        {
                        succ=ptr->rc;
                        if(succ->lc==NULL)
                        {
                                if(ptr==root)
                                {
                                   succ->lc=root->lc;
                                  root=succ;
                                }
                                else if(pre->lc==ptr)
                                {
                                        pre->lc=succ;
                                        succ->lc=ptr->lc;
                                }
                                else if(pre->rc==ptr)
                                {
                                        pre->rc=succ;
                                        succ->rc=ptr->rc;
                                }
                                free(ptr);
                                break;
                        }
                        else
                        {
                                while(succ->lc!=NULL)
                                {
                                        pre=succ;
                                        succ=succ->lc;
                                }
                                ptr->data=succ->data;
                                pre->lc=succ->rc;
                                free(ptr);
                                break;
                                }
                        }
                }
        }
        if(ptr==NULL)
                {
                        printf("\nElement absent\n");
                }
}
void postorder(struct node *temp)
{
        if(temp!=NULL)
        {
                postorder(temp->lc);
                postorder(temp->rc);
                printf("  %d",temp->data);
        }
  }
 void preorder(struct node *temp)
{

        if(temp!=NULL)
        {
                printf("  %d",temp->data);
                preorder(temp->lc);
        preorder(temp->rc);
        }
}

void display(struct node *root1)
{
int i;
if(root1==NULL)
        printf("Empty tree!");
else
{
printf("\nEnter the order\n1:inorder\n2:preorder\n3:postorder\n");
         printf("\nEnter your choice:");
         scanf("%d",&i);
         if(i==1)
         {
                 printf("\nINORDER:\n");
         inorder(root1);

          }
         else if(i==2)
         {
         printf("\nPREORDER:\n");
         preorder(root1);
         }
         else
         {
                 printf("\nPOSTORDER:\n");
                 postorder(root1);
         }
}
}
 void inorder(struct node *temp)
{
        if(temp!=NULL)
        {
        inorder(temp->lc);
                printf("  %d",temp->data);
                inorder(temp->rc);
        }
}

int height(struct node *root)
{
        if(root==NULL)
                return 0;
        else
             return max(height(root->lc),height(root->rc));
}

BFS and DFS

This is a program for the Depth First Search as well as Breadth first search. If you have any doubts, please let me know.

#include<stdio.h>
int adj[50][50];
int visited[50];
void adjcreate();
void dfs(int);
void bfs(int);
int n;
main()
{
    int i, v, ch;
     adjcreate();
       do
        {
            printf("\nMENU:\n1.DFS\n2.BFS\n3.Exit\n");
            printf("Enter your choice:");
            scanf("%d",&ch);
            switch (ch)
            {
                    case 1:
                    printf("\nEnter starting node for DFS:");
                    scanf("%d",&v);
                    for (i=1;i<=n;i++)
                        visited[i]=0;
                    dfs(v);
                    break;
                    case 2:
                    printf("\nEnter starting node for BFS:");
                    scanf("%d",&v);
                    for (i=1;i<=n;i++)
                         visited[i]=0;
                    bfs(v);
                    break;
                    case 3:
                       break;
                 default:
                    printf("Wrong choice!\n");
                     break;
                }
        }while(ch!=3);
}

void adjcreate()
{
        int i,j,ch1;
        printf("Enter the number of nodes :");
        scanf("%d", &n);
        for(i=1;i<=n;i++)
        {
         for(j=1;j<=n;j++)
         {
          printf("\n Press 1 if %d is connected to %d:",i,j);
          scanf("%d",&ch1);
          if(ch1==1)
          {
            adj[i][j]=1;
          }
          else
          {
             adj[i][j]=0;
          }
         }
        }
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=n;j++)
                printf("  %d ",adj[i][j]);
             printf("\n");
        }
}

void dfs(int v)
{
    int i,stack[50],top=-1,pop;
     top++;
    stack[top]=v;
     while(top>=0)
        {
            pop=stack[top];
            top--;
             if(visited[pop]==0)
                {
                    printf(" %d ",pop);
                    visited[pop]=1;
                }
            else
                continue;

            for(i=n;i>=1;i--)
                {
                    if(adj[pop][i]==1&&visited[i]==0)
                        {
                            top++;
                            stack[top]=i;
                        }
                }
        }
}

void bfs(int v)
{
    int i,front,rear;
    int que[50];
    front=rear=-1;
     printf(" %d ",v);
    visited[v]=1;
    rear++;
    front++;
    que[rear]=v;
     while(front<=rear)
      {
            v=que[front];
            front++;
             for (i=1;i<=n;i++)
                {
                      if (adj[v][i]==1&&visited[i]==0)
                        {
                            printf(" %d ",i);
                            visited[i]=1;
                            rear++;
                            que[rear]=i;
                        }
                }
        }
}








Heap Sort

This is the implementation of heap sort using the C programming language. If you have any doubts, please let me know.

#include<stdio.h>
int tree[50],n=0;
void del();
void ins(int);
main()
{
            int x,i,a;
            printf("\nEnter the number of elements:");
            scanf("%d",&a);
            printf("\nEnter the elements:");
            for(i=0;i<a;i++)
            {
                        scanf("%d",&x);
                        ins(x);
            }
            printf("\nAfter Sorting:\n");
            del();   
}

void ins(int x)
{
            int ptr,par;
            n=n+1;
            ptr=n;
            while(ptr>1)
            {
                        par=ptr/2;
                        if(x<=tree[par])
                        {
                                    tree[ptr]=x;
                                    return;
                        }
                        tree[ptr]=tree[par];
                        ptr=par;
            }
            tree[1]=x;
            return;
}

void del()
{
            int i,x,ptr,last,left,right;
            while(n!=0)
            {
                        x=tree[1];
                        printf(" %d  ",x);
                        last=tree[n];
                        n=n-1;
                        ptr=1;
                        left=2;
                        right=3;
                        while(right<=n)
                        {
                                    if(last>=tree[left]&&last>=tree[right])
                                    {
                                                tree[ptr]=last;
                                                break;
                                    }
                                    else if(tree[right]<=tree[left])
                                    {
                                                tree[ptr]=tree[left];
                                                ptr=left;
                                    }
                                    else
                                    {
                                                tree[ptr]=tree[right];
                                                ptr=right;
                                    }
                                    left=2*ptr;
                                    right=left+1;
                        }
                        if((left==n)&&(last<tree[left]))
                        {
                                    tree[ptr]=tree[left];
                                    ptr=left;
                        }
                        tree[ptr]=last;
}
            return;
}