0% found this document useful (0 votes)
53 views63 pages

Data Structures Practfile by Zoraver Eee - 015 Zoraver Singh Eee

Uploaded by

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

Data Structures Practfile by Zoraver Eee - 015 Zoraver Singh Eee

Uploaded by

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

Data Structures

Practical File

Name:- Zoraver Singh Virdi

Branch:- EEE

Enrollment No:-
01513204920
INDEX

1
Implementation of an Array and 26/08/21
deletion from a specified location
2
Implementation of a matrix ,and see 26/08/21
wether it is a sparse matrix or not
3 Implementation of Linear Search 02/09/21
4 Implementation of Binary Search 02/09/21
5 Implementation of stack as an array 09/09/21
6
Implementation of stack as a linked 16/09/21
list
7
Implementation of insertion , 07/10/21
deletion on linked list.
8 Implementation of Queue. 21/10/21
9
Implementation of Queue as a linked 27/10/21
list
10
Implementation of circular Queue 11/11/21
with stack
11
Binary search tree insertion deletion 26/11/21
and traversal
12 Implementation of heap sort 02/12/21
13 Implementation of insertion sort 09/12/21
14 Implementation of quick sort 09/12/21
15 Implementation of Selection sort 16/12/21
16 Implementation of Bubble sort 16/12/21
17 Implementation of Merge sort 23/12/21
18 Implemenation of Radix/Bucket sort 23/12/21

Experiment 1
AIM: Implementation of an Array and deletion from a
specified location.

Software used: VS code


CODE:-

#include<stdio.h>
#include<conio.h>
void insert_ele(int i,int arr[],int len)
{
int ele,loc;
printf("\n enter the element to be inserted \n");
scanf("%d",&ele);
printf("\n enter the location\n");
scanf("%d",&loc);
if(i==19)
{
printf("\nOverflow\n");
}
else
{
while(i>=loc)
{
arr[i+1]=arr[i];
i--;
}
}
arr[loc]=ele;
len++;
printf("\n New array: ");
for(i=0;i<len;i++)
{
printf("%d ",arr[i]);
}
}
void delete_ele(int i,int arr[],int len)
{
int loc;
printf("enter the location of element to be deleted \n");
scanf("%d ",&loc);
if(len==0)
{
printf("\n Underflow");

}
else
{
for(i=loc;i<len;i++)
{
arr[i]=arr[i+1];

}
len--;
printf("\n new array is\n");
for(i=0;i<len;i++)
{
printf("\t%d ",arr[i]);
}
}
}
void main(){
int arr[20], i=0, len=0, ch;
char choice;
printf("\nEnter number of elements you want to insert : ");
scanf("%d",&len);
for(i=0;i<len;i++)
{
printf("enter the element %d :",i);
scanf("%d",&arr[i]);
}
i--;
label:
printf("\n 1.To insert element,2.To delete \n");
scanf("%d",&ch);
switch(ch)
{
case 1:
insert_ele(i,arr,len);
break;
case 2:
delete_ele(i,arr,len);
break;
default:
printf("\n Invalid operation\n");
break;
}
printf("\n press y to continue or any other key to quit : ");
scanf("%s",&choice);
if(choice=='y'||choice=='Y')
{
goto label;
}
}

OUTPUT

EXPERIMENT 2
AIM: Implementation of a matrix ,and see wether it is
a sparse matrix or not.

Software used: VS code

CODE:

#include<stdio.h>
void main()
{
int matrix[10][10];
int i,j,m,n;
int sparse_counter=0;
printf("enter the orer of matrix : \n");
scanf("%d%d",&m,&n);
printf("Enter the elements of the matrix\n");
for(i=0;i<m;++i)
{
for(j=0;j<n;++j)
{
scanf("%d",&matrix[i][j]);
if(matrix[i][j]==0)
{
++sparse_counter;
}

}
}
if(sparse_counter>((m*n)/2))
{
printf("The given matrix is sparse matrix!!!\n");
}
else
{
printf("The given matrix is not a sparse matrix\n");
}
printf("There are %d no of zeroes.",sparse_counter);
}

OUTPUT
EXPERIMENT 3
AIM: Implementation of Linear Search.

Software used: VS code.


CODE:

#include<stdio.h>

int main()
{
int arr[100],search,c,n;
printf("Enter no of elements\n ");
scanf("%d",&n);
printf("Enter %d integer(s)\n",n);

for(c=0;c<n;c++)
{
scanf("%d",&arr[c]);
}
printf("Enter the element to search for \n");
scanf("%d",&search);

for(c=0;c<n;c++)
{
if(arr[c]==search)
{
printf("%d is present at location %d\n",search,c+1);
break;
}
}
if(c==n)
{
printf("%d isnt present in the array.\n",search);
}
return 0;
}

OUTPUT
EXPERIMENT 4
AIM: Implementation of Binary Search.

Software used: VS code.

CODE:

#include<stdio.h>
int main()
{
int c,first,l,last,middle,n,search,arr[100];
printf("Enter No of elements \n");
scanf("%d",&n);

printf("Enter %d Integers\n",n);
for(c=0;c<n;c++)
{
scanf("%d",&arr[c]);
}
printf("Enter the value to find :\n");
scanf("%d",&search);

first=0;
last=n-1;
middle=(first+last)/2;

while(first<=last)
{
if(arr[middle<search])
{
first=middle+1;
}
else if(arr[middle]==search)
{
printf("%d found at location %d.\n",search,middle+1);
break;
}
else
{
last=middle-1;

middle=(first+last)/2;
}
}
if(first>last)
{
printf("Not found ! %d isn't present in the list\n",search);
}
return 0;
}
OUTPUT

EXPERIMENT 5
AIM: Implementation of stack as an array.

Software used: VS code.

CODE:

#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{

top=-1;
printf("\n Enter the size of STACK[MAX=100]:");
scanf("%d",&n);
printf("\n\t STACK OPERATIONS USING ARRAY");
printf("\n\t--------------------------------");
printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");
do
{
printf("\n Enter the Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("\n\t EXIT POINT ");
break;
}
default:
{
printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");
}

}
}
while(choice!=4);
return 0;
}
void push()
{
if(top>=n-1)
{
printf("\n\tSTACK is over flow");

}
else
{
printf(" Enter a value to be pushed:");
scanf("%d",&x);
top++;
stack[top]=x;
}
}
void pop()
{
if(top<=-1)
{
printf("\n\t Stack is under flow");
}
else
{
printf("\n\t The popped elements is %d",stack[top]);
top--;
}
}
void display()
{
if(top>=0)
{
printf("\n The elements in STACK \n");
for(i=top; i>=0; i--)
printf("\n%d",stack[i]);
printf("\n Press Next Choice");
}
else
{
printf("\n The STACK is empty");
}

OUTPUT
EXPERIMENT 6
AIM: Implementation of stack as a linked list.

Software used: VS code.

CODE:

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node*next;

};
struct node*front;
struct node*rear;
void insert();
void delete();
void display();
void main()
{
int choice;
while(choice!=4)
{
printf("\n********Main menu***********");
printf("\n1.insert an element\n2.delete an
element\n3.display\n4.exit the program\n");
printf("\n enter yoiur choice: ");
printf("\n============================");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\n enter a valid choice: \n");
}
}
}
void insert()
{
struct node*ptr;
int item;
ptr=(struct node*)malloc(sizeof(struct node));
if(ptr==NULL)
{
printf("\nOVERFLOW\n");
return;
}
else
{
printf("\n enter a value?\n");
scanf("%d",&item);
ptr->data=item;
if(front==NULL)
{
front=ptr;
rear=ptr;
front->next=NULL;
rear->next=NULL;
}
else
{
rear->next=ptr;
rear=ptr;
rear->next=NULL;
}
}
}
void delete()
{
struct node*ptr;
if(front==NULL)
{
printf("\nUNDERFLOW");
return;
}
else
{
ptr=front;
front=front->next;
free(ptr);
}
}
void display()
{
struct node*ptr;
ptr=front;
if(front==NULL)
{
printf("\nEMPTY QUEUE\n");
}
else
{
printf("\n printing values...\n");
while(ptr!=NULL)
{
printf("%d ",ptr->data);
ptr=ptr->next;
}
}
}

OUTPUT
EXPERIMENT 7
AIM: Implementation of insertion , deletion on linked
list.

Software used: VS code.

CODE:

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node*next;

};
struct node*front;
struct node*rear;
void insert();
void delete();
void display();
void main()
{
int choice;
while(choice!=4)
{
printf("\n********Main menu***********");
printf("\n1.insert an element\n2.delete an
element\n3.display\n4.exit the program\n");
printf("\n enter yoiur choice: ");
printf("\n============================");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\n enter a valid choice: \n");
}
}
}
void insert()
{
struct node*ptr;
int item;
ptr=(struct node*)malloc(sizeof(struct node));
if(ptr==NULL)
{
printf("\nOVERFLOW\n");
return;
}
else
{
printf("\n enter a value?\n");
scanf("%d",&item);
ptr->data=item;
if(front==NULL)
{
front=ptr;
rear=ptr;
front->next=NULL;
rear->next=NULL;
}
else
{
rear->next=ptr;
rear=ptr;
rear->next=NULL;
}
}
}
void delete()
{
struct node*ptr;
if(front==NULL)
{
printf("\nUNDERFLOW");
return;
}
else
{
ptr=front;
front=front->next;
free(ptr);
}
}
void display()
{
struct node*ptr;
ptr=front;
if(front==NULL)
{
printf("\nEMPTY QUEUE\n");
}
else
{
printf("\n printing values...\n");
while(ptr!=NULL)
{
printf("%d ",ptr->data);
ptr=ptr->next;
}
}
}

OUTPUT

EXPERIMENT 8
AIM: Implementation of Queue.

Software used: VS code

CODE:

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node*head;
void beginsert();
void lastinsert();
void randominsert();
void begin_delete();
void last_delete();
void random_delete();
void display();

void main()
{
int choice=0;
while(choice!=9)
{
printf("\n\n *****Main menu*****\n");
printf("\n choose one of the option\n");
printf("\n-------------\n");
printf("\n1.Insert in beginning\n2.insert at last\n3.Insert in the
middle\n4.delete in the beginning\n5.delete in the last\n6.delete in
the middle\n7.display the list\n");
printf("\n Enter the choice \n");
scanf("\n %d",&choice);

switch(choice)
{
case 1:
beginsert();
break;
case 2:
lastinsert();
break;
case 3:
randominsert();
break;
case 4:
begin_delete();
break;
case 5:
last_delete();
break;
case 6:
random_delete();
break;
case 7:
display();
break;
case 8:
exit(0);
break;
default:
printf("\n Enter any valid choice\n");
}
}
}
void beginsert()
{
struct node*ptr;
int item;
ptr=(struct node*)malloc(sizeof(struct node*));
if(ptr==NULL)
{
printf("OVERFLOW");
}
else
{
printf("\n Enter the value");
scanf("%d",&item);
ptr->data=item;
ptr->next=head;
head=ptr;
printf("\n Node inserted\n");
}
}
void lastinsert()
{
struct node*ptr,*temp;
int item;
ptr=(struct node*)malloc(sizeof(struct node));
if(ptr==NULL)
{
printf("\nOVERFLOW\n");
}
else
{
printf("\n Enter the value?\n");
scanf("%d",&item);
ptr->data=item;
if(head==NULL)
{
ptr->next=NULL;
head=ptr;
printf("\n Node inserted");
}
else
{
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=ptr;
ptr->next=NULL;
printf("\n Node inserted");
}
}
}
void randominsert()
{
int i,loc,item;
struct node*ptr,*temp;
ptr=(struct node*)malloc(sizeof(struct node));
if(ptr==NULL)
{
printf("\n OVERFLOW");
}
else
{
printf("\n Enter the value\n");
scanf("%d",&item);
ptr->data=item;
printf("\n Enter the location of the element to be inserted\n");
scanf("\n %d",&loc);
temp=head;
for(i=0;i<loc;i++)
{
temp=temp->next;
if(temp==NULL)
{
printf("\n can't insert");
return;
}
}
ptr->next=temp->next;
temp->next=ptr;
printf("\n Node inserted");
}
}
void display()
{
struct node*ptr;
ptr=head;
if(ptr==NULL)
{
printf("\nNothing to print");
}
else
{
printf("\n Printing values. . . . .\n");
while(ptr!=NULL)
{
printf("%d ",ptr->data);
ptr=ptr->next;
}
}
}
void begin_delete()
{
struct node*ptr;
if(head==NULL)
{
printf("\n list is empty\n");
}
else
{
ptr=head;
head=ptr->next;
free(ptr);
printf("\n Node deleted from the beginning ..\n");
}
}
void last_delete()
{
struct node*ptr,*ptr1;
if(head==NULL)
{
printf("\n the list is empty\n");
}
else if(head->next==NULL)
{
head=NULL;
free(head);
printf("\nOnly node of the list deleted ..\n");
}
else
{
ptr=head;
while(ptr->next!=NULL)
{
ptr1=ptr;
ptr=ptr->next;
}
ptr1->next=NULL;
free(ptr);
printf("\n Deleted node from the last..\n");
}
}
void random_delete()
{
struct node*ptr,*ptr1;
int loc,i;
printf("\n Enter the location of the node after which you want to
delete\n");
scanf("%d",&loc);
ptr=head;
for(i=0;i<loc;i++)
{
ptr1=ptr;
ptr=ptr->next;
if(ptr==NULL)
{
printf("\n can't delete");
return;
}
}
ptr1->next=ptr->next;
free(ptr);
printf("\n deleted nodes%d",loc+1);
}

OUTPUT
EXPERIMENT 9
AIM: Implementation of Queue as a linked list.

Software used: VS code.

CODE:

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node*head;
void beginsert();
void lastinsert();
void randominsert();
void begin_delete();
void last_delete();
void random_delete();
void display();

void main()
{
int choice=0;
while(choice!=9)
{
printf("\n\n *****Main menu*****\n");
printf("\n choose one of the option\n");
printf("\n-------------\n");
printf("\n1.Insert in beginning\n2.insert at last\n3.Insert in the
middle\n4.delete in the beginning\n5.delete in the last\n6.delete in
the middle\n7.display the list\n");
printf("\n Enter the choice \n");
scanf("\n %d",&choice);

switch(choice)
{
case 1:
beginsert();
break;
case 2:
lastinsert();
break;
case 3:
randominsert();
break;
case 4:
begin_delete();
break;
case 5:
last_delete();
break;
case 6:
random_delete();
break;
case 7:
display();
break;
case 8:
exit(0);
break;
default:
printf("\n Enter any valid choice\n");
}
}
}
void beginsert()
{
struct node*ptr;
int item;
ptr=(struct node*)malloc(sizeof(struct node*));
if(ptr==NULL)
{
printf("OVERFLOW");
}
else
{
printf("\n Enter the value");
scanf("%d",&item);
ptr->data=item;
ptr->next=head;
head=ptr;
printf("\n Node inserted\n");
}
}
void lastinsert()
{
struct node*ptr,*temp;
int item;
ptr=(struct node*)malloc(sizeof(struct node));
if(ptr==NULL)
{
printf("\nOVERFLOW\n");
}
else
{
printf("\n Enter the value?\n");
scanf("%d",&item);
ptr->data=item;
if(head==NULL)
{
ptr->next=NULL;
head=ptr;
printf("\n Node inserted");
}
else
{
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=ptr;
ptr->next=NULL;
printf("\n Node inserted");
}
}
}
void randominsert()
{
int i,loc,item;
struct node*ptr,*temp;
ptr=(struct node*)malloc(sizeof(struct node));
if(ptr==NULL)
{
printf("\n OVERFLOW");
}
else
{
printf("\n Enter the value\n");
scanf("%d",&item);
ptr->data=item;
printf("\n Enter the location of the element to be inserted\n");
scanf("\n %d",&loc);
temp=head;
for(i=0;i<loc;i++)
{
temp=temp->next;
if(temp==NULL)
{
printf("\n can't insert");
return;
}
}
ptr->next=temp->next;
temp->next=ptr;
printf("\n Node inserted");
}
}
void display()
{
struct node*ptr;
ptr=head;
if(ptr==NULL)
{
printf("\nNothing to print");
}
else
{
printf("\n Printing values. . . . .\n");
while(ptr!=NULL)
{
printf("%d ",ptr->data);
ptr=ptr->next;
}
}
}
void begin_delete()
{
struct node*ptr;
if(head==NULL)
{
printf("\n list is empty\n");
}
else
{
ptr=head;
head=ptr->next;
free(ptr);
printf("\n Node deleted from the beginning ..\n");
}
}
void last_delete()
{
struct node*ptr,*ptr1;
if(head==NULL)
{
printf("\n the list is empty\n");
}
else if(head->next==NULL)
{
head=NULL;
free(head);
printf("\nOnly node of the list deleted ..\n");
}
else
{
ptr=head;
while(ptr->next!=NULL)
{
ptr1=ptr;
ptr=ptr->next;
}
ptr1->next=NULL;
free(ptr);
printf("\n Deleted node from the last..\n");
}
}
void random_delete()
{
struct node*ptr,*ptr1;
int loc,i;
printf("\n Enter the location of the node after which you want to
delete\n");
scanf("%d",&loc);
ptr=head;
for(i=0;i<loc;i++)
{
ptr1=ptr;
ptr=ptr->next;
if(ptr==NULL)
{
printf("\n can't delete");
return;
}
}
ptr1->next=ptr->next;
free(ptr);
printf("\n deleted nodes%d",loc+1);
}

OUTPUT

EXPERIMENT 10
AIM: Implementation of circular Queue with stack.

Software used: VS code.

CODE:
#include<stdio.h>
#include<stdlib.h>
# define maxsize 5
void insert();
void delete();
void display();
int front=-1,rear=-1;
int queue[maxsize];
void main()
{
int choice;
while(choice!=4)
{
printf("\n********Main menu********");
printf("\n =====================\n");
printf("\n1.insert an element\n2.delete an element\n3.display
list\n.4 exit");
printf("\n enter your choice?");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\n Enter valid choice\n");
}
}
}
void insert()
{
int item;
printf("\n enter the elements\n");
scanf("\n %d",&item);
if(rear==maxsize-1)
{
printf("\n OVERFLOW\n");
return;
}
if(front==-1&&rear==-1)
{
front=0;
rear=0;
}
else
{
rear=rear+1;
}
queue[rear]=item;
printf("\n Value inserted\n");
}
void delete()
{
int item;
if(front==-1||front>rear)
{
printf("\nUNDERFLOEW\n");
return;
}
else
{
item=queue[front];
if(front==rear)
{
front=-1;
rear=-1;
}
else
{
front=front+1;
}
printf("\n value deleted\n");
}
}
void display()
{
int i;
if(rear==-1)
{
printf("\n empty queue\n");
}
else
{
printf("\n printing values...\n");
for(i=front;i<=rear;i++)
{
printf("%d ",queue[i]);
}
}
}

OUTPUT
EXPERIMENT 11
AIM: Binary search tree insertion deletion and
traversal.

Software used: VS code.

CODE:

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

struct node{
int key;
struct node *left, *right;
};
struct node *newNode(int item)
{
struct node*temp=(struct node*)malloc(sizeof(struct node));
temp->key=item;
temp->left=temp->right=NULL;
return temp;
}
void inorder(struct node*root)
{
if(root!=NULL)
{
inorder(root->left);

printf("%d ",root->key);

inorder(root->right);
}
}
struct node *find_Minimum(struct node *node)
{
struct node *current=node;
while(current&&current->left!=NULL)
{
current=current->left;
}
return current;
}

struct node *insert(struct node *node,int key)


{
if(node==NULL)
{
return newNode(key);
}
if(key<node->key)
{
node->left=insert(node->left,key);
}
else
{
node->right=insert(node->right,key);
}
return node;
}
struct node *deleteNode(struct node*root,int key)
{
if(root==NULL)
{
return root;
}
if(key<root->key)
{
root->left=deleteNode(root->left,key);
}
else if(key>root->key)
{
root->right=deleteNode(root->right,key);
}
else
{
if(root->left==NULL)
{
struct node *temp=root->right;
free(root);
return temp;
}
else if(root->right==NULL)
{
struct node *temp=root->left;
free(root);
return temp;
}
else
{
struct node *temp=find_Minimum(root->right);
root->key=temp->key;
root->right=deleteNode(root->right,temp->key);
}
}
return root;
}
int main()
{
struct node*root=NULL;
root=insert(root,8);
root=insert(root,3);
root=insert(root,1);
root=insert(root,6);
root=insert(root,7);
root=insert(root,10);
root=insert(root,14);
root=insert(root,4);

inorder(root);

printf("\n after deleting 8\n");


root=deleteNode(root,8);

inorder(root);

return 0;

}
OUTPUT

EXPERIMENT 12
AIM: Implementation of heap sort.

Software used: VS code.

CODE:

#include<stdio.h>
void heapify(int a[],int n,int i)
{
int largest=i;
int left=2*i+1;
int right=2*i+2;
if(left<n&&a[left]>a[largest])
{
largest=left;
}
if(right<n&&a[right]>a[largest])
{
largest=right;
}
if(largest!=i)
{
int temp=a[i];
a[i]=a[largest];
a[largest]=temp;
heapify(a,n,largest);
}
}
void heapsort(int a[],int n)
{
for(int i=n/2-1;i>=0;i--)
{
heapify(a,n,i);
}
for(int i=n-1;i>=0;i--)
{
int temp=a[0];
a[0]=a[i];
a[i]=temp;

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

int main()
{
int a[]={48,10,23,43.8,12,36};
int n=sizeof(a)/sizeof(a[0]);
printarr(a,n);
heapsort(a,n);
printf("\n after sorting elements: ");
printarr(a,n);
return 0;
}

OUTPUT

EXPERIMENT 13
AIM: Implementation of insertion sort.

Software used: VS code.

CODE:

#include<stdio.h>
#include<conio.h>

void main()
{
int s,i,j,t,lst[50];
printf("Enter the size of the list: \n");
scanf("%d",&s);

printf("Enter the integer values: ",s);


for(i=0;i<s;i++)
{
scanf("%d",&lst[i]);
}
for(i=1;i<s;i++)
{
t=lst[i];
j=i-1;
while((t<lst[j]) && (j>=0))
{
lst[j+1] = lst[j];
j=j-1;
}
}
lst[j+1]=t;
printf("List after sorting is :");
for(i=0;i<s;i++)
{
printf("%d ",lst[i]);
}
getch();
}

OUTPUT

EXPERIMENT 14
AIM: Implementation of quick sort.

Software used: VS code.

CODE:
#include<stdio.h>
void swap(int *a,int *b)
{
int t=*a;
*a=*b;
*b=t;
}
int partition(int array[],int low ,int high)
{
int pivot = array[high];
int i=(low-1);
for(int j=low;j<high;j++)
{
if(array[j]<=pivot)
{
i++;
swap(&array[i],&array[j]);
}
}
swap(&array[i+1],&array[high]);
return (i+1);
}
void quicksort(int array[],int low,int high)
{
if(low<high)
{

int pi=partition(array,low,high);
quicksort(array,low,pi-1);
quicksort(array,pi+1,high);

}
}
void printarray(int array[],int size)
{
for(int i=0;i<size;++i)
{
printf("%d ",array[i]);
}
printf("\n");
}
int main()
{
int data[]={8,7,2,1,0,9,6};
int n=sizeof(data)/sizeof(data[0]);
printf("Unsorted array ");
printarray(data,n);

quicksort(data,0,n-1);

printf("Sorted array in ascending order : \n");


printarray(data,n);
return 0;

OUTPUT
EXPERIMENT 15
AIM: Implementation of Selection sort.

Software used: VS code.

CODE:

#include<stdio.h>
void swap(int *xp,int *yp)
{
int temp=*xp;
*xp=*yp;
*yp=temp;
}
void selectionsort(int arr[],int n)
{
int i,j,min_id;
for(i=0;i<n-1;i++)
{
min_id=i;
for(j=i+1;j<n;j++)
{
if(arr[j]<arr[min_id])
{
min_id=j;
}
}
swap(&arr[min_id],&arr[i]);
}
}
void printarray(int arr[],int size)
{
int i;
for(i=0;i<size;i++)
{
printf("%d ",arr[i]);
}
printf("\n");
}
int main()
{
int arr[]={64,25,12,22,11};
int n=sizeof(arr)/sizeof(arr[0]);
selectionsort(arr,n);
printarray(arr,n);
return 0;
}

OUTPUT
EXPERIMENT 16
AIM: Implementation of Bubble sort.

Software used: VS code.

CODE:

#include <stdio.h>

void bubbleSort(int array[], int size) {

for (int step = 0; step < size - 1; ++step) {

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

if (array[i] > array[i + 1]) {


int temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
}
}
}
}

void printArray(int array[], int size) {


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

int main() {
int data[] = {-2, 45, 0, 11, -9};

int size = sizeof(data) / sizeof(data[0]);

bubbleSort(data, size);

printf("Sorted Array in Ascending Order:\n");


printArray(data, size);
}

OUTPUT
EXPERIMENT 17
AIM: Implementation of Merge sort.

Software used: VS code.

CODE:

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

void merge(int arr[], int l, int m, int r)


{
int i,j,k;
int n1=m-l+1;
int n2=r-m;
int L[n1], R[n2];

for(i=0;i<n1;i++)
{
L[i]=arr[l+i];
}
for(j=0;j<n2;j++)
{
R[j]=arr[m+1+j];
}

i=0;
j=0;
k=1;
while(i<n1 && j<n2)
{
if(L[i]<=R[j])
{
arr[k]=L[i];
i++;
}
else
{
arr[k]=R[j];
j++;
}
k++;
}
while(i<n1)
{
arr[k]=L[i];
i++;
k++;
}
while(j<n2)
{
arr[k]=R[j];
j++;
k++;
}
}
void mergesort(int arr[],int l, int r)
{
if(l<r)
{
int m=l+(r-l)/2;
mergesort(arr,l,m);
mergesort(arr,m+1,r);
merge(arr,l,m,r);
}
}
void printarrAY(int A[],int size)
{
int i;
for(i=0;i<size;i++)
{
printf("%d ", A[i]);
}
printf("\n");
}
int main()
{
int arr[]={12,11,13,5,6,7};
int arr_size=sizeof(arr)/sizeof(arr[0]);
printf("Given array is \n ");
printarrAY(arr,arr_size);
mergesort(arr,0,arr_size-1);
printf("\n Sorted array is \n");
printarrAY(arr,arr_size);
return 0;
}

OUTPUT

EXPERIMENT 18
AIM: Implemenation of Radix/Bucket sort.

Software used: VS code.

CODE:

#include<stdio.h>
int largest(int a[])
{
int larger=a[0],i;
for(i=1;i<10;i++)
{
if(a[i]>larger)
{
larger=a[i];
}
}
return larger;
}
void radixsort(int a[], int n)
{
int bucket[10][10],bucket_count[10];
int i,j,k,r,NOP=0, divisor=1,lar,pass;
lar=largest(a);
while(lar>0)
{
NOP++;
lar/=10;
}
for(pass=0;pass<NOP;pass++)
{
for(i=0;i<10;i++)
{
bucket_count[i]=0;
}
for(i=0;i<n;i++)
{
r=(a[i]/divisor)%10;
bucket[r][bucket_count[r]]=a[i];
bucket_count[r]+=1;
}
i=0;
for(k=0;k<10;k++)
{
for(j=0;j<bucket_count[k];j++)
{
a[i]=bucket[k][j];
i++;
}
}
divisor*=10;
printf("After pass %d : ",pass+1);
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
printf("\n");
}
}
int main()
{
int i,n,a[10];
printf("enter the no of items to be sorted : ");
scanf("%d",&n);
printf("Enter the items : ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
radixsort(a,n);
printf("Sorted items : ");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
printf("\n");
return 0;
}

OUTPUT

You might also like