+ All Categories
Home > Education > Data structure new lab manual

Data structure new lab manual

Date post: 15-Jan-2015
Category:
Upload: santosh-rath
View: 735 times
Download: 1 times
Share this document with a friend
Description:
 
Popular Tags:
37
DATA STRUCTURE LAB MANUAL PREPARED BY: - Asst.Prof.Santosh Kumar Rath EXPERIMENT NO-1 a) WRITE A C PROGRAM FOR INSERT AN ELEMENT AT DESIRED POSITION IN AN ARRAY. Program: #include<stdio.h> #include<conio.h> void main() { int array[100], position, i, n, value; clrscr(); printf("Enter number of elements in array\n"); scanf("%d", &n); printf("Enter %d elements\n", n); for (i = 0; i< n; i++) scanf("%d", &array[i]); printf("Enter the location where you wish to insert an element\n"); scanf("%d", &position); printf("Enter the value to insert\n"); scanf("%d", &value); for (i = n - 1; i >= position - 1; i--) array[i+1] = array[i]; array[position-1] = value; printf("Resultant array is\n"); for (i = 0; i <= n; i++) printf("%d\n", array[i]); getch(); } b) WRITE A C PROGRAM FOR DELETE AN ELEMENT AT DESIRED POSITION IN AN ARRAY. Program: #include<stdio.h> #include<conio.h>
Transcript
Page 1: Data structure  new lab manual

DATA STRUCTURE LAB MANUAL PREPARED BY: - Asst.Prof.Santosh Kumar Rath

EXPERIMENT NO-1

a) WRITE A C PROGRAM FOR INSERT AN ELEMENT AT DESIRED POSITION IN AN ARRAY.

Program:

#include<stdio.h>#include<conio.h>void main(){ int array[100], position, i, n, value; clrscr(); printf("Enter number of elements in array\n"); scanf("%d", &n); printf("Enter %d elements\n", n); for (i = 0; i< n; i++) scanf("%d", &array[i]); printf("Enter the location where you wish to insert an element\n"); scanf("%d", &position); printf("Enter the value to insert\n"); scanf("%d", &value); for (i = n - 1; i >= position - 1; i--) array[i+1] = array[i]; array[position-1] = value; printf("Resultant array is\n"); for (i = 0; i <= n; i++) printf("%d\n", array[i]); getch(); }

b) WRITE A C PROGRAM FOR DELETE AN ELEMENT AT DESIRED POSITION IN AN ARRAY.

Program:

#include<stdio.h>#include<conio.h>void main(){ int a[100], pos, i, n, item; clrscr(); printf("Enter number of elements in array\n"); scanf("%d", &n); printf("Enter %d elements\n", n); for (i = 0; i< n; i++) scanf("%d", &a[i]);

Page 2: Data structure  new lab manual

DATA STRUCTURE LAB MANUAL PREPARED BY: - Asst.Prof.Santosh Kumar Rath

printf("Enter the location where you wish to delete an element\n"); scanf("%d", &pos); for (i = pos-1;i<n;i++) a[i] = a[i+1]; printf("Resultant array is\n"); for (i = 0; i <n-1; i++) printf("%d\n", a[i]); getch(); }

c) WRITE A C PROGRAM FOR SEARCHING AN ELEMENT IN AN ARRAY.

Program:

#include<stdio.h>#include<conio.h>\void main(){int a[10],i,item,n;printf("Enter The Range\n");scanf("%d",&n);printf("Enter The Data\n");for(i=0;i<n;i++)scanf("%d",&a[i]);printf("Enter The Item To Be Search\n");\scanf("%d",&item);for(i=0;i<n;i++){if(a[i]==item){printf("Item Present in Position:%d\t",i+1);}}getch();}

Page 3: Data structure  new lab manual

DATA STRUCTURE LAB MANUAL PREPARED BY: - Asst.Prof.Santosh Kumar Rath

EXPERIMENT NO-2 WRITE A C PROGRAM TO GENERATE SPARSE MATRIX

Program:

#include<stdio.h>#include<conio.h>void main(){ int A[10][10],B[10][3],m,n,s=0,i,j; clrscr(); printf("\nEnter the order m x n of the sparse matrix\n"); scanf("%d%d",&m,&n); printf("\nEnter the elements in the sparse matrix(mostly zeroes)\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf("\n%d row and %d column: ",i,j); scanf("%d",&A[i][j]); } } printf("The given matrix is:\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf("%d ",A[i][j]); } printf("\n"); } for(i=0;i<m;i++) { for(j=0;j<n;j++) { if(A[i][j]!=0) { B[s][0]=i; B[s][1]=j; B[s][2]=A[i][j]; s++; } } } printf("\nThe sparse matrix is given by"); printf("\n");

Page 4: Data structure  new lab manual

DATA STRUCTURE LAB MANUAL PREPARED BY: - Asst.Prof.Santosh Kumar Rath

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

Page 5: Data structure  new lab manual

DATA STRUCTURE LAB MANUAL PREPARED BY: - Asst.Prof.Santosh Kumar Rath

EXPERIMENT NO-3 WRITE A C-PROGRAM FOR MATRIX MULTIPLICATION

Program:

#include<stdio.h>Void main(){  int a[5][5],b[5][5],c[5][5],i,j,k,sum=0,m,n,o,p;  printf("\nEnter the row and column of first matrix");  scanf("%d %d",&m,&n);  printf("\nEnter the row and column of second matrix");  scanf("%d %d",&o,&p);      printf("\nEnter the First matrix->");      for(i=0;i<m;i++)      for(j=0;j<n;j++)           scanf("%d",&a[i][j]);      printf("\nEnter the Second matrix->");      for(i=0;i<o;i++)      for(j=0;j<p;j++)           scanf("%d",&b[i][j]);      printf("\nThe First matrix is\n");      for(i=0;i<m;i++){      printf("\n");      for(j=0;j<n;j++){           printf("%d\t",a[i][j]);      }      }      printf("\nThe Second matrix is\n");      for(i=0;i<o;i++){      printf("\n");      for(j=0;j<p;j++){           printf("%d\t",b[i][j]);      }              }      for(i=0;i<m;i++)      for(j=0;j<p;j++)           c[i][j]=0;      for(i=0;i<m;i++){ //row of first matrix      for(j=0;j<p;j++){  //column of second matrix           sum=0;           for(k=0;k<n;k++)               sum=sum+a[i][k]*b[k][j];           c[i][j]=sum;      }

Page 6: Data structure  new lab manual

DATA STRUCTURE LAB MANUAL PREPARED BY: - Asst.Prof.Santosh Kumar Rath

      }  }  printf("\nThe multiplication of two matrix is\n");  for(i=0;i<m;i++){      printf("\n");      for(j=0;j<p;j++){           printf("%d\t",c[i][j]);      }  }getch();}

Page 7: Data structure  new lab manual

DATA STRUCTURE LAB MANUAL PREPARED BY: - Asst.Prof.Santosh Kumar Rath

Experiment 4: WRITE A C PROGRAMME TO CREATE A STACK USING AN ARRAY AND PERFORM (I) PUSH OPERATION (II) POP OPERATION

Program:

#include<stdio.h>#include<conio.h>#define MAX 5void push(int[],int);void pop(int[]);void display(int[]);int top=-1;int item;void main(){int stack[MAX],ch;clrscr();while(ch!=4){printf("\n****Menu****\n");printf("\n1-INSERTION\n");printf("\n2-DELETION\n");printf("\n3-DISPLAY\n");printf("\n4-EXIT\n");printf("\n Enter Your Choice:\n");scanf("%d",&ch);switch(ch){case 1: printf("Enter Your Item:");

scanf("%d",&item);push(stack,item);break;

case 2: printf("Popped Item is:");pop(stack);break;

case 3: display(stack);break;

case 4: exit();}}getch();

Page 8: Data structure  new lab manual

DATA STRUCTURE LAB MANUAL PREPARED BY: - Asst.Prof.Santosh Kumar Rath

}void push(int stack[MAX],int item){if(top==MAX-1){printf("Overflow");}else{top++;stack[top]=item;}}void pop(int stack[MAX]){int item;if(top==-1){printf("Underflow");}else{item=stack[top];top--;printf("Popped Item is%d",item);}}void display(int stack[MAX]){int i;for(i=top;i>=0;i--){printf("%d\t",stack[i]);}}

Page 9: Data structure  new lab manual

DATA STRUCTURE LAB MANUAL PREPARED BY: - Asst.Prof.Santosh Kumar Rath

Experiment 5: WRITE A C PROGRAMME TO CREATE A LINEAR QUEUE AND PERFORM

(I)PUSH (II) POP (III) TRAVERSAL.

Program:

#include<stdio.h>#include<conio.h>#define MAX 5void insert(int[],int);void del(int[]);void display(int[]);int f=-1,r=-1;int item;void main(){int q[MAX],ch;clrscr();while(ch!=4){printf("\n****Menu****\n");printf("1-INSERTION\n");printf("2-DELETION\n");printf("3-DISPLAY\n");printf("4-EXIT\n");printf("Enter Your Choice:\n");scanf("%d",&ch);switch(ch){case 1: printf("Enter Your Item:");

scanf("%d",&item);insert(q,item);break;

case 2: del(q);break;

case 3: display(q);break;

case 4: exit();}

Page 10: Data structure  new lab manual

DATA STRUCTURE LAB MANUAL PREPARED BY: - Asst.Prof.Santosh Kumar Rath

}getch();}void insert(int q[MAX],int item){if(r==MAX-1){printf("Overflow");}if(f==-1||r==-1){f=0;r=0;}else{r++;}q[r]=item;}void del(int q[MAX]){int item;if(f==-1||r==-1){printf("Underflow");}item=q[f];if(f==r){f=-1;r=-1;}else{f++;}printf("Deleted Item is %d:",item);}void display(int q[MAX]){int i;for(i=f;i<=r;i++){printf("%d\t",q[i]);

Page 11: Data structure  new lab manual

DATA STRUCTURE LAB MANUAL PREPARED BY: - Asst.Prof.Santosh Kumar Rath

}}

Experiment 6: WRITE A C PROGRAMME TO CREATE A LINEAR QUEUE AND PERFORM

(I)PUSH (II) POP (III) TRAVERSAL

#include<stdio.h>#include<conio.h>#define MAX 5void insert(int[],int);void del(int[]);void display(int[]);int f=-1,r=-1;int item;void main(){int q[MAX],ch;clrscr();while(ch!=4){printf("\n****Menu****\n");printf("1-INSERTION\n");printf("2-DELETION\n");printf("3-DISPLAY\n");printf("4-EXIT\n");printf("Enter Your Choice:\n");scanf("%d",&ch);switch(ch){case 1: printf("Enter Your Item:");

scanf("%d",&item);insert(q,item);break;

case 2: del(q);break;

case 3: display(q);break;

case 4: exit();}

Page 12: Data structure  new lab manual

DATA STRUCTURE LAB MANUAL PREPARED BY: - Asst.Prof.Santosh Kumar Rath

}getch();}

