Prog Lab-II-Data Structures Using C-Lab Manual
Prog Lab-II-Data Structures Using C-Lab Manual
UNIVERSITY
I SEMESTER
Lab Incharge
(Dr.J.Sasikala)
1.Recursion in C
a) Factorial numbers
Aim
int n,i,fact=1;
printf("Enter any number : ");
scanf("%d", &n);
for(i=1; i<=n; i++)
fact = fact * i;
printf("Factorial value of %d = %d",n,fact);
return 0;
}
Sample output
Enter a number: 5
Factorial expansion phase:
5
4
3
2
1
Factorial wrapup phase:
1
2
6
24
120
The factorial of number 5 is 120.
Result
Thus the program was successfully executed
and the output was verified.
b) Fibonacci series
Aim
Write a C Program to implement Fibonacci
Series using Data Structures
Algorithm
(i)Start the program
(ii)Declare variables n,i=0,j=1,temp,k.
(iii)Input and read the limit of the Fibonacci
series,n.
(iv)If n==1 then
1)Print i and j.
(v)Else
1).Print i and j.
2).Initialize k as 2.
3).while(k<n) repeat steps a to e.
a)Find temp= i+j.
b)Print temp.
c)Assign i=j.
d)Assign j=temp.
e)Increment k by 1.
4).End while.
(vi) End if.
(vii) Stop the program
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i=0,j=1,temp,k;
clrscr();
printf("Enter the limit: ");
scanf("%d",&n);
printf("fibonacci series\n");
if(n==1)
{
printf("%d\n%d\n",i,j);
}
else
{
printf("%d\n%d\n",i,j);
for(k=2;k<n;k++)
{
temp=i+j;
printf("%d\n",temp);
i=j;
j=temp;
}
}
getch();
Output
Enter the limit:13
Fibonacci Series
0
1
1
2
3
5
8
13
21
34
55
89
144
Result
Thus the program was successfully executed
and the output was verified.
ARRAY
Aim
To write a C program to perform the
implementation of stack using array.
Algorithm
Step 1: Start the program
Step 2: Initialize the array variable st[100] for
storing elements and declare the required
variables.
Step 3: Define a function to push , pop and display
the data item of the queue.
Step 4: For push() function
Get the new elements
Create the top is greater than array
size,display stack is overflow.
else
Insert the new element and increment the top
pointer.
void main()
{
clrscr();
top=-1;
printf(\n enter the size of STACK[MAX=100]:);
scanf(%d,&n);
printf(\n\t\t STACK OPERATION);
printf(\n\t\t~~~~~~~~~~~~~~~~~);
printf(\n\t1.PUSH\n\t2.POP\n\t3.DISPLAY\n\t4.
EXIT);
do
{
//clrscr();
printf(\n enter UR option:);
scanf(%d,&option);
switch(option)
{
case 1:{push();break;}
case 2:{pop();break;}
case 3:{display();break;}
case 4:{printf(\n\tUR ON EXIT);break;}
default:{printf{\n\t enter only 1,2,3,4);
}
getch();
}
while(option!=4);
}
void push()
{
if(top>=n-1)
{
printf(\n\t\tSTACK is over flow);
getch();
}
else
{
top--;
//display();
}
}
void display()
{
if(top>=0)
{
printf(\n the elements in STACK);
printf(\n~~~~~~~~~~~~~~~~~~~);
for(i=top;i>=0;;i--)
printf(\n\n%d,stack[i]);
printf(\n\n\n\n press any key to continue);
}
else
{
printf(\n the STACK is empty);
}
}
Sample output:
Enter the size of STACK[MAX=100]: 3
STACK OPERATION
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter ur option: 1
Enter a value to be pushed:22
Enter ur option: 1
Enter a value to be pushed:33
Enter ur option: 1
Enter a value to be pushed:44
Enter ur option: 1
STACK is over flow
Enter ur option:3
The elements in STACK
44
33
22
Press any key to continue
Enter ur option: 2
The popped elements is 44
Enter ur option: 2
The popped elements is 33
Enter ur option: 2
The popped elements is 22
Enter ur option: 2
Stack is under flow
Enter ur option: 3
The STACK is empty
Enter ur option: 4
UR ON EXIT
Result:
Thus the program was successfully
executed and the output was verified.
clrscr();
printf(\n\t\tQueue using array);
printf(\n1.Insertion);
printf(\n2.Deletion);
printf(\n3.Display);
printf(\n4.Exit);
while(ch)
{
printf(\n Enter UR choice);
scanf(%d,&ch);
switch(ch)
{
case 1:
if(rear= =x)
printf(\n Queue full);
else
{
printf(\n enter no%d:,j++);
scanf(%d,&q[rear++]);
}
break;
case 2:
if(front= =rear)
{
printf(\n Queue is empty);
}
else
{
printf(\ n Deleted elements is %d,q[front++]);
x++;
}
break;
case 3:
printf(\n Queue elements);
if(front==rear)
printf(\n queue is empty);
else
{
for(i=front;i<rear;i++)
{
printf(%d,q[i]);
printf(\t);
}
break;
case 4:
exit();
default:
exit();
}
}
}
getch();
}
Sample output
Queue using array
1.Insertion
2.Deletion
3.Display
4.Exit
Enter ur choice 1
Enter no1: 22
Enter ur choice 1
Enter no1: 33
Enter ur choice 1
Enter no1: 11
Enter ur choice 3
Queue elements
22
Enter ur choice 2
Deleted elements is 11
Enter ur choice 2
Queue is empty
33
11
Enter ur choice 3
Queue elements
Queue is empty
Enter ur choice 4
Ur on exit..
Result
Thus the program was successfully
executed and the output was verified
Operator
X
*/
+-
priority
0
2
1
Program
/*infix to postfix*/
#include<stdio.h>
#include<conio.h>
#include<type.h>
int isp(char);
int icp(char);
void convert();
char str[50];
void main()
{
clrscr();
printf(\n\t\tINFIX
CONVERSION);
TO
POSTFIX
}
int isp(char c)
{
int pr;
switch(c)
{
case&:
pr=-1;
break;
case):
pr=0;
break;
case*:
pr=3;
break;
case/:
pr=2;
break;
case+:
pr=1;
break;
case-:
pr=1;
break;
case(:
pr=0;
break;
}
return(pr);
}
int icp(char c)
{
int pr;
switch(c)
{
case ):
pr=0;
break;
case ^:
pr=3;
break;
case *:
pr=2;
break;
case /:
pr=2;
break;
case +:
pr=1;
break;
case -:
pr=1;
break;
case (:
pr=4;
break;
}
return(pr);
}
void convert();
{
int i=0,top=0;
char item,st[50];
st[0]=&;
printf(\n The postfix expression:);
while(str[i]!=#)
{
item=str[i];
if(isalpha(item))
printf(%c,item);
else
{
if(item= =));
{
while(st[top]!=();
{
printf(%c,st[top]);
top--;
}
top--;
}
else
{
while(isp(st[top])>=icp(item))
{
pritnf(%c,st[top]);
top--;
}
top++;
st[top]=item;
}
}
i++;
}
while(top>=1)
{
printf(%c,st[top]);
top--;
}
}
Sample output
INFIX TO POSTFIX CONVERSION
Enter the infix expression
Enter the infix expression add # at last character
(a+b)*c+(d-a)#
The postfix expression: ab+c*da-+
Result
Thus the program was successfully executed and
the output was verified.
the
Program
/*SINGLE LINKED LIST*/
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct stack
{
int info;
struct stack *next;
}
*p,*root,*newn;
int ch=1,val;
void main()
{
void create();
void insert();
void del();
void disp();
clrscr();
printf(\n\t\tLINEAR LINKED LIST );
printf(\n1.create\n2.insert\n3.delete\n4.display);
while(ch)
{
printf(\n Enter ur choice :);
scanf(%d,&ch);
switch(ch)
{
case 1:
create();
break;
case 2:
insert();
break;
case 3:
del();
break;
case 4:
disp();
break;
default:
exit(0);
}
}
getch();
}
void create()
{
newn=malloc(sizeof(struct stack));
printf(\nenter val:);
scanf(%d,&val);
newn->info=val;
newn->next=null;
root=newn;
p=root;
printf(\nEnter val:);
scanf(%d,&val);
while(val!=0)
{
newn=malloc(sizeof(struct stack));
newn->info=val;
p=root;
if(val<=p->info)
{
newn->next=p;
root=newn;
printf(\nEnter val:);
scanf(%d,&val);
}
else
{
while((p->next!=null)&&(p->next->info<=val))
{
p=p->next;
}
newn->next=p->next;
p->next=newn;
printf(\nEnter val:);
scanf(%d,&val);
}
}
}
void insert()
{
int val;
newn=malloc(sizeof(struct stack));
printf(\nEnter val:);
scanf(%d,&val);
newn->info=val;
p=root;
if(val<=p->info)
{
newn->next=p;
root=newn;
}
else
{
while((p->next!=NULL)&&(p->next->info<=val))
{
p=p->next;
}
newn->next=p->next;
p->next=newn;
}
}
void del()
{
struct stack*f;
int val;
printf(\nEnter val:);
scanf(%d,&val);
p=root;
if(val==root->info)
{
root=root->next;
free(p);
}
else
{
while(p->next!=NULL)
{
if(p->next!=NULL)
{
f=p->next;
p->next=f->next;
free(f);
if(p->next= =NULL)
{
p=root;
}
break;
}
p=p->next;
}
if(p->next==NULL)
{
printf(\n Value not found);
}
}
}
void disp()
{
p=root;
if(root!=NULL)
{
4.display
Enter ur choice: 1
Enter val: 22
Enter val: 33
Enter val: 44
Enter val: 0
Enter ur choice: 4
The elements are: top <- 22 <- 33 <- 44 <- NULL
Enter ur choice:2
Enter val:36
Enter ur choice: 4
The elements are: top <- 22 <- 33 <- 36 <- 44 <NULL
Enter ur choice:3
Enter val:33
Enter ur choice:4
The elements are: top <- 22 <- 36 <- 44 <- NULL
Enter ur choice:5
Result
Thus the program was successfully executed
and the output was verified
the
*p,*root,*newn;
int ch,val;
void main()
{
void create();
void insert();
void del();
void disp();
clrscr();
printf(\n\t\tDOUBLY LINKED LIST );
printf(\n1.create\n2.insert\n3.delete\n4.display);
do
{
printf(\n Enter ur choice :);
scanf(%d,&ch);
switch(ch)
{
case 1:
create();
break;
case 2:
insert();
break;
case 3:
del();
break;
case 4:
disp();
break;
default:
exit(0);
}
}
while(ch<=4);
getch();
}
void create()
{
newn=malloc(sizeof(struct stack));
printf(\nEnter val:);
scanf(%d,&val);
newn->info=val;
newn->right=NULL;
root=newn;
p=root;
printf(\nEnter val:);
scanf(%d,&val);
while(val!=0)
{
newn=malloc(sizeof(struct stack));
newn->info=val;
p=root;
if(val<=p->info)
{
newn->right=root;
root->left=newn;
newn->left=null;
root=newn;
printf(\nEnter val:);
scanf(%d,&val);
}
else
{
while((p->right!=NULL)&&(p->right>info<=val))
{
p=p->right;
}
newn->right=p->right;
newn->left=p;
p->right->left=newn;
p->right=newn;
printf(\nEnter val:);
scanf(%d,&val);
}
}
}
void insert()
{
int val;
newn=malloc(sizeof(struct stack));
printf(\nenter val:);
scanf(%d,&val);
newn->info=val;
p=root;
if(val<=p->info)
{
newn->right=p;
newn->left=NULL;
p->left=newn;
root=newn;
}
else
{
while((p->right!=NULL)&&(p->right>info<=val))
{
p=p->right;
}
newn->right=p->right;
newn->left=p;
p->right->left=newn;
p->right=newn;
}
}
void del()
{
struct stack*f;
int val;
printf(\nenter val:);
scanf(%d,&val);
p=root;
if(val==root->info)
{
root=root->right;
free(p);
}
else
{
while(p->right!=NULL)
{
if(p->right->info= =val)
{
f=p->right;
p->right=f->right;
f->right->left=f->left;
free(f);
if(p->right= =NULL)
{
p=root;
}
break;
}
p=p->right;
}
if(p->right==NULL)
{
printf(\n value not found);
}
}
}
void disp()
{
p=root;
if(root!=NULL)
{
printf(\nThe elements are:);
printf(TOP<-);
for(p=root;p!=NULL;p=p->right)
{
printf(%d,p->info);
printf(->);
printf(<-);
}
printf(NULL);
}
else
printf(\nList is empty);
}
Sample output
DOUBLY LINKED LIST
1.create
2.insert
3.delete
4.display
Enter ur choice: 1
Enter val: 22
Enter val: 44
Enter val: 33
Enter val: 0
Enter ur choice: 4
The elements are: top <- 22 -><- 33-> <- 44-> <NULL
Enter ur choice:2
Enter val:11
Enter ur choice: 4
The elements are: top <-11-><- 22-> <- 33-> <44-> <- NULL
Enter ur choice:3
Enter val:33
Enter ur choice:4
The elements are: top <- 11-> <- 22-> <- 44 -><NULL
Enter ur choice:5
Result
Thus the program was successfully executed
and the output was verified.
Program
//STACK USING LINKED LIST
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct stack
{
int data;
struct stack *next;
}
*p,*top,*newn;
int ch=1;
void main()
{
void push();
void pop();
void display();
clrscr();
}
void push()
{
printf(Enter ur data);
newn=malloc(sizeof(struct stack));
scanf(%d,&newn->data);
newn->next=top;
top=newn;
}
void pop()
{
if(top= =NULL)
{
printf(stack is under flow);
}
else
{
p=top;
top=p->next;
printf(the popped elements is %d,p->data);
free(p);
}
printf(\n);
}
void display()
{
if(top= =NULL)
{
printf(stack is empty\n);
}
else
{
printf(the stack elements are:);
for(p=top;p!=NULL;p=p->next)
printf(\t%d,p->data);
printf(\n);
}
}
Sample output
STACK USING LINKED LIST
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter ur option: 1
Enter ur data 55
Enter ur option: 1
Enter ur data 35
Enter ur option: 1
Enter ur data 25
Enter ur option: 1
Enter ur data 45
Enter ur option: 3
Result
Thus the program was successfully executed
and the output was verified.
#include<process.h>
#include<malloc.h>
#define null 0
struct queue
{
int data;
struct queue *next;
}
*p,*front,*rear,*newn;
int ch=1,x;
void main()
{
void insert();
void delete();
void display();
clrscr();
printf(\n\t\t QUEUE USING LINKED LIST);
printf(\n1.Insert\n2.Delete\n3.Display\n4.Exit\n)
;
while(ch)
{
printf(Enter ur choice:);
scanf(%d,&ch);
switch(ch)
{
case 1: insert();break;
case 2: delete();break;
case 3: display(); break;
case 4: exit(0);
default:exit(0);
}
}
getch();
}
void insert()
{
printf(Enter ur data);
newn=malloc(sizeof(struct queue));
scanf(%d,&newn->data);
newn->next=null;
if(rear= =null)
{
front=newn;
rear=newn;
}
else
{
rear->next=newn;
rear=newn;
}
}
void delete()
{
if(front= =NULL)
{
printf(\nQueue is empty\n);
rear=null;
}
else
{
p=front;
x=p->data;
front=p->next;
printf(\n Deleted value is %d\n,x);
free(p);
}
}
void display()
{
if(front= =null)
{
printf(\nqueue is empty\n);
}
else
{
printf(The Queue elements are:);
for(p=front;p!=null;p=p->next)
printf(\t%d,p->data);
printf(\n);
}
}
Sample output
QUEUE USING LINKED LIST
1.Insert
2.Delete
3.Display
4.Exit
Enter ur option: 1
Enter ur data 55
Enter ur option: 1
Enter ur data 33
Enter ur option: 1
Enter ur data 44
Enter ur option: 1
Enter ur data 22
Enter ur option: 3
The queue elements are: 55 33 44 22
Enter ur option: 2
Deleted value is 55
Enter ur option: 2
Deleted value is 33
Enter ur option: 2
Deleted value is 44
Enter ur option: 2
Deleted value is 22
Enter ur option: 2
Queue is empty
Enter ur option: 3
Queue is empty
Enter ur option: 4
Result
Thus the program was successfully executed
and the output was verified.
Program
/*Binary Tree Traversal*/
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<process.h>
struct node
{
int data;
struct node *right,*left;
}
*root,*p,*q;
struct node *make(int y)
{
struct node *newnode;
newnode=(struct
node));
newnode->data=y;
node
*)malloc(sizeof(struct
newnode->right=newnode->left=NULL;
return(newnode);
}
void left(struct node *r, int x)
{
if(r->left!=NULL)
printf(\n Invalid !);
else
r->left=make(x);
}
void right(struct node *r,int x)
{
if(r->right!=NULL)
printf(\n Invalid !);
else
r->right=make(x);
}
void inorder(struct node *r)
{
if(r!=NULL)
{
inorder(r->left);
printf(\t %d,r->data);
inorder(r->right);
}
}
void preorder(struct node *r)
{
if(r!=NULL)
{
printf(\t %d,r->data);
preorder(r->left);
preorder(r->right);
}
}
void postorder(struct node *r)
{
if(r!=NULL)
{
postorder(r->left);
postorder(r->right);
printf(\t %d,r->data);
}
}
void display(struct node *r, int c, int r1, int w)
{
if(r!=NULL)
{
if(r)
{
gotoxy(c,r1);
printf(%d,r->data);
if(r->left)
display(r->left,c-w,r1+2,w/2);
if(r->right)
display(r->right,c+w,r1+2,w/2);
}
}
}
void main()
{
int no;
int choice;
clrscr();
printf(\n enter the root:);
scanf(%d,&no);
root=make(no);
p=root;
while(1)
{
printf(\n Enter another number:);
scanf(%d,&no);
if(no= =-1)
break;
p=root;
q=root;
while(no!=p->data && q!=NULL)
{
p=q;
if(no<p->data)
q=p->left;
else
q=p->right;
}
if(no<p->data)
{
printf(\n left branch of %d is %d,p->data,no);
left(p,no);
}
else
{
right(p,no);
printf(\n right branch of %d is %d,p->data,no);
}
}
while(1)
{
printf(\n 1.Inorder Traversal
\n 2.Preorder
Traversal\n 3.Postorder Traversal\n 4.Display\n
5.Exit);
printf(\n Enter choice:);
scanf(%d,&choice);
switch(choice)
{
case 1: inorder(root);
break;
case 2: preorder(root);
break;
case 3: postorder(root);
break;
case 4:
{
clrscr();
display(root,40,5,16);
getch();
break;
}
case 5: exit(0);
default:printf(Error ! Invalid Choice);
break;
}
getch();
}
}
Sample output
BINARY TREE TRAVERSAL
Enter the root: 5
Enter another number: 3
Left branch of 5 is 3
Enter another number: 6
Right branch of 5 is 6
Enter another number :2
Left branch of 3 is 2
Enter another number: 8
Right branch of 6 is 8
Enter another number: -1
1.Inorder traversal
2.Preorder traversal
3.Postorder traversal
4.display
5. exit
Enter choice: 4
5
3
1.Inorder traversal
2.Preorder traversal
3.Postorder traversal
4.display
5. exit
Enter choice:1
2
1.Inorder traversal
2.Preorder traversal
3.Postorder traversal
4.display
5. exit
Enter choice:2
5
1.Inorder traversal
2.Preorder traversal
3.Postorder traversal
4.display
5. exit
Enter choice:3
2
1.Inorder traversal
2.Preorder traversal
3.Postorder traversal
4.display
5. exit
Enter choice:5
Result
Thus the program was successfully executed
and the output was verified.
r->right=make(x);
}
void search(struct node *r,int t)
{
if(r!=NULL)
{
if(r->data= =t)
printf(Element found);
else if(r->data>t&&(r->left | | r->right!=NULL))
{
search(r->left,t);
}
else if(r->data<t&&(r->left | | r->right!=NULL))
search(r->right,t);
else
printf(Element not found);
}
}
int choice;
clrscr();
printf(\n enter the root:);
scanf(%d,& no);
root=make(no);
p=root;
while(1)
{
printf(\n Enter another number:);
scanf(%d,&no);
if(no= =-1)
break;
p=root;
q=root;
while(no!=p->data && q!=NULL)
{
p=q;
if(no<p->data)
q=p->left;
else
q=p->right;
}
if(no<p->data)
{
printf(\n Left branch of %d is %d,p->data,no);
left(p,no);
}
else
{
right(p,no);
printf(\n Right branch of %d is %d,p->data,no);
}
}
While(1)
{
printf(\n\n\n\n 1.search\n 2.display \n 3.exit);
}
case 3: exit(0);
default:printf(error ! invalid choice);
break;
}
getch();
}
}
Sample output:
BINARY SEARCH TREE
Enter the root: 5
Enter another number: 4
Left branch of 5 is 4
Enter another number: 6
Right branch of 5 is 6
Enter another number: 3
Left branch of 4 is 3
Enter another number: -1
1.search
2.display
3.exit
Enter choice: 2
5
4
3
1.search
2.display
3.exit
Enter choice: 1
Enter the element to be searched 3
Element found
1.search
2.display
3.exit
Enter choice: 1
Result
Thus the program was successfully executed
and the output was verified.
11(a).BUBBLE SORT
Aim
Write a C program to start the given set of
numbers using bubble sort technique.
Algorithm
Step 1: start the program.
Step 2: get the inputs for the array.
Step 3: bubble sort the smallest element to place
from sorting of the array by using a nested for loop
and swap the element in position i,j-1. If they are
out of orders.
Step 4: print the values of sorted elements.
Step 5: Stop the program.
Program
//Bubble sort
#include<stdio.h>
#include<conio.h>
void bubble (int a [],int n)
{
int i,j,t;
for(i=n-2;i>=0;i--)
{
for(j=0;j<=i;j++)
{
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}//end for 1.
}//end function.
void main()
{
int a[100],n,i;
clrscr();
printf(\n\n Enter integer value for total no.s of
elements to be sorted:);
scanf(%d,&n);
for(i=0;i<=n-1;i++)
{
printf(\n\n Enter integer value for element
no.%d:,i+1);
scanf(%d,&a[i]);
}
bubble(a,n);
printf(\n\n Finally sorted array is:);
for(i=0;i<=n-1;i++)
printf(%3d,a[i]);
getch();
}// end program.
Sample output
BUBBLE SORT
Enter integer value for total no.s of elements to be
sorted: 6
Enter integer value for element no.1: 88
Enter integer value for element no.2: -8
Enter integer value for element no.3: -66
Enter integer value for element no.4: 10
Enter integer value for element no.5: 77
Enter integer value for element no.6: 20
Finally sorted array is: -66 -8 10 20 77 88
Result
Thus the program was successfully executed
and the output was verified.
11(b).MERGE SORT
Aim
To sort the given set of numbers using merge
sort technique.
Algorithm
Step 1: start the program.
Step 2: get the input values of 2 arrays as a [m]
and b[n] and n as its array size.
Step 3: sort the contents of array a[] and b[].
Step 4: in merge()
(i). it contains two sorted arrays a[m]
and b[n] into one.
(ii). Scan both arrays from left to right.
(iii).compare a[1] with b[1] if a[1] put
a[1] as a[1] as compare a[2] with b[1]
and so on. Else put b[1] as c[1] and
compare a[1] with b[2] and so on.
(iv).repeat step until any one array
becomes empty.
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
return 0;
}
merge(int a[],int b[],int n,int c[])
{
int i,j,t,k=0;
for(i=0;i<n;i++)
{
{
if(a[i]>b[i])
{
c[k]=b[i];
k++;
c[k]=a[i];
k++;
}
else
{
c[k]=a[i];
k++;
c[k]=b[i];
k++;
}
}
}
return c;
}
Sample output
MERGE SORT
Enter the array size 5
Enter the 5 values
55 11 22 66 44
Enter the another 5 values
21 07 59 88 99
The sorted values are:
7
11
21
22
44
55
59
66
88
99
Result
Thus the program was successfully executed
and the output was verified.
Program
//Quick sort
#include<stdio.h>
#include<stdlib.h>
void swap(int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
int choose_pivot(int i,int j)
{
return((i+j)/2);
}
void quicksort(int list[],int m,int n)
{
int key,i,j,k;
if(m<n)
{
k=choose_pivot(m,n);
swap(&list[m],&list[k]);
key=list[m];
i=m+1;
j=n;
while(i<=j)
{
while((i<=n)&&(list[i]<=key))
i++;
while((j>=m)&&(list[j]>key))
j--;
if(i<j)
swap(&list[i],&list[j]);
}
swap(&list[m],&list[j]);
//recursively sort the lesser list
quick sort(list,m,j-1);
quick sort(list,j+1,n);
}
}
void printlist(int list[],int n)
{
int i;
for(i=0;i<n;i++)
printf(%d\t,list[i]);
}
void main()
{
int list[100],n;
int i=0;
clrscr();
printf(enter the maximum number of elements to
be sorted);
scanf(%d,&n);
for(i=0;i<n;i++)
{
scanf(%d,&list[i]);
}
printf(The list before sorting is:\n);
printlist(list,n);
quicksort(list,0,n-1);
printf(\n The list after sorting using quicksort
algorithm:\n);
printlist(list,n);
getch();
}
Sample output
QUICK SORT
Enter the maximum number of elements to be
sorted 5
50
11
88
77
22
The list before sorting is:
50
11
88 77 22
22
50 77
88
Result
Thus the program was successfully executed
and the output was verified.
printf("\tArray[%d] = ",i);
scanf("%d",&array[i]);
}
printf("\n Array Before Radix Sort:"); //Array
Before Radix Sort
for(i =0;i<n;i++)
{
printf("%8d", array[i]);
}
printf("\n");
radix_sort(array,n);
printf("\n Array After Radix Sort: "); //Array
After Radix Sort
for(i =0;i<n;i++)
{
printf("%8d", array[i]);
}
printf("\n");
getch();
}
radix_sort(int arr[], int n)
{
int bucket[10][5],buck[10],b[10];
int i,j,k,l,num,div,large,passes;
div=1;
num=0;
large=arr[0];
for(i=0; i<n; i++)
{
if(arr[i] > large)
{
large = arr[i];
}
while(large > 0)
{
num++;
large = large/10;
}
for(passes=0; passes<num; passes++)
{
for(k=0;k<10;k++)
{
buck[k] = 0;
}
for(i=0;i<n;i++)
{
l = ((arr[i]/div)%10);
bucket[l][buck[l]++] = arr[i];
}
i=0;
for(k=0;k<10;k++)
{
for(j=0;j<buck[k];j++)
{
arr[i++] = bucket[k][j];
}
}
div*=10;
}
}
}
Sample output
Enter the number of elements to be sorted: 7
Enter the elements to be sorted:
Array[0]=777
Array[1]=269
Array[2]=158
Array[3]=341
Array[4]=265
Array[5]=989
Array[6]=506
Array before radix sort: 777 269 158 341 265 989
506
Array after radix sort: 158 265 269 341 506 777
989
Result
Thus the program was successfully executed
and the output was verified.
12(a).Linear Search
Aim
To develop a C program to implement linear
search.
Algorithm
Step 1: Get the array elements.
Step 2: Get the element to search.
Step 3: Apply for loop differentiate the array
element and apply the below condition in for loop.
(i).check the array element is equal to
search element then display element found
Else
Display element not found.
Step 4: Stop the program.
Program
//Linear search
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,item,a[20];
clrscr();
printf(\n Enter no of elements=);
scanf(%d,&n);
printf(\n Enter %d elements=,n);
for(i=0;i<n;i++)
{
scanf(%d,&a[i]);
}
printf(\n Enter the element to be search=);
scanf(%d,&item);
for(i=0;i<n;i++)
{
if(a[i]= =item)
{
Result
Thus the program was successfully executed and
the output was verified.
12(b).BINARY SEARCH
Aim
To develop a C program to implement binary
search.
Algorithm
Step 1: Get the array elements.
Step 2: Get the elements to search.
Step 3: Sort the array element using any sorting
techniques.
Step 4: Get the mid value by (low+high)/2.
Step 5: Compare the search element with that mid
elements.
Step 6: Check the given value is less than the mid
element, then find mid=(low+mid-1)/2 else, find
mid=(mid+1+high/2).
Step 7: Repeat the steps 4 to 6 until the element is
found or high<low.
Step 8: Check the element is not present,display
,element not found,else displayelement found.
printf(\n\tElement found);break;
}
if(i= =(b+1))
printf(\nElement not found);
}
}
else if(a[b]<item)
{
for(i=b;i<=(n+1);i++)
{
if(a[i]= =item&&(i!=(n+1)))
{
printf(\n\tElement found);break;
}
If(i= =(n+1))
printf(\nElement not found);
}
}
else
printf(\n\tElement not found);
getch();
}
sort(int a[],int n)
{
int i,j,t;
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
return 0;
}
Sample output
BINARY SEARCH
Enter no of elements = 5
Enter 5 elements = 22 11 55 44 33
The sorted values are:
11
22
33
44
55
Enter the element to be search= 33
Element found
Enter no of elements = 5
Enter 5 elements = 22 11 55 44 33
Result
Thus the program was successfully executed
and the output was verified.