Practical File OF Data Structure and Algorithms
Practical File OF Data Structure and Algorithms
Practical File OF Data Structure and Algorithms
OF
Data Structure and
Algorithms
Submitted in partial fulfillment of the requirement for the
Degree of Bachelor of Technology (B. tech)
In
5
Write a program to Implement Bubble sort
6
Write a program to Implement Quick sort.
9
Write a program to Implement Queue and its
operations.
Write a program to Implement Circular Queue and
10 its operations.
Program: 1
Source Code:
#include <stdio.h>
int main()
{
int a [100], s, i, n;
Output:
Program: 2
Source Code:
#include <stdio.h>
int main()
{
int i, low, high, mid, n, key,
array[100];
printf("Enter number of elements\n");
scanf("%d",&n); printf("Enter %d
integers\n", n);
for(i = 0; i < n; i++)
scanf("%d",&array[i]);
printf("Enter value to find\n");
scanf("%d", &key);
low = 0;
high = n - 1;
mid = (low+high)/2;
while (low <= high)
{ if(array[mid] < key) low =
mid + 1; else if (array[mid]
== key)
{
printf("%d found at location %d", key, mid+1);
break;
} else high = mid - 1;
mid = (low + high)/2;
} if(low >
high)
printf("Not found! %d isn't present in the list\n", key); return
0;}
Output:
Program: 3
Aim: Write a program to Implement Insertion Sort.
Source Code:
#include <stdio.h>
int main(void)
{
int n, i, j, temp;
int arr[64];
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
for (i = 1; i < n; i++)
{ j
= i;
while (j > 0 && arr[j - 1] > arr[j])
{
temp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = temp;
j--;
}
}
printf("Sorted list in ascending order:\n");
for (i = 0; i < n; i++)
{
printf("%d\n", arr[i]);
}
return 0;
}
Output:
Program: 4
Source Code:
#include <stdio.h> int
main () {
int arr[10]={6,12,0,18,11,99,55,45,34,2};
int n=10; int i, j, position, swap; for (i =
0; i < (n - 1); i++) { position = i; for (j
= i + 1; j < n; j++) { if (arr[position] >
arr[j])
position = j;
}
if (position != i) {
swap = arr[i]; arr[i] =
arr[position];
arr[position] = swap;
}
}
for (i = 0; i < n; i++)
printf("%d\t", arr[i]);
return 0;
}
Output:
Program: 5
Source Code:
#include <stdio.h>
int main(){
scanf("%d", &num);
scanf("%d", &arr[x]);
arr[y + 1] = temp;
return 0;
Output:
Program: 6
Source Code:
#include<stdio.h>
if(first<last){
pivot=first;
i=first;
j=last;
while(i<j){
while(number[i]<=number[pivot]&&i<last)
i++;
while(number[j]>number[pivot])
j--;
if(i<j){
temp=number[i];
number[i]=number[j];
number[j]=temp;
temp=number[pivot];
number[pivot]=number[j];
number[j]=temp;
quicksort(number,first,j-1);
quicksort(number,j+1,last);
}
}
int main(){
scanf("%d",&count);
for(i=0;i<count;i++) scanf("%d",&number[i]);
quicksort(number,0,count-1);
for(i=0;i<count;i++)
printf(" %d",number[i]);
return 0;
Output:
Program: 7
Source Code:
#include <stdio.h> #define
max 10
int a[11] = { 10, 14, 19, 26, 27, 31, 33, 35, 42, 44, 0 }; int
b[10];
for(l1 = low, l2 = mid + 1, i = low; l1 <= mid && l2 <= high; i++) {
if(a[l1] <= a[l2]) b[i] = a[l1++]; else
b[i] = a[l2++];
}
sort(0, max);
Output:
Program: 8
Source Code:
#include<stdio.h>
#include<stdlib.h>
#define Size 4
int main()
{
int choice;
while(1)
{
printf("\nOperations performed by Stack");
printf("\n1.Push the element\n2.Pop the
element\n3.Show\n4.End");
printf("\n\nEnter the choice:");
scanf("%d",&choice);
switch(choice)
{
case 1: Push();
break;
case 2: Pop();
break;
case 3: show();
break;
case 4: exit(0);
void Push()
{
int x;
if(Top==Size-1)
{
printf("\nOverflow!!");
}
else
{
printf("\nEnter element to be inserted to the
stack:"); scanf("%d",&x); Top=Top+1;
inp_array[Top]=x;
}
}
void Pop()
{
if(Top==-1)
{
printf("\nUnderflow!!");
}
else
{
printf("\nPopped element: %d",inp_array[Top]);
Top=Top-1;
}
}
void show()
{
if(Top==-1)
{
printf("\nUnderflow!!");
}
else
{
printf("\nElements present in the stack: \n");
for(int i=Top;i>=0;--i)
printf("%d\n",inp_array[i]);
}
}
Output:
Program: 9
Source Code:
#include <stdio.h>
#include<stdlib.h> #define MAX 50 void
insert(); void delete(); void display(); int
queue_array[MAX]; int rear = - 1; int front
= - 1; int main() { int choice; while (1) {
printf("1.Insert element to queue n");
printf("2.Delete element from queue n");
printf("3.Display all elements of queue n");
printf("4.Quit n"); printf("Enter your
choice : "); scanf("%d", &choice);
switch(choice)
{ case
1:
insert
();
break;
case
2:
delete
();
break;
case
3:
displa
y();
break;
case
4:
exit(1
);
defaul
t:
printf
("Wron
g
choice
n");
}
} } void insert() { int
item; if(rear == MAX - 1)
printf("Queue Overflow n");
else { if(front== - 1)
front = 0;
printf("Inset the element in queue : ");
scanf("%d", &item); rear = rear + 1;
queue_array[rear] = item;
} } void
delete() {
if(front == - 1 || front > rear)
{
printf("Queue Underflow n");
return; }
else {
printf("Element deleted from queue is : %dn",
queue_array[front]); front = front + 1;
} }
void display()
{ int i; if(front == - 1)
printf("Queue is empty n");
else { printf("Queue is : n");
for(i = front; i <= rear; i++)
printf("%d ", queue_array[i]);
printf("n");
}
}
Output:
Program: 10
Source Code:
#include<stdio.h> #
define MAX 5 int
cqueue_arr[MAX]; int
front = -1; int rear = -
1;
void insert(int item)
{
if((front == 0 && rear == MAX-1) || (front == rear+1))
{
printf("Queue Overflow \n");
return;
}
if(front == -1)
{
front = 0;
rear = 0;
}
else
{
if(rear == MAX-1)
rear = 0; else
rear = rear+1;
}
cqueue_arr[rear] = item ;
}
void deletion()
{
if(front == -1)
{
printf("Queue Underflow\n");
return ;
}
printf("Element deleted from queue is : %d\n",cqueue_arr[front]);
if(front == rear)
{
front = -1;
rear=-1;
}
else
{
if(front == MAX-1)
front = 0; else
front = front+1;
}
}
void display()
{
int front_pos = front,rear_pos = rear;
if(front == -1)
{
printf("Queue is empty\n");
return;
}
printf("Queue elements :\n"); if(
front_pos <= rear_pos )
while(front_pos <= rear_pos)
{
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
}
else
{
while(front_pos <= MAX-1)
{
printf("%d ",cqueue_arr[front_pos]); front_pos++;
}
front_pos = 0;
while(front_pos <= rear_pos)
{
printf("%d ",cqueue_arr[front_pos]); front_pos++;
}
}
printf("\n");
}
int main()
{
int choice,item; do
{
printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Display\n");
printf("4.Quit\n"); printf("Enter
your choice : \n");
scanf("%d",&choice);
switch(choice)
{
case 1 : printf("Input the element for insertion in
queue : \n"); scanf("%d", &item); insert(item);
break; case 2 : deletion();
break; case 3: display();
break; case 4: break;
default: printf("Wrong
choice\n");
}
} while (choice! = 4);
return 0;
}
Output:
Program: 11
Source Code:
#include <stdlib.h>
#include <stdio.h>
struct node
{ int
info;
struct node *next;
};
struct node *start = NULL; int
main()
{ int
choice;
printf("\n MENU \n");
printf("\n 1.Insert at the end \n"); printf("\n
2.Delete from the end \n"); printf("\n
3.Display \n"); printf("\n 4.Exit \n");
while (1)
{
printf("\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 3:
display();
break; case 1:
insert_end();
break; case 2:
delete_end();
break; case 4:
exit(0);
break; default:
printf("\n Wrong Choice \n");
break;
}
}
return 0;
}
void insert_end()
{
struct node *temp, *ptr; temp = (struct node
*)malloc(sizeof(struct node)); printf("\nEnter the
data value for the node:\t"); scanf("%d", &temp-
>info); temp->next = NULL;
if (start == NULL)
{
start = temp;
}
else
{ ptr =
start;
while (ptr->next != NULL)
{
ptr = ptr->next;
}
ptr->next = temp;
}
}
void delete_end()
{
struct node *temp, *ptr;
if (start == NULL)
{
printf("\nList is Empty:");
exit(0);
}
else if (start->next == NULL)
{ ptr =
start; start =
NULL;
printf("\nThe deleted element is:%dt", ptr->info);
free(ptr);
}
else
{ ptr =
start;
while (ptr->next != NULL)
{
temp = ptr;
ptr = ptr->next;
}
temp->next = NULL;
printf("\nThe deleted element is:%d\t", ptr->info); free(ptr);
}
}
void display()
{
struct node *ptr;
if (start == NULL)
{
printf("\nList is empty\n");
return;
}
else
{ ptr =
start;
printf("\nThe List elements are:\t");
printf("[START] --> ");
while (ptr != NULL)
{
printf("%d --> ", ptr->info);
ptr = ptr->next;
}
printf("[END]");
}
}
Output: