0% found this document useful (0 votes)
65 views18 pages

Bubble Sort:: Vit University Data Structure and Algorithm Lab Assesment - 2

The document contains algorithms and programs for various sorting and data structure techniques: 1) It includes bubble sort, insertion sort, and merge sort algorithms and their implementations in C code using arrays. 2) Other sections cover converting infix to postfix notation, evaluating postfix expressions using a stack, and implementing a queue with functions for insertion and deletion. 3) For each algorithm, pseudocode is provided along with a full C program to demonstrate how to code the technique.

Uploaded by

NITESH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views18 pages

Bubble Sort:: Vit University Data Structure and Algorithm Lab Assesment - 2

The document contains algorithms and programs for various sorting and data structure techniques: 1) It includes bubble sort, insertion sort, and merge sort algorithms and their implementations in C code using arrays. 2) Other sections cover converting infix to postfix notation, evaluating postfix expressions using a stack, and implementing a queue with functions for insertion and deletion. 3) For each algorithm, pseudocode is provided along with a full C program to demonstrate how to code the technique.

Uploaded by

NITESH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

VIT UNIVERSITY

DATA STRUCTURE AND ALGORITHM

LAB ASSESMENT -2
Submitted By:

Shridhar Kumar Mahato (18BCE2500)

BUBBLE SORT:
Algorithm:-

begin BubbleSort (list)

for all elements of list

if list[j]>list[j+1]

swap (list[j] , list[j+1])


end if

end for

return list

end BubbleSort

Program:-

#include<stdio.h>

int main()

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

printf("Enter the size of array: ");

scanf("%d",&n);

printf("Enter the array elements: ");

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

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

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

if(a[j]>a[j+1])

temp=a[j];

a[j]=a[j+1];

a[j+1]=temp;

printf("\nArray after sorting: ");

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

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

return 0;

}
Insertion Sort
Algorithm:-

Let ARR is an array with N elements


1. Read ARR
2. Repeat step 3 to 8 for I=1 to N-1
3. Set Temp=ARR[I]
4. Set J=I-1
5. Repeat step 6 and 7 while Temp<ARR[J] AND J>=0
6. Set ARR[J+1]=ARR[J] [Moves element forward]
7. Set J=J-1
[End of step 5 inner
loop]
8. Set ARR[J+1]=Temp [Insert element in proper place]
[End of step 2 outer
loop]
9. Exit

Program:-

#include<stdio.h>

int main()
{
int i,j,n,temp,a[30];
printf("Enter the number of elements:");
scanf("%d",&n);
printf("\nEnter the elements\n");

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

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

while((temp<a[j])&&(j>=0))
{
a[j+1]=a[j];
j=j-1;
}

a[j+1]=temp;
}

printf("\nSorted list is as follows\n");


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

return 0;
}
Merge sort
Algorithm:-

Step 1 − if it is only one element in the list it is already sorted, return.

Step 2 − divide the list recursively into two halves until it can no more be divided.

Step 3 − merge the smaller lists into new list in sorted order.

Program:-

#include<stdio.h>

void mergesort(int a[],int i,int j);

void merge(int a[],int i1,int j1,int i2,int j2);

int main()

int a[30],n,i;

printf("Enter no of elements:");

scanf("%d",&n);

printf("Enter array elements:");

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

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

mergesort(a,0,n-1);

printf("\nSorted array is :");


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

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

return 0;

void mergesort(int a[],int i,int j)

int mid;

if(i<j)

mid=(i+j)/2;

mergesort(a,i,mid);

mergesort(a,mid+1,j);

merge(a,i,mid,mid+1,j);

void merge(int a[],int i1,int j1,int i2,int j2)

int temp[50];

int i,j,k;

i=i1;

j=i2;

k=0;
while(i<=j1 && j<=j2)

if(a[i]<a[j])

temp[k++]=a[i++];

else

temp[k++]=a[j++];

while(i<=j1)

temp[k++]=a[i++];

while(j<=j2)

temp[k++]=a[j++];

for(i=i1,j=0;i<=j2;i++,j++)

a[i]=temp[j];

}
CONVERT INFIX TO POSTFIX
Algorithm:-

1. Push “(“onto Stack, and add “)” to the end of X.


