Search This Blog

Showing posts with label adt. Show all posts
Showing posts with label adt. Show all posts

Wednesday, 13 January 2016

Queue using Linked List

/*Queue using Linked List*/

    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>

    typedef struct queue
    {

    int data;
    struct queue *next;

    }node;

    node *rear=NULL;
    node *front=NULL;
    node *getnode()
    {

    int val;
    node *temp;
    printf("Enter val:");
    scanf("%d",&val);
    temp=(node *)malloc(sizeof(node));
    temp->data=val;
    temp->next=NULL;
    return temp;

    }
    void insert()
    {

    node *neww;
    neww=getnode();
    if(front==NULL)
    {
         front=rear=neww;
    }
    else
    {
         rear->next=neww;
         rear=neww;
    }

    }
    void del()
    {

    if(front==NULL)
    {
         printf("\nQueue is Empty!\n");
    }
    else
    {
         node *t;
         t=front;
         printf("\n%d is deleted\n",front->data);
         front=front->next;
         t->next=NULL;
         free(t);
    }

    }
    void display()
    {

    if(front==NULL)
    {
          printf("\nEmpty!\n");
    }
    else
    {
         node *t;
         t=front;
         printf("\n");
         while(t)
         {
              printf("%d ",t->data);
              t=t->next;
         }
         printf("\n");
    }

    }
    void main()
    {

    int ch;
    clrscr();
    while(1)
    {
    printf("\n1.insert\n2.delete\n3.display\n4.exit\nYour choice:");
    scanf("%d",&ch);

    switch(ch)
    {
    case 1:insert();
    break;

    case 2:del();
    break;

    case 3:display();
    break;

    case 4:exit(0);
    break;

    default:printf("\nProper choice plz!\n");
    }

    }
    }

Stack Using Liked List

/* Stack Using Linked List*/

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

typedef struct stack
{

int data;
struct stack *next;

}node;
node *top=NULL;

node *getnode()
{

int val;
node *temp;
printf("Enter val");
scanf("%d",&val);
temp=(node *)malloc(sizeof(node));
temp->data=val;
temp->next=NULL;
return temp;

}
void push()
{

node *neww;
neww=getnode();
neww->next=top;
top=neww;

}
void pop()
{

if(top==NULL)
{
      printf("\nStack is Empty!\n");
}
else
{
     node *t;
     t=top;
     printf("\n%d is deleted\n",top->data);
     top=top->next;
     t->next=NULL;
     free(t);
}

}
void peep()
{

     printf("\nTop element is %d",top->data);
}

void display()
{

if(top==NULL)
{
     printf("\nEmpty!\n");
}
else
{
     node *t;
     t=top;
     while(t)
     {
          printf("\n%d",t->data);
          t=t->next;
     }
     printf("\n");
}

}
void main()
{

int ch;
clrscr();
while(1)
{
printf("\n1.push\n2.pop\n3.peep\n4.display\n5.exit\nYour choice:");
scanf("%d",&ch);

switch(ch)
{
case 1:push();
break;

case 2:pop();
break;

case 3:peep();
break;

case 4:display();
break;

case 5:exit(0);
break;

default:printf("\nProper choice plz!\n");
}
}

}

Tree (Recursive – inorder , preorder , postorder)

/*Tree (Recursive – inorder , preorder , postorder)*/

    #include<stdio.h>
    #include<conio.h>
    typedef struct tree{

    struct tree *right;
    struct tree *left;
    int data;

    }node;

    void inorder(node *root)
    {

    if(root!=NULL)
    {
          inorder(root->left);
          printf("%d ",root->data);
          inorder(root->right);
    }

    }
    void preorder(node *root)
    {

    if(root!=NULL)
    {
         printf("%d ",root->data);
         preorder(root->left);
         preorder(root->right);
    }

    }
    void postorder(node *root)
    {
    if(root!=NULL)
    {

    postorder(root->left);
    postorder(root->right);
    printf("%d ",root->data);
    }

    }
    node *getnode()
    {

    int val;
    node *temp;
    printf("\nEnter data:");
    scanf("%d",&val);
    temp=(node *)malloc(sizeof(node));
    temp->data=val;
    temp->right=NULL;
    temp->left=NULL;
    return temp;

    }
    node *insert(node *root)
    {

    char ch;
    node *neww;
    if(root==NULL)
    {
         neww=getnode();
         root=neww;
    }
    printf("\n\Ok now next node where to add?(L/R):");
    scanf("\n%c",&ch);
    if(ch=='l' || ch=='L')
    {
         neww=getnode();
         if(root->left==NULL)
         {
               root->left=neww;
         }
         else
         {
               insert(root->left);
         }
    }
    else if(ch=='r' || ch=='R')
    {
          neww=getnode();
          if(root->right==NULL)
          {
                root->right=neww;
          }
          else
          {
               insert(root->right);
          }
    }
    else
    {
          printf("\nEnter proper choice");
    }
    return root;

    }
    void main()
    {

    int ch;
    node *s=NULL;
    clrscr();
    while(1)
    {
    printf("\n1.insert\n2.inorder\n3.preorder\n4.postorder\n5.exit\n");
    scanf("%d",&ch);
    switch(ch)
    {
    case 1:s=insert(s);
    break;

    case 2:inorder(s);
    break;

    case 3:preorder(s);
    break;

    case 4:postorder(s);
    break;

    case 5:exit(0);
    break;

    default:printf("\nEnter proper choice\n");
    }
    }

    }

Priority Queue

/* Priority Queue */

    #include<stdio.h>
    #include<conio.h>
    #define size 10
    struct queue
    {

    int Q[size];
    int front,rear;

    }q;
    int full()
    {

    if(q.rear==size-1)
    {
         return 1;
    }
    else
    {
         return 0;
    }

    }
    int empty()
    {

    if(q.front>q.rear)
    {
         return 1;
    }
    else
    {
         return 0;
    }

    }
    void insert(int x)
    {

    int j;
    j=q.rear;
    ++q.rear;
    while(j>=0 && q.Q[j]>=x)
    {
         q.Q[j+1]=q.Q[j];
         j--;
    }
    q.Q[j+1]=x;

    }
    void del()
    {

         int x;
         x=q.Q[q.front];
         q.front++;
    }

    void display()
    {

    int i;
    for(i=q.front;i<=q.rear;i++)
    {
         printf("%d ",q.Q[i]);
    }

    }
    void main()
    {

    int ch,val;
    q.rear=-1;
    q.front=0;
    clrscr();
    while(1)
    {
         printf("\n1.insert\n2.delete\n3.display\n4.exit:");
         scanf("%d",&ch);

         switch(ch)
         {
              case 1:if(full())
              {
                    printf("\nFULL!");
              }
              else
              {
                    printf("\nEnter data");
                    scanf("%d",&val);
                    insert(val);
              }
              break;

              case 2:if(empty())
              {
                     printf("\Aalready empty\n");
              }
              else
              del();
              break;

              case 3:
              if(empty())
              {
                    printf("\nalready empty\n");
              }
              else
              display();
              break;

              case 4:exit(0);
              break;

             default:printf("\nproper choice plz!\n");
        }
    }

    }

Doubly Linked List

/*Doubly Linked List*/

    #include<stdio.h>
    #include<conio.h>
    typedef struct dll{

    int data;
    struct dll *next;
    struct dll *prev;

    }node;

    node *getnode()
    {

    int item;
    node *temp;
    temp=(node *)malloc(sizeof(node));
    printf("\nenter data:");
    scanf("%d",&item);
    temp->data=item;
    temp->next=NULL;
    temp->prev=NULL;
    return temp;

    }

    node *create()
    {

    int ch;
    node *trv,*start,*neww;
    start=trv=NULL;

    do
    {
         neww=getnode();
         if(start==NULL)
         {
              start=neww;
              trv=start;
         }
         else
         {
              trv->next=neww;
              neww->prev=trv;
              trv=trv->next;
         }

         printf("\nEnter any key to continue & 0 for stop:");
         scanf("%d",&ch);
    }while(ch!=0);
    return start;

    }
    node *insert_first(node *start)
    {

    node *trv,*neww;
    trv=start;
    neww=getnode();
    if(start==NULL)
    {
         start=neww;
    }
    else
    {
         neww->next=start;
         start->prev=neww;
         start=neww;
    }
    return start;

    }

    node *insert_last(node *start)
    {

    node *trv,*neww;
    trv=start;
    neww=getnode();
    if(start==NULL)
    {
         start=neww;
    }
    else
    {
         while(trv->next!=NULL)
         {
              trv=trv->next;
         }
         trv->next=neww;
         neww->prev=trv;
    }
    return start;

    }
    node *del_first(node *start)
    {

    node *trv;
    trv=start;
    start=start->next;
    start->prev=NULL;
    free(trv);
    return start;

    }
    node *del_last(node *start)
    {

    node *trv,*p;
    trv=start;
    if(start==NULL)
    {
         printf("\nEmpty\n");
    }
    else if(start->next==NULL)
    {
         free(start);
         start=NULL;
    }
    else
    {
         while(trv->next!=NULL)
         {
              p=trv;
              trv=trv->next;
         }
    }
    p=trv->prev;
    p->next=NULL;
    free(trv);
    return start;

    }
    void display(node *start)
    {

    node *trv;
    trv=start;
    if(start==NULL)
    {
         printf("\nNo node to display\n");
    }
    printf("\n");
    while(trv!=NULL)
    {
         printf("%d ",trv->data);
         trv=trv->next;
    }

    }

    void main()
    {

    node *f=NULL,*l=NULL;
    node *s=NULL;
    int ch;
    clrscr();
    while(1)
    {
    printf("\n1.Create\n2.Insert at front\n3.Insert at last\n4.Delete at front\n5.Delete at last\n6.Display\n10.exit\nYour choice:");
    scanf("%d",&ch);

         switch(ch)
         {
         case 1:s=create();
         break;

         case 2:s=insert_first(s);
         break;

         case 3:s=insert_last(s);
         break;

         case 4:s=del_first(s);
         break;

         case 5:s=del_last(s);
         break;

         case 6:display(s);
         break;

         case 10:exit(0);
         break;

         default:printf("\nEnter proper choice\n");
         }
    }

    }

Circular Linked List

/* Circular Linked List*/

    #include<stdio.h>
    #include<conio.h>

    typedef struct cll{

    int data;
    struct cll *next;

    }node;

    node *getnode()
    {

    node *temp;
    int item;
    printf("\nEnter data:");
    scanf("%d",&item);
    temp=(node *)malloc(sizeof(node));
    temp->data=item;
    temp->next=NULL;
    return temp;

    }

    node *create()
    {

    int ch;
    node *start,*neww,*trv;
    start=NULL;
    trv=NULL;
    do
    {
         neww=getnode();
         if(start==NULL)
         {
              neww->next=neww;
              start=neww;
              trv=start;
          }
          else
          {
               trv->next=neww;
               neww->next=start;
               trv=trv->next;
          }

    printf("\nEnter any key to add & 0 for stop:");
    scanf("%d",&ch);

    }
    while(ch!=0);
    return start;

    }

    node *insert_first(node *start)
    {

    node *trv,*neww;
    trv=start;
    neww=getnode();

    if(start==NULL)
    {
         start=neww;
         neww->next=neww;
    }
    else
    {
         while(trv->next!=start)
         {
              trv=trv->next;
         }
         trv->next=neww;
         neww->next=start;
         start=neww;
         }
     return start;

    }
    void insert_last(node *start)
    {

    node *trv,*neww;
    trv=start;
    neww=getnode();
    if(start==NULL)
    {
          neww=start;
          neww->next=neww;
    }
    else
    {
         while(trv->next!=start)
         {
              trv=trv->next;
         }
         trv->next=neww;
         neww->next=start;
    }

    }
    node *del_first(node *start)
    {

    node *trv;
    trv=start;
    if(start==NULL)
    {
         printf("\n\Empty\n");
    }
    else if(start->next==start)
    {
         start->next=NULL;
         free(start);
         start=NULL;
    }
    else
    {
          while(trv->next!=start)
          {
              trv=trv->next;
          }
          trv->next=start->next;
          trv=start;
          start=start->next;
          free(trv);
    }
    return start;

    }
    node *del_last(node *start)
    {

    node *trv,*temp;
    trv=start;
    if(start==NULL)
    {
         printf("\n\Empty\n");
    }
    else if(start->next==start)
    {
          start->next=NULL;
          free(start);
          start=NULL;
    }
    else
    {
          while((trv->next)->next!=start)
          {
               trv=trv->next;
          }
          temp=trv->next;
          trv->next=start;
          free(temp);
    }
    return start;

    }
    void display(node *start)
    {

    node *trv;
    trv=start;
    if(start==NULL)
    {
        printf("\nEmpty !\n");
    }
    printf("\n%d ",trv->data);
    trv=trv->next;

    while(trv!=start)
    {
          printf("%d ",trv->data);
          trv=trv->next;
    }

    }

    void main()
    {

    int ch;
    node *s=NULL;
    clrscr();
    while(1)
    {
         printf("\n1.Create\n2.insert_first\n3.insert_last\n4.delete           first\n5.delete_last\n6.Display\n10.exit\nYour choice:");
         scanf("%d",&ch);

         switch(ch)
         {
              case 1:s=create();
              break;

              case 2:s=insert_first(s);
              break;

              case 3:insert_last(s);
              break;

              case 4:s=del_first(s);
              break;

              case 5:s=del_last(s);
              break;

              case 6:display(s);
              break;

              case 10:exit(0);
              break;
    
              default:printf("\nEnter proper choice\n");
         }
    }

    }

Linked List


/* Linked List all operations */

    #include<stdio.h>
    #include<conio.h>

    typedef struct ll
    {

         struct ll *next;
         int data;

    }node;

    node *getnode()
    {

         int item;
         node *temp;
         temp=(node *)malloc(sizeof(node));
         printf("\nenter data:");
         scanf("%d",&item);
         temp->data=item;
         temp->next=NULL;
         return temp;

    }

    node *create()
    {

         int ch;
         node *trv,*start,*neww;
         start=trv=NULL;
         do
         {
              neww=getnode();
              if(start==NULL)
              {
                   start=neww;
                   trv=start;
              }
              else
              {
                   trv->next=neww;
                   trv=trv->next;
              }
              printf("\nEnter any key to continue & 0 for stop:");
              scanf("%d",&ch);
         }
         while(ch!=0);
         return start;

    }

    node *insert_before_val(node *start)
    {

    node *neww,*trv,*prv;
    int val;
    if(start==NULL)
    {
         printf("you should atleast create link list");
    }
    else
    {
         trv=start;
         printf("\nEnter value before you want to add new value:");
         scanf("%d",&val);
         while(trv->data!=val && trv!=NULL)
         {
              prv=trv;
              trv=trv->next;
         }
         if(trv==NULL)
         {
              printf("make sure that %d is there",val);
         }
         else
         {
              neww=getnode();
              neww->next=prv->next;
              prv->next=neww;
         }
    }

    }

    int search(node *start,int ele)
    {

    int pos;
    node *trv;
    pos=0;
    trv=start;
    while(trv!=NULL)
    {
         pos++;
         if(trv->data==ele)
         {
              break;
         }
         trv=trv->next;
    }
    return pos;

    }

    int count(node *start)
    {

    int c=0;
    node *trv;
    trv=start;
    while(trv!=NULL)
    {
          c++;
          trv=trv->next;
    }
    return c;

    }

    void insert_after_val(node *start)
    {
    node *trv,*neww;
    int val;
    trv=start;
    if(start==NULL)
    {
    printf(“atleast create a node”);
    }
    else
    {
    printf(“\nEnter value after you want to add element\n”);
    scanf(“%d”,&val);
    while(trv->data!=val && trv!=NULL)
    {
    trv=trv->next;
    }
    if(trv==NULL)
    {
    printf(“\nmake sure %d is there\n”,val);
    }
    else
    {
    neww=getnode();
    neww->next=trv->next;
    trv->next=neww;
    }
    }

    }
    node *insert_first(node *start)
    {

    node *neww;
    neww=getnode();
    if(start==NULL)
    {
         neww=start;
    }
    else
    {
         neww->next=start;
         start=neww;
    }
    return start;

    }
    node *insert_last(node *start)
    {
    node *neww,*trv;
    neww=getnode();
    if(start==NULL)
    {
    start=neww;
    }
    else
    {
    trv=start;
    while(trv->next!=NULL)
    {
    trv=trv->next;
    }
    trv->next=neww;
    }
    return start;
    }
    node *del_first(node *start)
    {
    node *trv;
    trv=start;
    if(start==NULL)
    {
    printf(“\nAlready empty\n”);
    }
    printf(“\n%d is deleted\n”,start->data);
    start=start->next;
    free(trv);
    return start;
    }
    void *del_last(node *start)
    {
    node *trv,*prv;
    trv=start;
    if(start==NULL)
    {
    printf(“\nEmpty!\n”);
    }
    else
    {

    while(trv->next!=NULL)
    {
    prv=trv;
    trv=trv->next;
    }
    printf(“%d is deleted”,trv->data);
    prv->next=NULL;
    free(trv);
    }
    }
    void display(node *start)
    {
    node *trv;
    trv=start;
    if(start==NULL)
    {
    printf(“\nNo node to display\n”);
    }
    printf(“\n”);
    while(trv!=NULL)
    {
    printf(“%d “,trv->data);
    trv=trv->next;
    }

    }
    void main()
    {
    int ch,c,p,ele;
    node *s=NULL;
    clrscr();
    while(1)
    {
    printf(“\n\n1.Create\n2.Insert front\n3.insert last\n4.Delete first\n5.Delete last\n6.display\n7.insert before value\n8.insert after val\n9.count\n10.search\n15.exit:”);
    scanf(“%d”,&ch);

    switch(ch)
    {
    case 1:s=create();
    break;

    case 2:s=insert_first(s);
    break;

    case 3:s=insert_last(s);
    break;

    case 4:s=del_first(s);
    break;

    case 5:del_last(s);
    break;

    case 6:display(s);
    break;

    case 7:insert_before_val(s);
    break;

    case 8:insert_after_val(s);
    break;

    case 9:c=count(s);
    printf(“\ntotal node are %d\n”,c);
    break;

    case 10:
    printf(“\nEnter element to search:”);
    scanf(“%d”,&ele);
    p=search(s,ele);
    printf(“%d found at %d position”,ele,p);
    break;

    case 15:exit(0);
    break;

    default:printf(“\nEnter proper choice\n”);
    }
    }
    }

Circular Queue

/*Circular Queue -*/

    #include<stdio.h>
    #include<conio.h>
    # define size 5

    struct queue
    {

         int rear,front,Q[size];

    }q;

    void insert(int item)
    {

        
    if ((q.front == 0 && q.rear == size-1) || (q.front == q.rear+1))
    {
         printf("\nQueue Overflow\n");
         return;
    }

    

        
    if (q.front == -1)   
    {
         q.front = 0;
         q.rear = 0;
    }
    else
    {
         if (q.rear == size - 1)
              q.rear = 0;
         else
              q.rear = q.rear + 1;
    } 
    q.Q[q.rear] = item ;

    }

    void del()
    {

    if (q.front == -1)
    {
         printf("Queue Underflow\n");
         return ;
    }

    printf("\nDeleted element is %d\n",q.Q[q.front]);

    if (q.front == q.rear)
    {
         q.front = -1;
         q.rear = -1;
    }

    else
    {

         if (q.front == size - 1)
              q.front = 0;
         else
              q.front = q.front + 1;
    }

    }

    void display()
    {

        
    int i = q.front, j=q.rear;

    if (q.front == -1)    
    {
         printf("Queue is empty");
         return;
    }

    printf("\nQueue is :\n");

    if (i <= j)
    {
         while (i <= j)
         {
              printf("%d ",q.Q[i]);
              i++;
         }
    }
    else
    {
         while (i <= size - 1)
         {
              printf("%d ",q.Q[i]);
              i++;
         }

         i = 0;
         while (i <= j)
         {
              printf("%d ",q.Q[i]);
              i++;
         }
    }

    }
    void main()
    {

    
    int choice,data;
    q.front=-1;
    q.rear=-1;

    while(1)
    {
         printf("\n1.Insert\n2.Delete\n3.display\n4.exit\n");
         scanf("%d",&choice);

         switch(choice)
         {
              case 1:printf("\nEnter data\n");
              scanf("%d",&data);
              insert(data);
              break;

              case 2:del();
              break;

              case 3:display();
              break;

              case 4:exit(0);
              break;

              default:printf("\nEnter proper choice\n");
    }

    }
    getch();
    }

Simple Queue using array

/* Simple Queue using array */

    #include<stdio.h>
    #include<conio.h>
    #define size 5

    struct Queue{

    int rear,front,Q[size];

    }q;

    int qfull()
    {

    if(q.rear>=size-1)
    {
    return 1;
    }
    else
    {
    return 0;
    }

    }

    int qempty()
    {

    if(q.front==-1 || q.front>q.rear)
    {
    return 1;
    }
    else
    {
    return 0;
    }

    }

    void insert(int x)
    {

    if(q.front==-1)
    {
    q.front++;
    }
    q.rear++;
    q.Q[q.rear]=x;

    }

    int del()
    {

    int i;
    i=q.Q[q.front];
    q.front++;
    return i;

    }

    void display()
    {

    int i;
    printf("\n");
    for(i=q.front;i<=q.rear;i++)
    {
    printf("%d ",q.Q[i]);
    }
    printf("\n");

    }

    void main()
    {

    int choice,value,d;
    q.front=-1;
    q.rear=-1;

    while(1)
    {
    printf("\n1.Insert\n2.delete\n3.display\n4.exit\nEnter your choice:\n");
    scanf("%d",&choice);

    switch(choice)
    {

    case 1:if(qfull())
    {
    printf("\nQueue is full\n");
    }
    else
    {
    printf("\nEnter data\n");
    scanf("%d",&value);
    insert(value);
    }
    break;

    case 2:if(qempty())
    {
    printf("\n\Queue is empty can't delete\n");
    }
    else
    {
    d=del();
    printf("\n\deleted element is %d\n",d);
    }
    break;

    case 3:if(qempty())
    {
    printf("\nAdd at least one item\n");
    }
    else
    {
    display();
    }
    break;

    case 4:exit(0);
    break;

    default:printf("\nEnter proper choice\n");
    }

    }

    }

Stack Using Array

/* Stack using array*/

    #include<stdio.h>
    #define size 5

    struct stack
    {

    int top,S[size];

    }st;

    int stfull()
    {

    if(st.top==size-1)
    {
         return 1;
    }
    else
    {
         return  0;
    }

    }

    int stempty()
    {

    if(st.top==-1)
    {
         return 1;
    }
    else
    {
         return 0;
    }

    }

    void push(int value)
    {

    st.top++;
    st.S[st.top]=value;

    }

    void pop()
    {

    int r;
    r=st.S[st.top];
    st.top--;

    }

    void peep()
    {

    printf("\nTop element is %d\n",st.S[st.top]);

    }

    void display()
    {

    int i;
    printf("\n");
    for(i=st.top;i>=0;i--)
    {
         printf("%d\n",st.S[i]);
    }

    }

    void main()
    {

    int choice,data;
    st.top=-1;

    while(1)
    {
    printf("\n1.Push\n2.Pop\n3.Peep\n4.Display\n5.Exit\nYour choice:");
    scanf("\n%d",&choice);

    switch(choice)
    {
    case 1:
    if(stfull())
    {
         printf("\nStack is Full\n");
    }
    else
    {
         printf("\nEnter value:");
         scanf("%d",&data);
         push(data);
    }
    break;

    case 2:
    if(stempty())
    {
         printf("\nStack is Empty\n");
    }
    else
    {
         pop();
    }
    break;

    case 3:peep();
           break;

    case 4:if(stempty())
    {
         printf("\nAdd at least one element\n");
    }
    else
    {
         display();
    }
    break;

    case 5:exit(0);
           break;

    default:printf("\nEnter proper choice\n");
    }
    }

    }

Binary Search

/* Binary Search */

    #include<string.h>
    #include<stdio.h>

    void main()
    {

    int i,n,first,last,middle,search,a[10];
    clrscr();
    printf("\nEnter total number of elements");
    scanf("%d",&n);

    for(i=0;i<n;i++)
    {
         printf("Enter a[%d]:",i);
         scanf("%d",&a[i]);
    }

    printf("\nEnter ele to search");
    scanf("%d",&search);

    first=0;
    last=n-1;
    while(first<=last)
    {
         middle=(first+last)/2;
         if(a[middle]==search)
         {
              printf("%d found at %d",search,middle+1);
              break;
         }
         else if(a[middle]>search)
         {
              last=middle-1;
         }
         else
         {
              first=middle+1;
         }
    }

    if(first>last)
    {
         printf("Not found");
    }
    getch();

    }

Quick Sort

/* Quick Sort */

    #include<stdio.h>
    #include<conio.h>
    void quicksort(int [],int,int);
    void main()
    {

    int a[10],n,i;
    printf("\nEnter size of array");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
         printf("\nEnter a[%d]",i);
         scanf("%d",&a[i]);
    }
    quicksort(a,0,n-1);
    printf("\nSorted array is:\n");
    for(i=0;i<n;i++)
    {
         printf("%d ",a[i]);
    }

    }
    void quicksort(int x[10],int first,int last)
    {

    int pivot,i,j,temp;
    if(first<last)
    {
         pivot=first;
         i=first;
         j=last;

    while(i<j)
    {
         while(x[i]<=x[pivot] && i<last)
         i++;

         while(x[j]>x[pivot])
         j--;

         if(i<j)
         {
              temp=x[i];
              x[i]=x[j];
              x[j]=temp;
         }
    }
    temp=x[pivot];
    x[pivot]=x[j];
    x[j]=temp;
    quicksort(x,first,j-1);
    quicksort(x,j+1,last);
    }

    }

Merge Sort


    #include<stdio.h>
    #include<conio.h>
    void partition(int [],int,int);
    void merge_sort(int [],int,int,int);
    void main()
    {

    int a[10],i,n;
    clrscr();
    printf("\nEnter no of elements:");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
         printf("\nEnter a[%d]",i);
         scanf("%d",&a[i]);
    }
    partition(a,0,n-1);
    for(i=0;i<n;i++)
    {
         printf("%d ",a[i]);
    }
    getch();

    }

    void partition(int a[],int low,int high)
    {

    int mid;
    if(low<high)
    {
         mid=(low+high)/2;
         partition(a,low,mid);
         partition(a,mid+1,high);
         merge_sort(a,low,mid,high);
    }

    }

    void merge_sort(int a[],int low,int mid,int high)
    {

    int temp[10];
    int i,j,k,m;
    m=mid+1;

    for(i=low;j<=mid && m<=high;i++)
    {
         if(a[j]<=a[m])
         {
              temp[i]=a[j];
              j++;
         }
         else
         {
              temp[i]=a[m];
              m++;
         }
    }
    if(j>mid)
    {
         for(k=m;k<=high;k++)
         {
              temp[i]=a[k];
              i++;
         }
    }
    else
    {
         for(k=j;k<=mid;k++)
         {
               temp[i]=a[k];
               i++;
         }
    }
    for(k=low;k<=high;k++)
    {
         a[k]=temp[k];
    }

    }

Insertion Sort


    #include<stdio.h>
    #include<conio.h>

    void main()
    {

    int i,j,temp,arr[10],n;
    clrscr();
    printf("\nEnter size of ayyar");
    scanf("%d",&n);

    for(i=0;i<n;i++)
    {
         scanf("%d",&arr[i]);
    }

    for (i = 1; i < n; i++)
    {
         int tmp = arr[i];
         int j;
         for (j = i; j > 0; j--)
         {
              if (arr[j - 1] < tmp)
                   break;
              arr[j] = arr[j - 1];
         }
         arr[j] = tmp;
    }

    printf("\nSorted array is \n");
    for(i=0;i<n;i++)
    {
         printf("%d\n",arr[i]);
    }
    getch();

    }

Selection Sort



    #include<stdio.h>
    #include<conio.h>

    void main()
    {

    int i,j,min,n,a[6],temp;
    clrscr();
    printf("Enter number");
    scanf("%d",&n);

    for(i=0;i<n;i++)
    {
         scanf("%d",&a[i]);
    }
    printf("\nSorted List is:\n");

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

    for(i=0;i<n;i++)
    {
         printf("\n%d",a[i]);
    }
    getch();

    }

Bubble Sort



   #include<stdio.h>

    void main()
    {

    int i,j,n,temp,a[10];
    printf("\nEnter n\n");
    scanf("%d",&n);

    for(i=0;i<n;i++)
    {
    printf("Enter a[%d]",i);
    scanf("%d",&a[i]);
    }

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

    printf("\nSorted ayyay using bubble sort is\n");
    for (i = 0; i < n; i++)
    {
    printf("\n%d", a[i]);
    }

    }