Docstoc

Data structure lab man-veltech-3rd sem

Document Sample
Data structure lab man-veltech-3rd sem Powered By Docstoc
					    DEPARTMENT OF CSE




        LAB MANUAL
DATA STRUCTURES LABORATORY




                     Prepared By


                   T.SENTHIL MURUGAN
                     ASST PROF / CSE
                     VELTECH
U3CSA05                   DATA STRUCTURES LAB



List of Experiments

USING ‘C’

1. Implementation of Stack using Array

2.Implementation of Queue using Array

3.Implementation of linked list

4.Implementation of stack using linked list

5.Infix to postfix conversion

6.Evaluation of postfix expression

7.Implementation of Binary Search Tree

8.Implementation of Breadth First Search and Depth First Search



USING ‘C++’

9.Insertion Sort and Bubble Sort

10.Heap Sort

11.Quick Sort

12.Linear and Binary Search.
EX.NO:1                   IMPLEMENTATION OF STACK USING ARRAY

AIM:

          To write a program using c language to implement the STACK using arrays.

ALOGORITHM:

Step 1: Start the Program.

Step 2: Initialize the value of top = 1, c,a[10] as integer.

Step 3: Then print the stack using array.

Step 4: Print the value of PUSH operation, POP operation, and DISPLAY operation. and EXIT operation.

Step 5: Select your choice in user screen.

Step 6: Then perform switch operation then is cursor. if top is greater or equal to the print stack is
overflow as a top is incremented by 1,then print the element..

Step 7: In case of top is equal to then print stack is empty else delete the element present in stack then top
is decremented by 1.

Step 8: In the case of top is equal to -1 then print stack is empty else displays the present element in the
stack for lops.

Step 9: Increments for exist is entered

Step 10: End of the program.
                                               PROGRAM

#include<stdio.h>

#include<conio.h>

# define max 3

typedef int stackvalues;

stackvalues top,status,stack[max];

void main()

{

stackvalues item,choice;

top=-1;

while(1)

{

clrscr();

printf("\n 1-PUSH \n 2-POP \n 3-QUIT");

printf("\n Enter the choice");

scanf("\n %d", &choice);

switch(choice)

{

case 1:

printf("\n Enter the element to be pushed");

scanf ("\n %d", & item);

push (stack, item);

if(status)

{

printf("\n AFTER PUSHING");

display(status);

if(top==(max-1))

printf("\n STACK IS FULL");
}

     else

     printf("\n STACK IS OVERFLOW");

     getch();

     break;

    case

     item=pop(stack);

      if(status)

     {

       printf("\n popped item is %d", item);

    display(stack);

         }

             Else

         printf("\n STACK UNDERFLOW");

       getch()

       break;

             case 3:

             exit(0);

              }

              }

                  getch();

               }

                   push(stackvalues stack[],stackvalues item)

                   {

                        if(top==(max-1))

                        status=0;

                        else

                         {

                             status=1;
               top++;

               stack[top]=item;

          }

      }

      stackvalues pop(stackvalues stack[])

          {

              stackvalues ret;

              if(top==-1)

  {

                    ret=0;

                    status=0;

                }

              else

               {

                    status=1;

                    ret=stack[top];

                    top--;

                   }

                return(ret);

          }

      display()

      {

stackvalues i;

if(top==-1)

printf("\n STACK IS EMPTY");

else

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

printf("\n %d is in %d\t",stack[i],i);

  }
                    OUTPUT
     1-push

     2-pop

     3-quit

PUSH OPERATION

     Enter the choice:1

     Enter the element to be pushed 10

     AFTER PUSHING

    10 is in 0

    Enter the choice :1

    Enter the element to be pushed 20

    AFTER PUSHING

     20 is in 1

     10 is in 0

     Enter the choice:1

     Enter the element to be pushed 30

     AFTER PUSHING

     30 is in 2

     20 is in 1

     10 is in 0

     STACK FULL

     Enter the choice:1

     Enter the element to be pushed 40

     STACK OVERFLOW

     POP OPERATION

     Enter the choice:2
            popped item is 30

             20 is in 1

             10 is in 0

            Enter the choice:2

            popped item is 20

            10 is in 0

            Enter the choice:2

            popped item is 10

            STACK EMPTY

           Enter the choice:2

           STACK UNDERFLOW




RESULT:

      Thus the implement the stack using array program was executed and output was verified
EX.NO: 2                  QUEUE IMPLEMENTATION USING ARRAY

AIM:

         To write a C program for the queue implementation using array.

ALGORITHM:

Step 1: Start the program.

Step 2: Get the choice using a VARIABLE

Step 3: Using a control structure call function required to process the data.