2. Scan X from left to right and repeat Step 3 to 6 for each element of X until the Stack is empty.
3. If an operand is encountered, add it to Y.
4. If a left parenthesis is encountered, push it onto Stack.
5. If an operator is encountered ,then:
1. Repeatedly pop from Stack and add to Y each operator (on the top of Stack) which
has the same precedence as or higher precedence than operator.
2. Add operator to Stack. [End of If]
6. If a right parenthesis is encountered ,then:
1. Repeatedly pop from Stack and add to Y each operator (on the top of Stack) until a
left parenthesis is encountered.
2. Remove the left Parenthesis. [End of If] [End of If]
7. END

PROGRAM:-

#include<stdio.h>

#include<conio.h>
#include<ctype.h>

#define MAX 50

typedef struct stack

int data[MAX];

int top;

}stack;

int precedence(char);

void init(stack *);

int empty(stack *);

int full(stack *);

int pop(stack *);

void push(stack *,int);

int top(stack *);

void infix_to_postfix(char infix[],char postfix[]);

void main()

char infix[30],postfix[30];

printf("Enter an infix expression(eg: 5+2*4): ");

gets(infix);

infix_to_postfix(infix,postfix);

printf("\nPostfix expression: %s",postfix);


}

void infix_to_postfix(char infix[],char postfix[])

stack s;

char x,token;

int i,j;

init(&s);

j=0;

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

token=infix[i];

if(isalnum(token))

postfix[j++]=token;

else

if(token=='(')

push(&s,'(');

else

if(token==')')

while((x=pop(&s))!='(')

postfix[j++]=x;

else

while(precedence(token)<=precedence(top(&s))&&!empty(&s))

{
x=pop(&s);

postfix[j++]=x;

push(&s,token);

while(!empty(&s))

x=pop(&s);

postfix[j++]=x;

postfix[j]='\0';

int precedence(char x)

if(x=='(')

return(0);

if(x=='+'||x=='-')

return(1);

if(x=='*'||x=='/'||x=='%')

return(2);

return(3);
}

void init(stack *s)

s->top=-1;

int empty(stack *s)

if(s->top==-1)

return(1);

return(0);

int full(stack *s)

if(s->top==MAX-1)

return(1);

return(0);

void push(stack *s,int x)

s->top=s->top+1;
s->data[s->top]=x;

int pop(stack *s)

int x;

x=s->data[s->top];

s->top=s->top-1;

return(x);

int top(stack *p)

return (p->data[p->top]);

}
POSTFIX Expression EVALUATION
Algorithm:-
1) Add ) to postfix expression.
2) Read postfix expression Left to Right until ) encountered
3) If operand is encountered, push it onto Stack [End If]
4) If operator is encountered, Pop two elements i) A -> Top element ii) B-> Next to Top element
iii) Evaluate B operator A push B operator A onto Stack
5) Set result = pop
6) END

Program:-

#include<stdio.h>

#define MAX 20

typedef struct stack

{
int data[MAX];

int top;

}stack;

void init(stack *);

int empty(stack *);

int full(stack *);

int pop(stack *);

void push(stack *,int);

int evaluate(char x,int op1,int op2);

int main()

stack s;

char x;

int op1,op2,val;

init(&s);

printf("Enter the expression(eg: 59+3*)\nSingle digit operand and operators only:");

while((x=getchar())!='\n')

if(isdigit(x))

push(&s,x-48);

else

op2=pop(&s);
op1=pop(&s);

val=evaluate(x,op1,op2);

push(&s,val);

val=pop(&s);

printf("\nValue of expression=%d",val);

return 0;

int evaluate(char x,int op1,int op2)

if(x=='+')

return(op1+op2);

if(x=='-')

return(op1-op2);

if(x=='*')

return(op1*op2);

if(x=='/')

return(op1/op2);

if(x=='%')

return(op1%op2);

}
void init(stack *s)

s->top=-1;

int empty(stack *s)

if(s->top==-1)

return(1);

return(0);

int full(stack *s)

if(s->top==MAX-1)

return(1);

return(0);

void push(stack *s,int x)

s->top=s->top+1;

s->data[s->top]=x;

}
int pop(stack *s)

int x;

x=s->data[s->top];

s->top=s->top-1;

return(x);

Implementation of queue
Program:-

EMPTY-CHECK(QUEUE,FRONT,REAR,EMPTY)

bool isempty(){
if(front<0 || front>rear)
return true;
else
return false;
}

FULL-CHECK(QUEUE,FRONT,REAR,MAX,FULL)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy