Download as pdf or txt
Download as pdf or txt
You are on page 1of 47

Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3

Practical no.1: Write a C program to perform following operations on


array
1(a): To create, display and reverse the array

#include<stdio.h>
int main()
{
int a[10],n,i;
printf("Enter the size of the array : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\nArray element in proper order : ");
for(i=0;i<n;i++)
{
printf("%d",a[i]);
}
printf("\nArray element in reverse order : ");
for(i=n-1 ;i>=0; i--)
{
printf("%d",a[i]);
}
return 0;

Output

12002040701170 Page 1
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3

1(b): To insert and delete a particular element from given position in the
array.

#include<stdio.h>
int a[20],i,n,pos,val;
void insertinarray(int position,int value)
{
i=n;
while(i>=position)
{
a[i+1]=a[i];
i--;
}
n++;
a[pos]=value;
}
void deleteinarray(int positon,int n)
{
i=positon;
while(i<=n-1)
{
a[i]=a[i+1];
i++;
}
n--;
}
int main()
{
int c;
printf("how much element you want to enter : ");
scanf("%d",&n);
printf("\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("elments are\n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
do
{
printf("enter the case : ");
scanf("%d",&c);
switch (c)
{

12002040701170 Page 2
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3

case 1:
printf("enter the position : ");
scanf("%d",&pos);
printf("enter the value : ");
scanf("%d",&val);
insertinarray(pos,val);
for(i=0;i<n;i++)
{
printf("%d",a[i]);
}
printf("\n");
break;
case 2:
printf("enter the position : ");
scanf("%d",&pos);
deleteinarray(pos,val);
for(i=0;i<n;i++)
{
printf("%d",a[i]);
}
printf("\n");
break;
case 3:
printf("exit");
}
}while(c!=3);
return 0;

Output

12002040701170 Page 3
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3

1(c): To search a particular element from the list and print its position in
the array.

#include<stdio.h>
int main()
{
int a[10];
int i,n,element;
printf("Enter the size of the array : ");
scanf("%d",&n);
printf("\nEnter the element you want to search\n");
for (int i = 0; i < n; i++)
{
scanf("%d",&a[i]);
}
printf("\nEnter the element you want to search : ");
scanf("%d",&element);
for (i = 0; i < n; i++)
{
if(element==a[i])
{
printf("\nThe position of the element is : %d ",i+1);
break;
}
if(i==n-1)
{
printf("\n Element not found ");
}
}
return 0;
}

12002040701170 Page 4
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3

Output

12002040701170 Page 5
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3

1(d): To merge two sorted arrays and sort the merged array

#include<stdio.h>
int main()
{
int a[10],b[10],c[10];
int i,j,k;
int m,n,q;
q=m+n;
printf("Enter the size of array 1 : ");
scanf("%d",&n);
printf("\nEnter the size of array 2 : ");
scanf("%d",&m);
printf("\nElements of array 1 \n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\nElements of array 2 \n");
for(j=0;j<m;j++)
{
scanf("%d",&b[j]);
}
// Merged the Sorted array
i=0;
j=0;
k=0;
while (i<n && j<m)
{
if (b[j]>a[i])
{
c[k]=a[i];
i++,k++;
}
else
{
c[k]=b[j];
k++,j++;
}
}
while (i < 3)
{
c[k] = a[i];
k++,i++;
}
while (j < 2)

12002040701170 Page 6
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3

{
c[k] = b[j];
k++,j++;
}
for(k=0;k<m+n;k++)
{
printf("%d",c[k]);
}
return 0;
}

// To merge two sorted arrays and sort the merged array. (part 2)
#include<stdio.h>
int main()
{
int a[10],b[10],c[10];
int i,j,k=0;
int m,n,q,p,temp;
q=m+n;
printf("Enter the size of array 1 : ");
scanf("%d",&n);
printf("\nEnter the size of array 2 : ");
scanf("%d",&m);
printf("\nElements of array 1 \n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\nElements of array 2 \n");
for(j=0;j<m;j++)
{
scanf("%d",&b[j]);
}
for(i=0;i<n;i++)
{
c[k]=a[i];
k++;
}
k=n;
for(j=0;j<m;j++)
{
c[k]=b[j];
k++;
}
printf("\nMerged array is : ");
for(k=0;k<m+n;k++)
{
printf("%d",c[k]);

12002040701170 Page 7
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3

}
printf("\nSorted merged array is : ");
for(k=0;k<m+n-1;k++)
{
for(p=k+1;p<m+n;p++)
{
if(c[p]<c[k])
{
temp = c[k];
c[k]=c[p];
c[p]=temp;
}
}
}
for(k=0;k<m+n;k++)
{
printf("%d",c[k]);
}
}

Output

12002040701170 Page 8
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3

Practical no. 2: Write a C program to implement the stack which should


include following functions:
a. Push() : insert an element into the stack
b. Pop() : return and then delete an element from the stack
c. Display(): display the entire content of the stack
d. Peep(): display the element on the top of the stack.
#include<stdio.h>
#define max 3
int stack[max],top=-1,value;
void push(int stack[],int value)
{
if(top==max-1)
{
printf("\nStack Overflow\n");
}
else
{
top++;
stack[top]=value;
}
}
int pop (int stack[])
{
if(top==-1)
{
printf("\nStack underflow\n");
}
else
{
value=stack[top];
top--;
return value;
}
}
void display(int stack[])
{
if(top==-1)
{
printf("\nEmpty Stack\n");
}
else
{
int i;
for(i=top;i>=0;i--)
{
printf("\n%d\n",stack[i]);
}

12002040701170 Page 9
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3

}
}
int peek (int stack[])
{
if(top==-1)
{
printf("\nEmpty Stack\n");
}
else
{
return (stack[top]);
}
}
int main()
{
int c;
do
{
printf("Enter the case : ");
scanf("%d",&c);
switch (c)
{
case 1:
printf("Enter the value : ");
scanf("%d",&value);
push(stack,value);
break;
case 2:
value=pop(stack);
if (value!=-1)
{
printf("\nthe value deleted from stack is : %d ",value);
}
break;
case 3: value=peek(stack);
if (value!=-1)
{
printf("\nthe value deleted from stack is : %d ",value);
}
case 4:
display(stack);
}
} while (c!=5);
return 0;
}

12002040701170 Page 10
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3

Output

12002040701170 Page 11
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3

Practical no. 3: Write a C program to convert given infix expression to


postfix using stack.

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

#define SIZE 100

char stack[SIZE];
int top = -1;

void push(char item)


{
if(top >= SIZE-1)
{
printf("\nStack Overflow.");
}
else
{
top = top+1;
stack[top] = item;
}
}

char pop()
{
char item ;

if(top <0)
{
printf("stack under flow: invalid infix expression");
getchar();
/* underflow may occur for invalid expression */
/* where ( and ) are not matched */
exit(1);
}
else
{
item = stack[top];
top = top-1;
return(item);
}

12002040701170 Page 12
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3

int is_operator(char symbol)


{
if(symbol == '^' || symbol == '*' || symbol == '/' || symbol == '+' || symbol =='-')
{
return 1;
}
else
{
return 0;
}
}

int precedence(char symbol)


{
if(symbol == '^')
{
return(3);
}
else if(symbol == '*' || symbol == '/')
{
return(2);
}
else if(symbol == '+' || symbol == '-')
{
return(1);
}
else
{
return(0);
}
}

void InfixToPostfix(char infix_exp[], char postfix_exp[])


{
int i, j;
char item;
char x;

push('(');
strcat(infix_exp,")");

i=0;
j=0;
item=infix_exp[i];

while(item != '\0')
{
if(item == '(')

12002040701170 Page 13
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3

{
push(item);
}
else if( isdigit(item) || isalpha(item))
{
postfix_exp[j] = item;
j++;
}
else if(is_operator(item) == 1)
{
x=pop();
while(is_operator(x) == 1 && precedence(x)>= precedence(item))
{
postfix_exp[j] = x;
j++;
x = pop();
}
push(x);

push(item);
}
else if(item == ')')
{
x = pop();
while(x != '(')
{
postfix_exp[j] = x;
j++;
x = pop();
}
}
else
{
printf("\nInvalid infix Expression.\n");
getchar();
exit(1);
}
i++;

item = infix_exp[i];
}
if(top>0)
{
printf("\nInvalid infix Expression.\n");
getchar();
exit(1);
}
if(top>0)
{

12002040701170 Page 14
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3

printf("\nInvalid infix Expression.\n");


getchar();
exit(1);
}

postfix_exp[j] = '\0';

int main()
{
char infix[SIZE], postfix[SIZE];

printf("ASSUMPTION: The infix expression contains single letter variables and single digit
constants only.\n");
printf("\nEnter Infix expression : ");
gets(infix);

InfixToPostfix(infix,postfix);
printf("Postfix Expression: ");
puts(postfix);

return 0;
}

Output

12002040701170 Page 15
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3

Practical no. 6: Write a C program to evaluate postfix expression using


stack.

#include<stdio.h>
int stack[20];
int top = -1;

void push(int x)
{
stack[++top] = x;
}

int pop()
{
return stack[top--];
}

int main()
{
char exp[20];
char *e;
int n1,n2,n3,num;
printf("Enter the expression :: ");
scanf("%s",exp);
e = exp;
while(*e != '\0')
{
if(isdigit(*e))
{

12002040701170 Page 16
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3

num = *e - 48;
push(num);
}
else
{
n1 = pop();
n2 = pop();
switch(*e)
{
case '+':
{
n3 = n1 + n2;
break;
}
case '-':
{
n3 = n2 - n1;
break;
}
case '*':
{
n3 = n1 * n2;
break;
}
case '/':
{
n3 = n2 / n1;
break;
}
}

12002040701170 Page 17
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3

push(n3);
}
e++;
}
printf("\nThe result of expression %s = %d\n\n",exp,pop());
return 0;
}

OUTPUT

12002040701170 Page 18
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3

Practical no. 5: Write a program to implement QUEUE using arrays that


performs following operations
a. INSERT : to insert element in the queue
b. DELETE: to delete an element from the queue
c. DISPLAY: to display the queue content

#include<stdio.h>
#define n 5
int main()
{
int queue[n],ch=1,front=0,rear=0,i,j=1,x=n;
printf("Queue using Array");
printf("\n1.Insertion \n2.Deletion \n3.Display \n4.Exit");
while(ch)
{
printf("\nEnter the Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(rear==x)
printf("\n Queue is Full");
else
{
printf("\n Enter no %d:",j++);
scanf("%d",&queue[rear++]);
}
break;
case 2:

12002040701170 Page 19
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3

if(front==rear)
{
printf("\n Queue is empty");
}
else
{
printf("\n Deleted Element is %d",queue[front++]);
x++;
}
break;
case 3:
printf("\nQueue Elements are:\n ");
if(front==rear)
printf("\n Queue is Empty");
else
{
for(i=front; i<rear; i++)
{
printf("%d",queue[i]);
printf("\n");
}
break;
case 4:
exit(0);
default:
printf("Wrong Choice: please see the options");
}
}
}
return 0;}

12002040701170 Page 20
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3

OUTPUT

12002040701170 Page 21
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3

Practical no. 6: Write a program to implement Circular Queue using


arrays that performs following operations.
(a) INSERT (b) DELETE (c) DISPLAY

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

#define max 5

int front=-1,rear=-1; // global variable


int CQueue[max];

void insert();
int delete();
void display();

int main()
{
int w,no;
for(;;)
{
printf("\n1. Insert");
printf("\n2. Delete");
printf("\n3. Display");
printf("\n4. EXIT");
printf("\nEnter what you want :");
scanf("%d",&w);

switch(w)

12002040701170 Page 22
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3

{
case 1:
insert();
break;
case 2:
no=delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("\nInvalid Choice !!\n");
}
}
}

void insert()
{
int no;
if((front ==0 && rear == max-1) || front == rear+1)
{
printf("\nCircular Queue Is Full !\n");
return;
}
printf("\nEnter a number to Insert :");
scanf("%d",&no);
if(front==-1)
front=front+1;

12002040701170 Page 23
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3

if(rear==max-1)
rear=0;
else rear=rear+1;
CQueue[rear]=no;
}

int delete()
{
int e;
if(front==-1)
{
printf("\nThe Circular Queue is Empty !!\n");

}
e=CQueue[front];
if(front==max-1)
front=0;
else if(front==rear)
{
front=-1;
rear=-1;
}
else front=front+1;
printf("\n%d was deleted !\n",e);
return e;
}

void display()
{
int i;

12002040701170 Page 24
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3

if(front==-1)
{
printf("\nThe Circular Queue is Empty ! Nothing To Display !!\n");
return;
}
i=front;
if(front<=rear)
{
printf("\n\n");
while(i<=rear)
printf("%d ",CQueue[i++]);
printf("\n");
}
else
{
printf("\n\n");
while(i<=max-1)
printf("%d ",CQueue[i++]) ;
i=0;
while(i<=rear)
printf("%d ",CQueue[i++]);
printf("\n");
}
}

12002040701170 Page 25
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3

OUTPUT

12002040701170 Page 26
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3

Practical no. 7: Write a menu driven program to implement following


operations on the singly linked list.
a. Insert a node at the front of the linked list.
b. Insert a node at the end of the linked list.
c. Insert a node such that linked list is in ascending
order.(According to info. Field)
d. Delete a first node of the linked list.
e. Delete a node before specified position.
f. Delete a node after specified position.

//singly linked list


#include <stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node* link;
};
struct node* start = NULL;
void traverse()
{
struct node* temp;
if (start == NULL)
printf("\nList is empty\n");
else
{
temp = start;
while (temp != NULL)
{
printf("Data = %d\n",temp->info);
temp = temp->link;
}
}
}
void insertAtFront()
{
int data;
struct node* temp;
temp = malloc(sizeof(struct node));
printf("\nEnter number to be inserted : ");
scanf("%d", &data);
temp->info = data;
temp->link = start;
start = temp;
}
{
int data;
struct node *temp, *head;
12002040701170 Page 27
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3

temp = malloc(sizeof(struct node));


printf("\nEnter number to be inserted : ");
scanf("%d", &data);
temp->link = 0;
temp->info = data;
head = start;
while (head->link != NULL)
{
head = head->link;
}
head->link = temp;
}
void sort()
{
struct node* current = start;
struct node* index = NULL;
int temp;
if (start == NULL)
{
return;
}
else
{
int data;
struct node *temp, *head;
temp = malloc(sizeof(struct node));
printf("\nEnter number to be inserted : ");
scanf("%d", &data);
temp->link = 0;
temp->info = data;
head = start;
while (head->link != NULL)
{
head = head->link;
}
head->link = temp;
}
void sort()
{
struct node* current = start;
struct node* index = NULL;
int temp;
if (start == NULL)
{
return;
}
else
free(temp);
}
}

12002040701170 Page 28
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3

void deleteEnd()
{
struct node *temp, *prevnode;
if (start == NULL)
printf("\nList is Empty\n");
else
{
temp = start;
while (temp->link != 0)
{
prevnode = temp;
temp = temp->link;
}
free(temp);
prevnode->link = 0;
}
}
void deletePosition()
{
struct node *temp, *position;
int i = 1, pos;
if (start == NULL)
printf("\nList is empty\n");
else
{
printf("\nEnter index : ");
scanf("%d", &pos);
position = malloc(sizeof(struct node));
temp = start;
while (i < pos - 1)
{
temp = temp->link;
i++;
}
position = temp->link;
temp->link = position->link;
free(position);
}
}
int main()
{
int choice;
while (1)
{
printf("\t1 To see data\n");
printf("\t2 For insertion at starting\n");
printf("\t3 For insertion at end\n");
printf("\t4 To sort\n");
printf("\t5 For deletion of first element\n");
printf("\t6 For deletion of last element\n");

12002040701170 Page 29
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3

printf("\t7 For deletion of element at any position\n");


printf("\t8 To exit\n");
printf("\nEnter Choice :\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
traverse();
break;
case 2:
insertAtFront();
break;
case 3:
insertAtEnd();
break;
case 4:
sort();
break;
case 5:
deleteFirst();
break;
case 6:
deleteEnd();
break;
case 7:
deletePosition();
break;
case 8:
exit(1);
break;
default:
printf("Incorrect Choice\n");
}
}
return 0;
}

12002040701170 Page 30
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3

Output:-

12002040701170 Page 31
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3

12002040701170 Page 32
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3

Practical no.8: Write a program to implement following operations on the


doubly linked list.
a. Insert a node at the front of the linked list.
b. Insert a node at the end of the linked list.
c. Delete a last node of the linked list.

//Doubly Linked List


#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *prev, *next;
};
struct node* start = NULL;
void traverse()
{
if (start == NULL)
{
printf("\nList is empty\n");
return;
}
struct node* temp;
temp = start;
while (temp != NULL)
{
printf("Data = %d\n", temp->info);
temp = temp->next;
}
}
void insertAtFront()
{
int data;
struct node* temp;
temp = (struct node*)malloc(sizeof(struct node));
printf("\nEnter number to be inserted: ");
scanf("%d", &data);
temp->info = data;
temp->prev = NULL;
temp->next = start;
start = temp;
}
void insertAtEnd()
{

12002040701170 Page 33
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3

int data;
struct node *temp, *trav;
temp = (struct node*)malloc(sizeof(struct node));
temp->prev = NULL;
temp->next = NULL;
printf("\nEnter number to be inserted: ");
scanf("%d", &data);
temp->info = data;
temp->next = NULL;
trav = start;
if (start == NULL)
{
start = temp;
}
else
{
while (trav->next != NULL)
trav = trav->next;
temp->prev = trav;
trav->next = temp;
}
}
void deleteEnd()
{
struct node* temp;
if (start == NULL)
printf("\nList is empty\n");
temp = start;
while (temp->next != NULL)
temp = temp->next;
if (start->next == NULL)
start = NULL;
else
{
temp->prev->next = NULL;
free(temp);
}
}
void deletePosition()
{
int pos, i = 1;
struct node *temp, *position;
temp = start;
if (start == NULL)
printf("\nList is empty\n");
else
{
printf("\nEnter position : ");
scanf("%d", &pos);

12002040701170 Page 34
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3

if (pos == 1)
{
deleteEnd();
if (start != NULL)
{
start->prev = NULL;
}
free(position);
return;
}
while (i < pos - 1)
{
temp = temp->next;
i++;
}
position = temp->next;
if (position->next != NULL)
position->next->prev = temp;
temp->next = position->next;
free(position);
}
}
int main()
{
int choice;
while (1)
{
printf("\n\t1 To see list\n");
printf("\t2 For insertion at starting\n");
printf("\t3 For insertion at end\n");
printf("\t4 For deletion of last element\n");
printf("\t5 For deletion of element at any position\n");
printf("\t6 To exit\n");
printf("\nEnter Choice :\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
traverse();
break;
case 2:
insertAtFront();
break;
case 3:
insertAtEnd();
break;;
case 4:
deleteEnd();
break;

12002040701170 Page 35
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3

case 5:
deletePosition();
break;
case 6:
exit(1);
break;
default:
printf("Incorrect Choice.\n");
continue;
}
}
return 0;
}

12002040701170 Page 36
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3

Output

12002040701170 Page 37
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3

12002040701170 Page 38
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3

12002040701170 Page 39
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3

Practical no. 9: Write a program to implement linear search and the binary
search.

#include<stdio.h>
void main()
{
int a[25],beg,item,last,n,num,i,ch,mid,f=0;
printf("menu\n");
printf("\n 1.linear search");
printf("\n 2.binary search");
printf("\n enter the choice");
scanf("%d",&ch);
if(ch==1)
{
printf("\n enter the number of elements in the array");
scanf("%d",&n);
printf("\n enter the sorted array");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n enter the item to be searched");
scanf("%d",&item);
for(i=0;i<n;i++)
{
if(a[i]==item)
{
printf("\n item found at position%d",i+1);
break;
}
}

12002040701170 Page 40
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3

if(i==n)
printf("\n item not found");
}
if(ch==2)
{
printf("\nenter the number of elements in the array");
scanf("%d",&n);
printf("enter the sorted array");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("item to be searched");
scanf("%d",&item);
last=n-1;
mid=(beg+last)/2;
while(beg<=last)
{
if(item==a[mid])
{
printf("\n item found at position %d",mid+1);
break;
}
else if(a[mid]>item)
last=mid-1;
else beg=mid+1;
mid=(beg+last)/2;
}
}
}

12002040701170 Page 41
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3

Output

12002040701170 Page 42
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3

Practical no. 10: Write a program to implement the Bubble Sort and
selection sort.

#include<stdio.h>
#include<stdlib.h>
void display(int a[],int n);
void bubble_sort(int a[],int n);
void selection_sort(int a[],int n);
void insertion_sort(int a[],int n);

//-----------------Main Function----------------------
int main()
{
int n,choice,i;
char ch[20];

printf("Enter no. of elements u want to sort : ");


scanf("%d",&n);
int arr[n];

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

printf("Please select any option Given Below for Sorting : \n");

while(1)

12002040701170 Page 43
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3

{
printf("\n1. Bubble Sort\n2. Selection Sort\n3. Insertion Sort\n4. Display Array.\n5. Exit
the Program.\n");
printf("\nEnter your Choice : ");
scanf("%d",&choice);

switch(choice)
{
case 1:
bubble_sort(arr,n);
break;

case 2:
selection_sort(arr,n);
break;

case 3:
display(arr,n);
break;

case 4:
return 0;

default:
printf("\nPlease Select only 1-5 option ----\n");
}
}
return 0;
}

12002040701170 Page 44
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3

//-----------End of main function---------------------

//-------------------Display Function-----------------

void display(int arr[],int n)


{

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

//---------------------Bubble Sort Function-----------


void bubble_sort(int arr[],int n)
{
int i,j,temp;

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

12002040701170 Page 45
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3

}
}

printf("After Bubble sort Elements are : ");


display(arr,n);
}

//------------------Selection Sort Function---------


void selection_sort(int arr[],int n)
{
int i,j,temp;

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

printf("After Selection sort Elements are : ");


display(arr,n);
}

12002040701170 Page 46
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3

OUTPUT

12002040701170 Page 47

You might also like