Step 4: If size of array full then print that queue is full. Else insert the element

Step 5: To delete an element from an array check for over empty condition if not delete the first inserted
element.

Step 6: choice is 3 used to display the element in the queue. Choose the 4th option to end the program.

Step 7: End the program.
                                         PROGRAM

#include <stdio.h>

#include <conio.h>

typedef int qv

qv rear=0,front=0,queue[3],f=0,max=3;

main()

{

    qv ch;

 clrscr();

    while(1)

    {

        clrscr();

        printf("\n 1:Addqueue \n 2:Dequeue \n 3:Display \n 4:Exit");

        printf("Enter your choice:");

         scanf("\n %d",&ch);

        switch(ch)

        {

            case 1:

              addqueue();

             break;

        case 2:

         dequeue();

            break;

                  case 3:

                     disp();

                     break;

                      case 4:

                exit(0);
                }

    }

getch();

}

    addqueue()

                          {

            qv no, i;

            if(rear==max)

            {

                     printf("\n QUEUE IS FULL");

                            }

    else

        {

                    printf("\n Enter the item to be inserted:");

                scanf("\n %d",&no);

                    queue[rear]=no;

                for(i=front; i<=rear,i++)

                     {

                         printf("\n Item %d is inserted into queue",queue[i]);

                    }

                    rear++;

                f=0;

            }

    getch();

    return(0);

}

        del()

        {

                    qv item;
            if(front==rear)

            {

                    printf("\n QUEUE IS EMPTY");

                    f=1;

            }

        else

                           {

            item=queue[front];

                front++;

                max++;

            printf("\n ITEM TO BE DELETED IS %d", item);

        }

    getch();

    return(0);

}

disp()

    {

            qv i;

        if(f==1)

                {

                    rear=max+3;

                     front=3;

                    }

     if(front==rear)

        printf("\n QUEUE IS EMPTY");

    else

            {

                    for(i=front; i<rear; i++)

                {
        printf("\n %d",queue[i]);

        }

    }

    getch();

return(0);

}
               OUTPUT
   1:Addqueue

   2:Dequeue

   3:Display

   4:Exit

ADDQUEUE

Enter your choice:1

Enter the item to be inserted:10

Item 10 is inserted into queue

Enter your choice:1

Enter the item to be inserted:20

Item 10 is inserted into queue

Item 20 is inserted into queue

Enter your choice:1

Enter the item to be inserted: 30

Item 10 is inserted into queue

Item 20 is inserted into queue

Item 30 is inserted into queue

Enter your choice:1

 QUEUE IS FULL

DISPLAY

   10

   20

   30
            DEQUEUE

              Enter your choice:2

              Item to be deleted is 10

              Enter your choice:2

              Item to be deleted is 20

              Enter your choice:2

             Item to be deleted is 30

             Enter your choice:2

             QUEUE IS EMPTY




RESULT:

    Thus the program Implementation queue using array’s implemented and output is verified.
EX.NO:3              IMPLEMENTATION OF SINGLY LINKED LIST

Aim
       To implement the singly linked list using C- program

Algorithm

   1. Create a list of elements one linked to the other using the create function
   2. To insert any new element to the list, we have to get the position where to insert it and do the
      insertion using insert function.
   3. After creating the list, if we have to delete any element we can do it, by getting the position to be
      deleted
   4. Display the contents using display function
   5. Exit the program

Program

          #include<stdio.h>

          #include<conio.h>

          typedef int list;

          struct node

          {

                  list data;

                  struct node *link;

           } *p;

           list n,c;

           void append();

           void del();

           void disp();

           void insert();



           void main()

              {

              list choice;
clrscr();

do

{

     clrscr();

     printf("\n1.Addnode\n 2.Deletenode\n 3.Insertnode\n 4.Display\n 5.Exit");

     printf("\nEnter the choice:-");

     scanf("%d",&choice);

    switch(choice)

      {

          case 1:

             append();

             break;

              case 2:

              del();

              break;

                         case 3:

             insert();

             break;



      case 4:

             disp();

             break;




       case 5:

             exit(0);
          }

      } while(choice<5);

     getch();

 }



void append()

 {

      struct node *q,*t;

      printf("\n Enter the Data to add");

      scanf("\n %d",&n);

      if(p==0)

      {

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

          p->data=n;

          p->link=0;

      }

     else

      {

          q=p;

          while(q->link!=0)

          q=q->link;

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

          t->data=n;

          t->link=0;

          q->link=t;

      }
    getch();

}

void del()

    {

        struct node *q,*r;

        q=p;

        printf("\n Enter the Data to be Deleted");

        scanf("\n %d",&n);

        if(q->data==n)

         {

             p=q->link;

             free(q);

             return;

         }

    while(q!=0)

    {

         if(q->data==n)

             {

                 r->link=q->link;

                 return;

             }

         r=q;

         q=q->link;

    }

        printf("\nELEMENT NOT FOUND");

        getch();
    }

 void insert()

    {

        struct node *q,*t,*r;

        list i;

        printf("\n Enter the position to be Inserted:");

        scanf("\n %d",&c);

        printf("\n Enter the Data:");

        scanf("\n %d",&n);

        for(i=0,q=p;i<c;i++)

         {

             q=q->link;

             if(q==0)

             printf("\n There are less then %d elements",q);

          }

        r=p;

    while(r->link!=q)

    r=r->link;

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

    t->data=n;



 t->link=r->link;

    r->link=t;

    getch();

}

void disp()
{

    struct node *q;

    for(q=p;q!=0;q=q->link)

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

    getch();

}
OUTPUT


   1.Addnode

   2.Deletenode

   3.Insertnode

   4.Display

   5.Exit



   ADDNODE

   Enter the choice:-1

   Enter the Data to add:10



   Enter the choice:-1

   Enter the Data to add:20



   Enter the choice:-1

   Enter the Data to add:30



   INSERTNODE



   Enter the choice:-3

   Enter the position to be Inserted:1

   Enter the Data: 100

   DISPLAY

   Enter the choice:-4

   10
              100

              20

              30

              DELETENODE

              Enter the choice:-2

              Enter the data to be Deleted:20

              10

              100

              30




Result

   Thus the C- Program to implement singly linked list was written and verified.
Ex.No: 4        IMPLEMENTATION OF STACK USING LINKED LIST

AIM:

        To write a C program for the stack using linked list.

ALGORITHM:

Step 1: Start the program.

Step 2: Declare the variable of different data type and read the size of the stack.

Step 3: Enter the choice and read the choice and by using switch case

Step 4: Declare the insertion values.

Step 5: Create a node using malloc function

Step 6: Read the new node and store the value as assign it as top pointer

Step 7: Declare the deletion process and delete the top value

Step 8: End the program
                                               PROGRAM

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

#include<process.h>

struct linked

{

        int data;

        struct linked *next;

};

typedef struct linked stack;

stack*top=0;

int size=sizeof(stack);

int push();

int pop();

int display();

void main()

{

        int ch;

        clrscr();

        while(1)

        {

                    printf("\n\t\t~~STACK IMPLEMENTATION USING LINKED LIST~~");

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

                    printf("\n\t2.POP");

                    printf("\n\t3.DISPLAY");
               printf("\n\t4.EXIT");

               printf("\n\tEnter your choice-->");

               scanf("%d",&ch);

               switch(ch)

               {

               case 1:

                          push();

                          break;

               case 2:

                          pop();

                          break;

               case 3:

                          display();

                          break;

               case 4:

                          exit(1);

               default:

                          printf("wrong choice\n");

               }

       }

}

int push(stack*new)

{

       new=(stack*)malloc(sizeof(stack));

       printf("\nEnter the element to be pushed-->");

       scanf("%d",&new->data);
        new->next=top;

        top=new;

        return(0);

}

int pop(stack*temp)

{

        if(top==0)

                 printf("\nSTACK IS UNDERFLOW");

        else

        {

                 temp=top;

                 top=top->next;

                 printf("Deleted element is %d",temp->data);

                 free(temp);

        }

        return(0);

}

int display(stack*p)

{

        p=top;

        if(p==0)

                 printf("\n The stack is empty");

        else

                 printf("\nthe Elements in the stack-->");

        {

                 do
            {

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

                    p=p->next;

            }while(p!=0);

    }

    return(0);

}




RESULT:

    Thus the program Implementation of stack using linked list is implemented and output is verified.
Ex.No:5                  INFIX TO POSTFIX CONVERSION

AIM:

To write a C program for convert the infix expression to postfix expression.

ALGORITHM:

Step 1: Declare a string variable to get the expression as infix

Step 2: By using while loop the expression is evaluated.

Step 3: POP the element if it goes through any operators.

Step 4: Based on the hierarchy of the operators.POP the elements out.

Step 5: POP the last two entered elements and display them in output screen.

Step 6: POP and PUSH functions are constructed using stack concept of last in and first out.
                                                   PROGRAM

#include<stdio.h>

#include<conio.h>

#include<malloc.h>

char inf[10],post[10];

int top=0,st[10];

void push(int);

char pop();

void main()

{

        int i,j=0;

        clrscr();

        printf("\n\n\nEnter the Infix expression:");

        scanf("%s",inf);

        for(i=0;inf[i]!='\0';i++)

        {

                    switch(inf[i])

                    {

                    case'+':

                               while(st[top]>=1)

                               post[j++]=pop();

                               push(1);

                               break;

                    case'-':

                               while(st[top]>=1)

                               post[j++]=pop();
                  push(2);

                  break;

       case'*':

                  while(st[top]>=3)

                  post[j++]=pop();

                  push(3);

                  break;

       case'/':

                  while(st[top]>=3)

                  post[j++]=pop();

                  push(4);

                  break;

       case'(':

                  push(0);

                  break;

       case')':

                  while(st[top]!=0)

                  post[j++]=pop();

                  top--;

                  break;

       default:

                  post[j++]=inf[i];

       }

}

while(top>0)

post[j++]=pop();
        printf("\n\t postfix expession is:\n\n\t%s", post);

        getch();

}

void push(int ele)

{

        top++;

        st[top]=ele;

}

char pop()

{

        int el;

        char e;

        el=st[top];

        top--;

        switch(el)

        {

        case 1:

                   e='+';

                   break;

        case 2:

                   e='-';

                   break;

        case 3:

                   e='*';

                   break;

        case 4:
            e='/';

            break;

    }

    return(e);

}




RESULT:

    Thus the program convert infix to postfix implemented and output is verified.
Ex.No:6                   EVALUATION OF POSTFIX EXPRESSION

AIM:

To write a C program for evaluate the postfix expression.

ALGORITHM:

Step 1: Declare a string variable, get the postfix expression in the variable.

Step 2: Read the operator one by one and implement the stack.

Step 3: Evaluate the expression using the hierarchy of operators.

Step 4: PUSH the elements when ever a integer is read.

Step 5: POP the elements whenever an operator is read.

Step 6: finally, get the value of the expression and print it.

Step 7: End the program.
                                                   PROGRAM

#include<math.h>

#include<stdio.h>

#include<conio.h>

#include<ctype.h>

#include<string.h>

#include<stdlib.h>

float stack[10];

int top=-1;

void push(char);

float pop();

float eval(char[],float[]);

void main()

{

        int i=0;

        char suffix[20];

        float value[20],result;

        clrscr();

        printf("\nEnter the valid postfix expression:");

        gets(suffix);

        while(suffix[i])

        {

                    if(isalpha(suffix[i]))

                    {

                            fflush(stdin);

                            printf("\nEnter the value of %c",suffix[i]);
                           scanf("%f",&value[i]);

                   }

                   i++;

        }

        result=eval(suffix,value);

        printf("The result of %s is %2f",suffix,result);

        getch();

}

float eval(char suffix[],float data[])

{

        int i=0;

        float op1,op2,res;

        char ch;

        while(suffix[i]!='\0')

        {

                   ch=suffix[i];

                   if(isalpha(suffix[i]))

                   {

                           push(data[i]);

                   }

                   else

                   {

                           op1=pop();

                           op2=pop();

                           switch(ch)

                           {
                      case'+':

                                 push(op1+op2);

                                 break;

                      case'-':

                                 push(op1-op2);

                                 break;

                      case'*':

                                 push(op1*op2);

                                 break;

                      case'/':

                                 push(op1/op2);

                                 break;

                      case'^':

                                 push(pow(op1,op2));

                                 break;

                      }

               }

               i++;

       }

       res=pop();

       return(res);

}



void push(char num)

{

       top=top+1;
        stack[top]=num;

}

float pop()

{

        float num;

        num=stack[top];

        top=top-1;

        return(num);

}




RESULT:

        Thus the program of evaluation of postfix expression was implemented and output is verified.
Ex.No:7                BINARY SEARCH TREE (BST) TRAVERSAL

AIM:

To write a C program to traverse the nodes in a binary search tree.

ALGORITHM:

Step 1: Declare a structure for creation of tree

Step 2: Read the node as allocate dynamic memory to the node.

Step 3: Insert the node using the condition of the binary search tree

Step 4: If the node is lesser than the root, insert the node as left of the BST. Other s the node in right

Step 5: Call the pre-order traversal as ROOT->LEFT_NODE-> RIGHT_NODE.

Step 6: Call the in-order traversal as LEFT_NODE-> ROOT -> RIGHT_NODE.

Step 7: Call the post-order traversal as LEFT_NODE-> RIGHT_NODE-> ROOT.

Step 8: End of the program.
                                                    PROGRAM

#include<stdio.h>

#include<conio.h>

#include<alloc.h>

typedef struct btree

{

          int data;

          struct btree *left,*right;

}*tree;

tree head;

void main()

{

          int ch=0,n,val;

          void create();

          void display();

          clrscr();

          printf("\n\n\t\t\t BINARY TREE TRAVERSALS\n");

          while(1)

          {

                      printf("\n\n 1-CREATE\t2-DIAPLAY\t3-EXIT");

                      printf("\n\nyour option ");

                      scanf("%d",&ch);

                      switch(ch)

                      {

                               case 1: create();

                                         break;

                               case 2: display();

                                         break;
                            case 3: exit(0);

                    }

         }

}

tree getnewnode()

{

         tree temp;

         printf("\n ENTER THE DATA:              ");

         temp=(tree)malloc(sizeof(struct btree));

         scanf("%d",&temp->data);

         temp->left=0;

         temp->right=0;

         return(temp);

}

void inorder(tree root)

{

         tree temp;

         temp=root;

         if(temp)

         {

                    inorder(temp->left);

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

                    inorder(temp->right);

         }

}

void preorder(tree root)

{

         tree temp;
           temp=root;

           if(temp)

           {

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

                      preorder(temp->left);

                      preorder(temp->right);

           }

}

void postorder(tree root)

{

           tree temp;

           temp=root;

if(temp)

           {

                      postorder(temp->left);

                      postorder(temp->right);

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

           }

}

void create()

{

           tree temp,newnode;

           int flag=0,n;

           do

           {

                      temp=head;

                      if(temp->left==NULL)

                      {
        temp->left=getnewnode();

        temp=temp->left;

}

else

        temp=temp->left;

        flag=0;

        newnode=getnewnode();

while(flag==0)

{

        if(newnode->data<temp->data)

        {

        if(temp->left==NULL)

        {

                  temp->left=newnode;

                  printf("\n%d is attached to the left of %d",

                  newnode->data,temp->data);

                  flag=1;

        }

        else

                  temp=temp->left;

        }

        else

        {

        if(temp->right==NULL)

        {

                  temp->right=newnode;

                  printf("%d is attached to right of %d\n",

                  newnode->data,temp->data);
                                   flag=1;

                           }

                           else

                           temp=temp->right;

                  }

         }

         printf("\n\n DO YOU WANT TO CONTINUE Y/N");

         }

         while(getch()=='Y');

}

void display()

{

         printf("\n\n IN ORDER     :");

         inorder(head->left);

         printf("\n\n PRE ORDER :");

         preorder(head->left);

         printf("\n\nPOST ORDER :");

         postorder(head->left);

}




Output
1.CREATE          2.DIAPLAY        3.EXIT

YOUR OPTION                1



ENTER THE DATA             :       10

ENTER THE DATA             :       5
      5 is attached to the left of 10

DO YOU WANT TO CONTINUE Y/N:Y

ENTER THE DATA           :          7

      7 is attached to right of 5

DO YOU WANT TO CONTINUE Y/N:Y

ENTER THE DATA           :          1

      1 is attached to the left of 5

DO YOU WANT TO CONTINUE Y/N:Y

ENTER THE DATA           :          18

      18 is attached to right of 10

DO YOU WANT TO CONTINUE Y/N:Y

ENTER THE DATA           :          4

      4 is attached to right of 1

DO YOU WANT TO CONTINUE Y/N:N

1.CREATE       2.DISPLAY            3.EXIT

YOUR OPTION: 2

IN ORDER       :         1          4        5   7   10   18

PRE ORDER      :         10         5        1   4   7    18

POST ORDER :             4          1        7   5   18   10

1.CREATE           2.DISPLAY            3.EXIT

YOUR OPTION : 3
RESULT:

        Thus the program of binary search tree traversal was implemented and output is verified.

EX.NO:8 IMPLEMENTATION OF BREADTH FIRST SEARCH & DEPTH FIRST SEARCH

AIM:

   To write a C program to implement breadth first search.

ALGORITHM:

Step 1: Start the program.

Step 2: Create the node in the program marked any one node on visited

Step 3: Using the adjacency matrix of the graph find all the unvisited adjacencynode to search node
Step 4: Then the node is queued from the queue and that node on visit and designed it on new search node

Step 5: Stop the program




                                              PROGRAM


#include<stdio.h>

#include<conio.h>

void searchfrom(int k);

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

void main()

{

        int i,j;

        clrscr();

        printf("\n\n\t\t BREADTH FIRST SEARCH");

        printf("\n\n Enter number of vertex        :");

        scanf("%d",&n);

        printf("\n\n Enter the adjacency matrix\n");

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

        {

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

                    {

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

                    }

        }

        printf("\n BREADTH FIRST SEARCH :");
        for(i=1;i<=n;i++)

                   if(visited[i]==0)

                            searchfrom(i);

        getch();

}

void searchfrom(int k)

{

        int i;

        printf("\n%d",k);

        visited [k]=1;

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

        {

                   if(visited[i]==0)

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

                            searchfrom(i);

                   break;

        }

}




OUTPUT:

                                       BREADTH FIRST SEARCH
Enter number of vertex                 :     5

Enter the adjacency matrix

0       1          1        1          0

1       0          0        0          1

1       0          0        0          0
1       0           0      0       1

0       1           0      1       0

BREADTH FIRST SEARCH               :

                    1

                    2

                    3

                    4

                    5




                                   /*DEPTH FIRST SEARCH:*/

PROGRAM:
#include<stdio.h>

#include<conio.h>

void searchfrom(int k);

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

void main()

{

        int i,j;

        clrscr();

        printf("\n\n\t\t DEPTH FIRST SEARCH");

        printf("\n\n Enter number of vertex      :");

        scanf("%d",&n);

        printf("\n\n Enter the adjacency matrix\n");

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

        {

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

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

                   }

        }

        printf("\n DEPTH FIRST SEARCH              :");

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

                   if(visited[i]==0)

                           searchfrom(i);

        getch();

}

void searchfrom(int k)

{

        int i;

        printf("\t%d",k);

        visited [k]=1;

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

                   if(visited[i]==0)

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

                           searchfrom(i);

}



OUTPUT:

                                            DEPTH FIRST SEARCH



Enter number of vertex                 :     5

Enter the adjacency matrix
0       1       1       1        0

1       0       0       0        1

1       0       0       0        0

1       0       0       0        1

0       1       0       1        0

DEPTH FIRST SEARCH:

                1

                2

                5

                4

                3




RESULT:

        Thus the program of BFS and DFS are implemented and outputs are verified.




EX.NO:9         BUBBLE SORT

AIM:

    To write a C++ program to sort the element using bubble sort technique.

ALGORITHM:

Step 1: Start the program.

Step 2: Get the element in the array in the main program
Step 3: Call the heap sort function

Step 4: Print the values of array

Step 5: Set a key value. Usually key value is first value of array

Step 6: Compare the key value with the other elements in the array and sort the elements

Step 7: Arrage the array with respect to the key value

Step 8: Return the array to main program

Step 9: Stop the program



PROGRAM:

#include<iostream.h>

#include<conio.h>

#include<stdlib.h>

const int MAX=10;

class array

{

private:

int arr[MAX];

int count;

public:

array();

void add(int item);

void sort();

void display();

};

// initialises data member

array::array()
{

count=0;

for(int i=0;i<MAX;i++)

arr[i]=0;

}



//add a new element to array

void array::add(int item)

{

if(count<MAX)

{

arr[count]=item;

count++;

}

else

cout<<"\narray is full"<<endl;

}

//sorts array using selection sort method

void array::sort()

{

int temp;

for(int i=0;i<=count-2;i++)

{

for(int j=i+1;j<=count-1;j++)

{

if(arr[i]>arr[j])
{

temp=arr[i];

arr[j]=arr[j];

arr[j]=temp;

}

}

}

}

//displays elements in array

void array::display()

{

for(int i=0;i<count;i++)

cout<<arr[i]<<"\t";

cout<<endl;

}

void main()

{

array a;

a.add(25);

a.add(17);

a.add(31);

a.add(13);

a.add(2);

cout<<"\nselection sort:\n";

cout<<"\narray before sort:"<<endl;

a.display();
a.sort();

cout<<"\narray after sort:"<<endl;

a.display();

}




                                     OUTPUT:

array before sort:

25 17 31 13 2



array after sort:

2 13 17 25 31
.




RESULT:

        Thus the program of bubble sort was implemented and output is verified.



EX.NO:10                                 HEAP SORT

AIM:

    To write a C++ program to sort the element using heap sort technique.

ALGORITHM:

Step 1: Start the program.

Step 2: Get the element in the array in the main program

 Step 3: Call the heap sort function

Step 4: Print the values of array
Step 5: Set a key value. Usually key value is first value of array

Step 6: Compare the key value with the other elements in the array and sort the elements

Step 7: Arrage the array with respect to the key value

Step 8: Return the array to main program

Step 9: Stop the program




                                                PROGRAM

#include <iostream>

#include <fstream>

#include <iomanip>

using namespace std;

class HeapSort

{

    private:
             int list[10];

     protected:

              void fixup(int, int, int);

              void buildheap();

     public:

             void heapsort();

             void store(int, int);

             void printHeap();



};



void

HeapSort::fixup(int value, int start, int last)

{

     int look;

     bool done;

     done = false;

     look = 2*start+1;



     while(look <= last && !done)

     {

         if(look < last)

         {

             if(list[look] < list[look+1])

                 look = look+1;

         }
        if(value > list[look])

            done = true;

        else

        {

            list[start] = list[look];

            start = look;

            look = 2*start+1;

        }

    }

}



void

HeapSort::buildheap()

{

    int count;

    int item;



    for(count = (10/2)-1; count >= 0; count--)

    {

        item = list[count];

        fixup(item, count, 9);

    }

}



void

HeapSort::heapsort()
{

    int count;

    int item;



    buildheap();

    for(count = 9; count >= 1; count--)

    {

        //cout << list[count] << endl;

        //cout <<"count " << count << endl;

        item = list[count];

        list[count] = list[0];

        fixup(item, 0, count-1);

        //cout << list[count] << endl;

    }

}



void

HeapSort::store(int number, int place)

{

    list[place] = number;

}



void

HeapSort::printHeap()

{

    for(int i = 0; i < 10; i++)
          cout << list[i] << endl;

}

int

main()

{

      HeapSort heap1;

      HeapSort heap2;



      int x = 0;

      int numberInput;



      cout << "Enter ten numbers: " << endl;



      while(x < 10)

      {

          cin >> numberInput;

          //heap1.heapsort();

          //cout << x;

          heap1.store(numberInput, x);

          x++;

      }



      x = 0;



      /*while(x < 10)

      {
         cin >> numberInput;

         heap2.store(numberInput, x);

         x++;

    }

    */

    heap1.heapsort();

    heap1.printHeap();

    //heap2.printHeap();

    // heap2.heapsort();

    //heap2.printHeap();



    system("pause");

}

OUTPUT:

array before sort:

50 60 70 20 10 30 40



array after sort:

10 20 30 40 50 60 70




RESULT:

           Thus the program of heap sort was implemented and output is verified.
EX.NO: 11                                 QUICK SORT

AIM:

   To write a C++ program to sort the element using quick sort technique.

ALGORITHM:

Step 1: Declare an array of size n.

Step 2: Read the number of elements to be added in array using for loop get the values.

Step 3: call the quick sort function in main program

Step 4: Assign a pivot value on which the element are to be sorted

Step 5: Use control statements and while loop to arrabge the elements in group based on pivot value

Step 6: To swap the values call the swap function in quick sort function

Step 7: To sort element to the left and right of pivot value call recursively the function of quick sort

Step 8: Return the array to main program

Step 9: Stop the program
                                                     PROGRAM



#include<iostream.h>

#include<conio.h>

#include<stdlib.h>

const int MAX=10;

class array

{

    private:

              int arr[MAX];

              int count;

    public:

              array();

              void add(int item);

              int getcount();

               static int split(int*,int,int);

               void quiksort(int lower,int upper);

               void display();

               };



               // initialises data member

               array::array()

               {

                   count=0;

                   for(int i=0;i<MAX;i++)

                         arr[i]=0;
}

void array::add(int item)

{

if(count<MAX)

{

arr[count]=item;

count++;

}

else

cout<<"array is full"<<endl;

}

 //returns total number of elements

int array::getcount()

{

return count;

}



// calls split to arrrange array

void array::quiksort(int lower,int upper)

{

if(upper>lower)

{

int i=split(arr,lower,upper);

quiksort(lower,i-1);

quiksort(i+1,upper);

}
}

//sorts array using quick sort

int array::split(int *a,int lower,int upper)

{

int p,q,t,i;

p=lower+1;

q=upper;

i=a[lower];

while(q>=p)

{

while(a[p]<i)

p++;

while(a[q]>i)

q--;

if(q>p)

{

t=a[p];

a[p]=a[q];

a[q]=t;

}

}

t=a[lower];

a[lower]=a[q];

a[q]=t;

return q;

}
//displays the elements of array

void array::display()

{

for(int i=0;i<count;i++)

cout<<arr[i]<<"\t";

cout<<endl;

}

void main()

{

array a;

a.add(11);

a.add(2);

a.add(9);

a.add(13);

a.add(57);

a.add(25);

a.add(17);

a.add(1);

a.add(90);

a.add(4);

cout<<"\nquick sort:\n";

cout<<"\narray before sort:"<<endl;

a.display();

int c=a.getcount();
           a.quiksort(0,c-1);

           cout<<"\narray after sort:"<<endl;

           a.display();

           }



                                                OUTPUT:



quick sort:

array before sort:

11 2 9 13 57 25 17 1 90 4

array after sort:

1 2 4 9 11 13 17 25 57 90




RESULT:

         Thus the program of quick sort was implemented and output is verified.
EX.NO: 12             Binary Searches

AIM:

To write a program to implement binary search and linear search using C++ programming.



PROGRAM:

#include <iostream.h>

#include <conio.h>

#include <stdlib.h>

class binary_search

{

    private :

     int datas[10];

    public :

     int size;

     binary_search()

     { int k;

       clrscr();

       size=10;

       for(int i=0;i<size;i++)

       {

            cout << "Enter the value for item " << i+1 << " : ";

            cin >> datas[i];

       }

       for(i=0;i<size-1;i++)

       {

           for(int j=i+1;j<size;j++)
        {

        if(datas[i]>datas[j])

        {

             k=datas[i];

             datas[i]=datas[j];

             datas[j]=k;

        }

        }

    }

        cout<<endl;

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

        {

            cout <<i+1 << " ->" << datas[i]<<endl;

        }

}

void search()

{

int item,found,mid,lb,ub;

ub=size-1;found=0;lb=0;

cout<<endl;

cout<<"Enter the value you want to search : ";

cin>>item;

while(found==0 && lb <= ub)

{

    mid=(ub+lb)/2;

    if(datas[mid]==item)
        {

        found=1;

        }

        else

        {

        if(datas[mid]<item)

        {

               lb=mid+1;

        }

        else

        {

               ub=mid-1;

        }

        }

    }

    cout<<endl;

    if(found==1)

    {

        cout<<"Data you searched " << item << " is found at position " << mid+1 << ".";

    }

    else

    {

        cout<<"Data you searched " << item << " is not found.";

    }

    cout<<endl;

}
    };

    void main()

    {

    char ch;

    binary_search bs1;

    do

    {

        bs1.search();

        cout<<endl;

        cout << "Do you want to search any more data (Y/N) : ";

        ch=getch();

    }while(ch != 'N' && ch != 'n');

}
                                 OUTPUT



Enter the value for item1 : 12

Enter the value for item2 : 56

Enter the value for item3 : 8

Enter the value for item4 : 92

Enter the value for item5 : 24

Enter the value for item6 : 37

Enter the value for item7 : 84

Enter the value for item8 : 61

Enter the value for item9 : 43

Enter the value for item10: 15



1->8

2->12

3->15

4->24

5->37

6->43

7->56

8->61

9->84

10->92
Enter you want to search : 43



Data you searched 43 is found at position 6.



Do you want to search any more data (y/n) : y



Enter you want to search : 145



Data you searched 145 is not found.



Do you want to search any more data (y/n) : n
                                             Linear Search
PROGRAM:

#include <iostream.h>

#include <conio.h>

#include <stdlib.h>

class linear_search

{

    private :

     int datas[10];

    public :

     linear_search()

     { int i;

       clrscr();

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

       {

            cout << "Enter the value for item " << i+1;

            cin >> datas[i];

       }

           for(i=0;i<size-1;i++)

       {

           for(int j=i+1;j<size;j++)

           {

            if(datas[i]>datas[j])

            {

                k=datas[i];

                datas[i]=datas[j];
                datas[j]=k;

           }

           }

    }

           cout<<endl;

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

           {

               cout <<i+1 << " ->" << datas[i]<<endl;

           }



}

void search()

{

int item,i,found;

found=0;i=0;

cout<<"Enter the value to be search : ";

cin>>item;

while(found==0 && i < 10)

{

    if(datas[i]==item)

        found=1;

    i++;

}

if(found==1)

    cout<<"Data you searched " << item << " is found at position" << i << ".";

else
     cout<<"Data you searched " << item << " is not found.";

     }

};

void main()

{

char ch;

linear_search ls1;

do

{

    ls1.search();

    cout << "do you want to search any more data (Y/N) : ";

    cin>>ch;

}while(ch != 'N' && ch != 'n');

}
                                 OUTPUT



Enter the value for item1 : 12

Enter the value for item2 : 56

Enter the value for item3 : 8

Enter the value for item4 : 92

Enter the value for item5 : 24

Enter the value for item6 : 37

Enter the value for item7 : 84

Enter the value for item8 : 61

Enter the value for item9 : 43

Enter the value for item10: 15



1->8

2->12

3->15

4->24

5->37

6->43

7->56

8->61

9->84

10->92
Enter you want to search : 43



Data you searched 43 is found at position 6.



Do you want to search any more data (y/n) : y



Enter you want to search : 145



Data you searched 145 is not found.



Do you want to search any more data (y/n) : n




        RESULT:

                Thus the programs of binary and linear search are implemented and the output is verified.

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:17
posted:10/18/2012
language:Latin
pages:79
aravind a aravind a devilx science http://www.devilxscience.in
About