void insert(int q[MAX],int item){if(r==MAX-1){printf("Overflow");}if(f==-1||r==-1){f=0;r=0;}else{r=(r+1)%MAX;}q[r]=item;}void del(int q[MAX]){int item;if(f==-1||r==-1){printf("Underflow");}item=q[f];if(f==r){f=-1;r=-1;}else{f=(f+1)%MAX;}printf("Deleted Item is %d:",item);}void display(int q[MAX]){int i;for(i=f;i<=r;i++){

Page 13: Data structure  new lab manual

DATA STRUCTURE LAB MANUAL PREPARED BY: - Asst.Prof.Santosh Kumar Rath

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

Experiment 7: WRITE A C PROGRAMME TO CREATE A DOUBLE ENDED QUEUE AND PERFORM

(I)PUSH (II) POP (III) TRAVERSAL.

#include<stdio.h>#include<conio.h>#define MAX 5void insert_r(int[],int);void insert_f(int[],int);void del_f(int[]);void del_r(int[]);void display(int[]);int f=-1,r=-1;int item;void main(){int q[MAX],ch;clrscr();while(ch!=4){printf("\n****Menu****\n");printf("1-INSERTION AT REAR END\n");printf("2-INSERTION AT FRONT END\n");printf("3-DELETION AT FRONT END\n");printf("4-DELETION AT REAR END\n");printf("5-DISPLAY\n");printf("6-EXIT\n");printf("Enter Your Choice:\n");scanf("%d",&ch);switch(ch){case 1: printf("Enter Your Item:");

scanf("%d",&item);insert_r(q,item);break;

Page 14: Data structure  new lab manual

DATA STRUCTURE LAB MANUAL PREPARED BY: - Asst.Prof.Santosh Kumar Rath

case 2: printf("Enter Your Item:");scanf("%d",&item);insert_f(q,item);break;

case 3: del_f(q);break;

case 4: del_r(q);break;

case 5: display(q);break;

case 6: exit();}}getch();}void insert_r(int q[MAX],int item){if(r==MAX-1){printf("Overflow");}if(f==-1||r==-1){f=0;r=0;}else{r++;}q[r]=item;}void insert_f(int q[MAX],int item){if(f==0){printf("Overflow");}if(f==-1||r==-1){f=0;r=0;}else{

Page 15: Data structure  new lab manual

DATA STRUCTURE LAB MANUAL PREPARED BY: - Asst.Prof.Santosh Kumar Rath

f--;}q[f]=item;}void del_f(int q[MAX]){int item;if(f==-1||r==-1){printf("Underflow");}item=q[f];if(f==r){f=-1;r=-1;}else{f++;}printf("Deleted Item is %d:",item);}void del_r(int q[MAX]){int item;if(f==-1||r==-1){printf("Underflow");}item=q[r];if(f==r){f=-1;r=-1;}else{r--;}printf("Deleted Item is %d:",item);}void display(int q[MAX]){int i;

Page 16: Data structure  new lab manual

DATA STRUCTURE LAB MANUAL PREPARED BY: - Asst.Prof.Santosh Kumar Rath

for(i=f;i<=r;i++){printf("%d\t",q[i]);}}

Experiment 8: WRITE A C PROGRAM SEARCH AN ELEMENT FROM LIST OF INTEGERS USING LINEAR SEARCH.

Program:

#include<stdio.h>#include<conio.h>void search(int[],int,int);void main(){int a[10],item,n,i;clrscr();printf("enter the range");scanf("%d",&n);printf("enter the data");for(i=0;i<n;i++){scanf("%d",&a[i]);}printf("Enter the item to be search");scanf("%d",&item);search(a,n,item);getch();}void search(int a[],int n,int item){int k=0,flag=0;while(k<=n){if(a[k]==item){flag=1;

Page 17: Data structure  new lab manual

DATA STRUCTURE LAB MANUAL PREPARED BY: - Asst.Prof.Santosh Kumar Rath

printf("Item found at %d",k);break;}k++;}if(flag==0){printf("Item is not found");}}

Experiment 9: WRITE A C PROGRAM SEARCH AN ELEMENT FROM LIST OF INTEGERS USING BINARY SEARCH.

Program:

Experiment 6: WRITE A C PROGRAM TO INFIX TO POSTFIX AND EVALUATION OF POSTFIX

Program:

#include<stdio.h>#include<string.h>#include<math.h>#define MAX 40long int pop ();long int post_eval();char infix[MAX], postfix[MAX];long int stack[MAX];int top;main(){

top = 0;printf("Enter infix expression: ");gets(infix);infix_postfix();printf("Postfix : %s\n",postfix);printf("Value of expression is %ld\n",post_eval());

}// end of main()infix_postfix(){

Page 18: Data structure  new lab manual

DATA STRUCTURE LAB MANUAL PREPARED BY: - Asst.Prof.Santosh Kumar Rath

int i,p=0,precedence;char next ;stack[top]='#';infix[strlen(infix)]='#';i=0;while(infix[i]!='#'){

if(!space(infix[i])){

switch(infix[i]){case '(':

push(infix[i]);break;

case ')':while((next = pop()) != '(')

postfix[p++] = next;break;

case '+':case '-':case '*':case '/':case '%':case '^':

precedence = find_prec(infix[i]);while(stack[top]!='#' && precedence<= find_prec(stack[top]))

postfix[p++] = pop();push(infix[i]);break;

default: //if an operand comes postfix[p++] = infix[i];}//End of switch

}//End of ifi++;

}//End of whilewhile(stack[top]!='#')

postfix[p++] = pop();postfix[p] = '\0' ; //To make post fix array to string

}//End of infix_postfix()find_prec(char symbol ){

switch(symbol){case '(':

return 0;

Page 19: Data structure  new lab manual

DATA STRUCTURE LAB MANUAL PREPARED BY: - Asst.Prof.Santosh Kumar Rath

case '+':case '-':

return 1;case '*':case '/':case '%':

return 2;case '^':

return 3;}

}//End of prec()push(long int symbol){

if(top > MAX){

printf("Stack overflow(Stack is full)\n");exit(1);

}else{

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

}}//End of push()long int pop(){

if (top == -1 ){

printf("Stack underflow(stack is empty)\n");exit(1);

}else

return (stack[top--]);}//End of popspace(char symbol){

if( symbol == ' ' || symbol == '\t' || symbol == '\0')return 1;

elsereturn 0;

}//End of space()

long int post_eval(){

long int x,y,val,result,len;

Page 20: Data structure  new lab manual

DATA STRUCTURE LAB MANUAL PREPARED BY: - Asst.Prof.Santosh Kumar Rath

int i;len=strlen(postfix);postfix[len]='#';for(i=0;postfix[i]!='#';i++){

if(postfix[i]<='9' && postfix[i]>='0')push( postfix[i]-48 );

else{

x=pop();y=pop();

switch(postfix[i]){case '+':

val=x+y;break;

case '-':val=x-y;break;

case '*':val=x*y;break;

case '/':val=x/y;break;

case '%':val=x%y;break;

case '^':val=pow(x,y);

}push(val);

}}return pop();

}//End of eval_post

Page 21: Data structure  new lab manual

DATA STRUCTURE LAB MANUAL PREPARED BY: - Asst.Prof.Santosh Kumar Rath

Experiment 7: WRITE A C PROGRAMME STACK IMPLEMENTATION USING LINKED LIST

#include<stdio.h>#include<conio.h>#include<alloc.h>#include<stdlib.h>/* Node decleration */struct node{int data;struct node *link; //to maintain the link other nodes};struct node *top,*temp;

void create();void push();void pop();void display();/* create function create the head node */void create()

Page 22: Data structure  new lab manual

DATA STRUCTURE LAB MANUAL PREPARED BY: - Asst.Prof.Santosh Kumar Rath

{printf("\nENTER THE FIRST ELEMENT: ");top=(struct node *)malloc(sizeof(struct node));scanf("%d",&top->data);top->link=NULL;temp=top;}

/* display function visit the linked list from top to end */void display(){top=temp; // bring the top to top positionprintf("\n");while(top!=NULL){printf("%d\n",top->data);top=top->link; // Now top points the previous node in the list}}void push(){printf("\nENTER THE NEXT ELEMENT: ");top=(struct node *)malloc(sizeof(struct node));scanf("%d",&top->data);top->link=temp;temp=top;}

void pop(){if(temp==NULL){printf("\nSTACK IS EMPTY\n");}else{top=temp;printf("\nDELETED ELEMENT IS %d\n",temp->data);temp=temp->link;free(top);}}void main(){int ch;

Page 23: Data structure  new lab manual

DATA STRUCTURE LAB MANUAL PREPARED BY: - Asst.Prof.Santosh Kumar Rath

clrscr();while(1){printf("\n\n 1.CREATE \n 2.PUSH \n 3.POP \n 4.EXIT \n");printf("\n ENTER YOUR CHOICE : ");scanf("%d",&ch);switch(ch){case 1:

create();display();break;

case 2:push();display();break;

case 3:pop();display();break;

case 4:exit(0);

}}

Experiment 8: WRITE A C PROGRAMME TO PERFORM THE FOLLOWING OPERATION ON A SINGLE LINKED LIST (I) CREATION (II) INSERTION (III) TRAVERSE

Program:

#include<stdio.h>#include<conio.h>#include<alloc.h>struct node{int info;struct node *link;};struct node *head,*ptr,*temp;void insert_beg();void insert_end();void insert_any();void display();

Page 24: Data structure  new lab manual

DATA STRUCTURE LAB MANUAL PREPARED BY: - Asst.Prof.Santosh Kumar Rath

void main(){int item,ch,c=1;clrscr();head->info=NULL;head->link=NULL;while(c==1){printf("\nDISPLAY MENUES\n");printf("\n1.INSERT_BEG\n");printf("\n2.INSERT_END\n");printf("\n3.INSERT_ANY\n");printf("\n4.DISPLAY\n");printf("\nEnter Your Choice\n");scanf("%d",&ch);switch(ch){case 1: insert_beg();

break;case 2: insert_end();

break;case 3: insert_any();

break;case 4: display();

break;}printf("\nDo You Want To Continue(1/0)");scanf("%d",&c);}getch();}/************INSERT AT BEGING***************/void insert_beg(){int item;printf("\nEnter The Item\n");scanf("%d",&item);temp=(struct node*)malloc(sizeof(struct node));temp->info=item;temp->link=head;head=temp;}/************INSERT AT END***************/void insert_end(){

Page 25: Data structure  new lab manual

DATA STRUCTURE LAB MANUAL PREPARED BY: - Asst.Prof.Santosh Kumar Rath

int item;printf("\nEnter The Item\n");scanf("%d",&item);temp=(struct node*)malloc(sizeof(struct node));ptr=head;while(ptr->link!=NULL){ptr=ptr->link;}temp->info=item;temp->link=ptr->link;ptr->link=temp;}/************INSERT AT ANY POSITION***************/void insert_any(){int item,key;printf("\nEnter The Item\n");scanf("%d",&item);printf("\nEnter The Key Element:\n");scanf("%d",&key);temp=(struct node*)malloc(sizeof(struct node));ptr=head;while(ptr->link!=NULL&&ptr->info!=key){ptr=ptr->link;}if(ptr->info==key){temp->info=item;temp->link=ptr->link;ptr->link=temp;}else{printf("Key Element Is Not Found:%d",key);}}/************DISPLAY THE ELEMENTS***************/

void display(){int item;ptr=head;while(ptr->link!=NULL)

Page 26: Data structure  new lab manual

DATA STRUCTURE LAB MANUAL PREPARED BY: - Asst.Prof.Santosh Kumar Rath

{ptr=ptr->link;}printf("%d",ptr->info);}

Experiment 9: SOURCE CODE OF SIMPLE SELECTION SORT IMPLEMENTATION USING ARRAY ASCENDING ORDER IN C PROGRAMMING LANGUAGE

#include<stdio.h>#include<conio.h>void main(){  int s,i,j,temp,a[20];clrscr();  printf("Enter total elements: ");  scanf("%d",&s);  printf("Enter %d elements: ",s);  for(i=0;i<s;i++)      scanf("%d",&a[i]);  for(i=0;i<s;i++){      for(j=i+1;j<s;j++){

Page 27: Data structure  new lab manual

DATA STRUCTURE LAB MANUAL PREPARED BY: - Asst.Prof.Santosh Kumar Rath

           if(a[i]>a[j]){               temp=a[i];              a[i]=a[j];              a[j]=temp;           }      }  }  printf("After sorting is: ");  for(i=0;i<s;i++)      printf(" %d",a[i]);  }Output:Enter total elements: 5Enter 5 elements: 4 5 0 21 7The array after sorting is:  0 4 5 7 21

Experiment 10: SOURCE CODE OF SIMPLE MERGE SORT IMPLEMENTATION USING ARRAY IN ASCENDING ORDER IN C PROGRAMMING LANGUAGE

#include<stdio.h>#define MAX 50void mergeSort(int arr[],int low,int mid,int high);void partition(int arr[],int low,int high);int main(){    int merge[MAX],i,n;    printf("Enter the total number of elements: ");    scanf("%d",&n);    printf("Enter the elements which to be sort: ");    for(i=0;i<n;i++){         scanf("%d",&merge[i]);    }

Page 28: Data structure  new lab manual

DATA STRUCTURE LAB MANUAL PREPARED BY: - Asst.Prof.Santosh Kumar Rath

    partition(merge,0,n-1);    printf("After merge sorting elements are: ");    for(i=0;i<n;i++){         printf("%d ",merge[i]);    }   return 0;}void partition(int arr[],int low,int high){    int mid;    if(low<high){         mid=(low+high)/2;         partition(arr,low,mid);         partition(arr,mid+1,high);         mergeSort(arr,low,mid,high);    }}void mergeSort(int arr[],int low,int mid,int high){    int i,m,k,l,temp[MAX];    l=low;    i=low;    m=mid+1;    while((l<=mid)&&(m<=high)){

         if(arr[l]<=arr[m]){             temp[i]=arr[l];             l++;         }         else{             temp[i]=arr[m];             m++;         }         i++;    }    if(l>mid){         for(k=m;k<=high;k++){             temp[i]=arr[k];             i++;         }    }    else{         for(k=l;k<=mid;k++){             temp[i]=arr[k];             i++;         }    }

Page 29: Data structure  new lab manual

DATA STRUCTURE LAB MANUAL PREPARED BY: - Asst.Prof.Santosh Kumar Rath

       for(k=low;k<=high;k++){         arr[k]=temp[k];    }}Sample output:Enter the total number of elements: 5Enter the elements which to be sort: 2 5 0 9 1After merge sorting elements are: 0 1 2 5 9

Experiment 11: SOURCE CODE OF SIMPLE INSERTION SORT IMPLEMENTATION USING ARRAY IN ASCENDING ORDER IN C PROGRAMMING LANGUAGE

#include<stdio.h>int main(){  int i,j,s,temp,a[20];  printf("Enter total elements: ");  scanf("%d",&s);  printf("Enter %d elements: ",s);  for(i=0;i<s;i++)      scanf("%d",&a[i]);  for(i=1;i<s;i++){      temp=a[i];      j=i-1;

Page 30: Data structure  new lab manual

DATA STRUCTURE LAB MANUAL PREPARED BY: - Asst.Prof.Santosh Kumar Rath

      while((temp<a[j])&&(j>=0)){      a[j+1]=a[j];          j=j-1;      }      a[j+1]=temp;  }  printf("After sorting: ");  for(i=0;i<s;i++)      printf(" %d",a[i]);  return 0;}Output:Enter total elements: 5Enter 5 elements: 3 7 9 0 2After sorting:  0 2 3 7 9

Experiment 12: SOURCE CODE OF SIMPLE BUBBLE SORT IMPLEMENTATION USING ARRAY ASCENDING ORDER IN C PROGRAMMING LANGUAGE

#include<stdio.h>int main(){  int s,temp,i,j,a[20];  printf("Enter total numbers of elements: ");  scanf("%d",&s);  printf("Enter %d elements: ",s);  for(i=0;i<s;i++)      scanf("%d",&a[i]);  //Bubble sorting algorithm  for(i=s-2;i>=0;i--){      for(j=0;j<=i;j++){

Page 31: Data structure  new lab manual

DATA STRUCTURE LAB MANUAL PREPARED BY: - Asst.Prof.Santosh Kumar Rath

           if(a[j]>a[j+1]){               temp=a[j];              a[j]=a[j+1];              a[j+1]=temp;           }      }  }  printf("After sorting: ");  for(i=0;i<s;i++)      printf(" %d",a[i]);  return 0;


Recommended