0% found this document useful (0 votes)
3 views

Datastructure Programs

MSU c++ dsa programs

Uploaded by

rbsiva421688
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Datastructure Programs

MSU c++ dsa programs

Uploaded by

rbsiva421688
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 66

DEPARTMENT OF COMPUTER SCIENCE

2024–2025

NAME :

REG.NO. :

CLASS : II BCA-A

SUBJECT : DATA STRUCTURES AND


ALGORITHMS USING C++ LAB

SUBJECT CODE : EMCAP3


DEPARTMENT OF COMPUTER SCIENCE

Reg.No: Class:II BCA-A

Certified bonafide record of work done by in

the DATA STRUCTURES AND ALGORITHMS USING C++LAB, KAMARAJ COLLEGE,

THOOTHUKUDI, during the year 2023–2026.

STAFF INCHARGE HEAD OF THE DEPARTMENT

Submitted for the practical examination held on

at KAMARAJ COLLEGE, THOOTHUKUDI.

EXAMINERS

I.

II.
INDEX

S.No Date Title of the program Page No. Signature

1. List ADT

2.a. Stack ADT

2.b. Queue ADT

3. Infix to Postfix

4. Priority queue ADT

5.a. Inserting into BST

5.b. Deleting into BST

5.c. Searching in BST

6.a. Insertion into AVL Tree

6.b. Deletion into AVL Tree

7 BFS and DFS

8.a. Linear Search

8.b. Binary Search

9.a. Bubble Sort

9.b. Selection Sort

9.c. Insertion Sort

9.d. Radix Sort


1)Program to implement the list ADT using arrays and linked list

#include<iostream.h>
#include<conio.h>
struct Node

{
int data;
Node*next;
};

struct Node*newNode(int data)


{
Node*node=new Node;
node->data=data;

node->next=NULL;
return node;
}
void insertNewNode(Node**root,int data)
{

Node*node=newNode(data);
Node*ptr;
if(*root==NULL)
{

*root=node;
}
else
{

ptr= *root;
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
ptr->next=node;
}

}
void printLinkedList(Node * root)
{
while(root!=NULL)

{
cout<<root->data<<"->";
root=root->next;
}

cout<<"NULL"<<endl;
}
Node*createLinkedList(int arr[], int n)
{
Node*root=NULL;

for(int i=0;i<n;i++)
{
insertNewNode(&root, arr[i]);
}

return root;
}
void main()
{

clrscr();
int arr[]={1,2,3,4,5}, n=5;
Node*root=createLinkedList(arr,n);
printLinkedList(root);

getch();
}

OUTPUT:

1->2->3->4->5->NULL
2)a)Program to implement stack ADT using singly linked list
#include <iostream.h>
#include <conio.h>
struct Node {
int data;

Node* next;
};
class Stack {
private:

Node* top;

public:
Stack() {

top = 0;
}
void push(int value) {
Node* newNode = new Node;
newNode->data = value;

newNode->next = top;
top = newNode;
cout << value <<" pushed to stack\n";
}

int pop() {
if (isEmpty()) {
cout <<"Stack is empty\n";

return -1; // Return -1 to indicate stack is empty


}
Node* temp = top;
int poppedValue = top->data;
top = top->next;
delete temp;

cout << poppedValue <<" popped from stack\n";


return poppedValue;
}
int isEmpty() {

return top == 0;
}
void display() {
if (isEmpty()) {

cout <<"Stack is empty\n";


return;
}
Node* temp = top;
cout <<"Stack elements: ";

while (temp != 0) {
cout << temp->data <<" ";
temp = temp->next;
}

cout << endl;


}
};
void main() {

Stack stack;
int choice, value;
clrscr();
while (1) {

cout <<"\n1. Push\n2. Pop\n3. Display\n4. Exit\n";


cout <<"Enter your choice: ";
cin >> choice;
switch (choice) {

case 1:
cout <<"Enter value to push: ";
cin >> value;
stack.push(value);

break;
case 2:
stack.pop();
break;

case 3:
stack.display();
break;
case 4:
cout <<"Exiting...\n";

return;
default:
cout <<"Invalid choice. Please try again.\n";
}

}
}

OUTPUT:
1.Push
2.Pop
3.Display

4.Exit
Enter your choice:1
Enter value to push:0
0 pushed to stack

1.Push
2.Pop
3.Display

4.Exit
Enter your choice:1
Enter value to push:1
1 pushed to stack

1.Push
2.Pop
3.Display
4.Exit

Enter your choice:1


Enter value to push:2
2 pushed to stack

1.Push
2.Pop
3.Display
4.Exit

Enter your choice:2


2 popped from stack

1.Push

2.Pop
3.Display
4.Exit
Enter your choice:3

Stack elements: 1 0

1.Push
2.Pop

3.Display
4.Exit
Enter your choice:4
2)b)Program to implement queue ADT using singly linked list
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
struct Node {

int data;
Node* next;
};
class Queue {

private:
Node* front;
Node* rear;
public:

Queue() {
front = rear = NULL;
}
void enqueue(int value) {
Node* newNode = new Node();

newNode->data = value;
newNode->next = NULL;
if (rear == NULL) {
front = rear = newNode;

return;
}
rear->next = newNode;
rear = newNode;

}
int dequeue() {
if (front == NULL) {
cout <<"Queue is empty!"<< endl;
return -1;
}

int value = front->data;


Node* temp = front;
front = front->next;
if (front == NULL) {

rear = NULL;
}
delete temp;
return value;

}
void display() {
if (front == NULL) {
cout <<"Queue is empty!"<< endl;
return;

}
Node* temp = front;
cout <<"Queue elements: ";
while (temp != NULL) {

cout << temp->data <<"";


temp = temp->next;
}
cout << endl;

}
};

void main() {

clrscr();
Queue q;
int choice, value;
do {

cout <<"\nQueue Operations:\n";


cout <<"1. Enqueue\n";
cout <<"2. Dequeue\n";
cout <<"3. Display\n";

cout <<"4. Exit\n";


cout <<"Enter your choice: ";
cin >> choice;
switch (choice) {

case 1:
cout <<"Enter value to enqueue: ";
cin >> value;
q.enqueue(value);
break;

case 2:
value = q.dequeue();
if (value != -1) {
cout <<"Dequeued: "<< value << endl;

}
break;
case 3:
q.display();

break;
case 4:
cout <<"Exiting program."<< endl;
break;

default:
cout <<"Invalid choice! Please try again."<< endl;
}
} while (choice != 4);

getch();
}

OUTPUT:
Queue Operations:
1. Enqueue
2. Dequeue

3. Display
4. Exit
Enter your choice: 1
Enter the value to enqueue: 1

Queue Operations:
1. Enqueue
2. Dequeue
3. Display

4. Exit
Enter your choice: 1
Enter the value to enqueue: 2

Queue Operations:
1. Enqueue
2. Dequeue
3. Display

4. Exit
Enter your choice: 1
Enter the value to enqueue: 3

Queue Operations:
1. Enqueue
2. Dequeue
3. Display

4. Exit
Enter your choice: 2
Dequeued: 1

Queue Operations:
1. Enqueue
2. Dequeue
3. Display
4. Exit

Enter your choice: 3


Queue elements: 2 3

Queue Operations:

1. Enqueue
2. Dequeue
3. Display
4. Exit

Enter your choice: 4


Exiting program.
3)Program to evaluate postfix expression

#include <iostream.h>

#include <conio.h>

#include <ctype.h>

#include <string.h>

#define MAX 50

int precedence(char op)

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

return 1;

if (op == '*' || op == '/')

return 2;

return 0;

void infixToPostfix(const char infix[], char postfix[])

char stack[MAX];

int top = -1;

int j = 0;

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

char ch = infix[i];

if (isdigit(ch))

postfix[j++] = ch;

}
else if (ch == '(')

stack[++top] = ch;

else if (ch == ')')

while (top != -1 && stack[top] != '(')

postfix[j++] = stack[top--];

top--;

else

while (top != -1 && precedence(stack[top]) >= precedence(ch))

postfix[j++] = stack[top--];

stack[++top] = ch;

while (top != -1)

postfix[j++] = stack[top--];

postfix[j] = '\0';
}

int evaluatePostfix(const char postfix[])

int stack[MAX];

int top = -1;

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

char ch = postfix[i];

if (isdigit(ch))

stack[++top] = ch - '0';

else

if (top < 1)

cout <<"Error: Insufficient operands for operation"<< endl;

return -1;

int operand2 = stack[top--];

int operand1 = stack[top--];

switch (ch) {

case '+':

stack[++top] = operand1 + operand2;

break;

case '-':
stack[++top] = operand1 - operand2;

break;

case '*':

stack[++top] = operand1 * operand2;

break;

case '/':

if (operand2 == 0)

cout <<"Error: Division by zero"<< endl;

return -1;

stack[++top] = operand1 / operand2;

break;

default:

cout <<"Error: Invalid operator"<< endl;

return -1;

if (top != 0)

cout <<"Error: Invalid postfix expression"<< endl;

return -1;

return stack[top];

}
int main()

char infix[50], postfix[50];

clrscr();

cout <<"Enter infix expression: ";

cin.getline(infix, 50);

infixToPostfix(infix, postfix);

cout <<"Postfix expression: "<< postfix << endl;

int result = evaluatePostfix(postfix);

if (result != -1) {

cout <<"Evaluation result: "<< result << endl;

getch();

return 0;

OUTPUT:

Enter infix expression: 2+2+(3+3)-(4*3)/9

Postfix expression: 22+33++43*9/-

Evaluation result: 9
4)Program to implement priority queue ADT
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>

#include<stdlib.h>
struct node
{
int priority;

int info;
struct node*link;
};
class Priority_Queue

{
private:
node*front;
public:
Priority_Queue()

{
front=NULL;
}
void insert(int item, int priority)

{
node*tmp, *q;
tmp=new node;
tmp->info=item;

tmp->priority=priority;
if(front==NULL||priority<front->priority)
{
tmp->link=front;
front=tmp;
}

else
{
q=front;
while(q->link!=NULL && q->link->priority<=priority)

q=q->link;
tmp->link=q->link;
q->link=tmp;
}

}
void del()
{
node*tmp;
if(front==NULL)

cout<<"Queue Underflow\n";
else
{
tmp=front;

cout<<"Deleted item is:"<<tmp->info<<endl;


front=front->link;
free(tmp);
}

}
void display()
{
node*ptr;

ptr=front;
if(front==NULL)
cout<<"Queue is empty\n";
else

{
cout<<"Queue is:\n";
cout<<"Priority Item\n";
while(ptr!=NULL)

{
cout<<ptr->priority<<""<<ptr->info<<endl;
ptr=ptr->link;
}

}
}
};
void main()
{

clrscr();
int choice, item, priority;
Priority_Queue pq;
do

{
cout<<"1.Insert\n";
cout<<"2.Delete\n";
cout<<"3.Display\n";

cout<<"4.Quit\n";
cout<<"Enter your choice:";
cin>>choice;
switch(choice)

{
case 1:
cout<<"Input the item value to be added in the queue:";
cin>>item;

cout<<"Enter its priority:";


cin>>priority;
pq.insert(item,priority);
break;

case 2:
pq.del();
break;
case 3:

pq.display();
break;
case 4:
break;
default:

cout<<"Wrong choice\n";
}
}
while(choice!=4);

getch();
}

OUTPUT:
1.Insert
2.Delete
3.Display

4.Quit
Enter your choice:1
Input the item value to be added in the queue:5
Enter its priority:1

1.Insert
2.Delete
3.Display

4.Quit
Enter your choice:1
Input the item value to be added in the queue:4
Enter its priority:2

1.Insert
2.Delete
3.Display
4.Quit

Enter your choice:1


Input the item value to be added in the queue:3
Enter its priority:3

1.Insert
2.Delete
3.Display
4.Quit

Enter your choice:1


Input the item value to be added in the queue:2
Enter its priority:4

1.Insert
2.Delete
3.Display
4.Quit

Enter your choice:1


Input the item value to be added in the queue:1
Enter its priority:5

1.Insert
2.Delete
3.Display
4.Quit

Enter your choice:3


Queue is:
Priority Item
1 5
2 4

3 3
4 2
5 1

1.Insert
2.Delete
3.Display
4.Quit

Enter your choice:2


Deleted item is:5

1.Insert

2.Delete
3.Display
4.Quit
Enter your choice:4
5)a)Program to insert an element in a binary search tree
#include<iostream.h>
#include<conio.h>
struct Node
{

int data;
Node*left;
Node*right;
};

Node*createNode(int data)
{
Node*newNode=new Node();
if(!newNode)

{
cout<<"Memory error\n";
return NULL;
}
newNode->data=data;

newNode->left=newNode->right=NULL;
return newNode;
}
Node*insertNode(Node*root, int data)

{
if(root==NULL)
{
root=createNode(data);

return root;
}
if(data<root->data)
{
root->left=insertNode(root->left, data);
}

else if(data>root->data)
{
root->right=insertNode(root->right, data);
}

return root;
}
void printTree(Node*root)
{

if(root==NULL)
return;
printTree(root->left);
cout<<root->data<<"";
printTree(root->right);

}
int main()
{
clrscr();

Node*root=NULL;
root=insertNode(root,50);
root=insertNode(root,30);
root=insertNode(root,20);

root=insertNode(root,40);
root=insertNode(root,70);
root=insertNode(root,60);
root=insertNode(root,80);

cout<<"Binary search tree:";


printTree(root);
cout<<endl;
getch();

OUTPUT:

Binary search tree: 20 30 40 50 60 70 80


5)b)Program to delete an element from a binary search tree
#include<iostream.h>
#include<conio.h>
struct Node
{

int data;
Node*left;
Node*right;
};

Node*createNode(int data)
{
Node*newNode=new Node();
if(!newNode)

{
cout<<"Memory error\n";
return NULL;
}
newNode->data=data;

newNode->left=newNode->right=NULL;
return newNode;
}
Node*insertNode(Node*root, int data)

{
if(root==NULL)
{
root=createNode(data);

return root;
}
if(data<root->data)
{
root->left=insertNode(root->left,data);
}

else if (data>root->data)
{
root->right=insertNode(root->right,data);
}

return root;
}
Node*findMin(Node*root)
{

while(root->left!=NULL)
{
root=root->left;
}
return root;

}
Node*deleteNode(Node*root, int data)
{
if(root==NULL)

return root;
if(data<root->data)
{
root->left=deleteNode(root->left,data);

}
else if(data>root->data)
{
root->right=deleteNode(root->right,data);

}
else
{
if(root->left==NULL && root->right==NULL)

{
delete root;
root=NULL;
}

else if(root->left==NULL)
{
Node*temp=root;
root=root->right;

delete temp;
}
else if(root->right==NULL)
{
Node*temp=root;

root=root->left;
delete temp;
}
else

{
Node*temp=findMin(root->right);
root->data=temp->data;
root->right=deleteNode(root->right,temp->data);

}
}
return root;
}

void printTree(Node*root)
{
if(root==NULL)
return;

printTree(root->left);
cout<<root->data<<"";
printTree(root->right);
}

int main()
{
clrscr();
Node*root=NULL;

root=createNode(50);
root=insertNode(root,30);
root=insertNode(root,20);
root=insertNode(root,40);
root=insertNode(root,70);

root=insertNode(root,60);
root=insertNode(root,80);
cout<<"Binary search tree before deletion:";
printTree(root);

cout<<endl;
root=deleteNode(root,20);
cout<<"Binary search tree after deletion:";
printTree(root);

cout<<endl;
getch();
}
OUTPUT:
Binary search tree before deletion: 20 30 40 50 60 70 80
Binary search tree after deletion: 30 40 50 60 70 80
5)c)Program to search a key element in a binary search tree
#include<iostream.h>
#include<conio.h>
struct Node
{

int data;
Node*left;
Node*right;
};

Node*createNode(int data)
{
Node*newNode=new Node();
if(!newNode)

{
cout<<"Memory error\n";
return NULL;
}
newNode->data=data;

newNode->left=newNode->right=NULL;
return newNode;
}
Node*insertNode(Node*root, int data)

{
if(root==NULL)
{
root=createNode(data);

return root;
}
if(data<root->data)
{
root->left=insertNode(root->left, data);
}

else if(data>root->data)
{
root->right=insertNode(root->right, data);
}

return root;
}
int searchNode(Node*root, int data)
{

if(root==NULL)
{
return 0;
}
if(data==root->data)

{
return 1;
}
if(data<root->data)

{
return searchNode(root->left, data);
}
else

{
return searchNode(root->right, data);
}
}

int main()
{
clrscr();
Node*root=NULL;

root=insertNode(root,50);
root=insertNode(root,30);
root=insertNode(root,20);
root=insertNode(root,40);

root=insertNode(root,70);
root=insertNode(root,60);
root=insertNode(root,80);
int searchValue=40;

int found=searchNode(root, searchValue);


if(found)
{
cout<<"Element "<<searchValue<<" found in the binary search tree."<<endl;
}

else
{
cout<<"Element "<<searchValue<<" not found in the binary search tree."<<endl;
}

getch();
}

OUTPUT:
Element 40 found in the binary search tree.
6)a)Program to perform insertion into an AVL-Tree
#include <iostream.h>
#include <conio.h>
struct Node
{

int key;
Node* left;
Node* right;
int height;

};
int max(int a, int b)
{
return (a > b) ? a : b;

}
Node* createNode(int key)
{
Node* newNode = new Node;
newNode->key = key;

newNode->left = newNode->right = NULL;


newNode->height = 1;
return newNode;
}

int getHeight(Node* node)


{
return node ? node->height : 0;
}

void updateHeight(Node* node)


{
node->height = max(getHeight(node->left), getHeight(node->right)) + 1;
}
int getBalance(Node* node)
{

return node ? getHeight(node->left) - getHeight(node->right) : 0;


}

Node* rotateRight(Node* node)

{
Node* temp = node->left;
Node* tempRight = temp->right;
temp->right = node;

node->left = tempRight;
updateHeight(node);
updateHeight(temp);
return temp;
}

Node* rotateLeft(Node* node)


{
Node* temp = node->right;

Node* tempLeft = temp->left;


temp->left = node;
node->right = tempLeft;
updateHeight(node);

updateHeight(temp);
return temp;
}
Node* rebalance(Node* node)

{
int balance = getBalance(node);
if (balance > 1)
{

if (getHeight(node->left->left) >= getHeight(node->left->right))


{
node = rotateRight(node);
}

Else
{
node->left = rotateLeft(node->left);
node = rotateRight(node);

}
}
else if (balance < -1)
{
if (getHeight(node->right->right) >= getHeight(node->right->left))

{
node = rotateLeft(node);
}
else

{
node->right = rotateRight(node->right);
node = rotateLeft(node);
}

}
return node;
}
Node* insertNode(Node* node, int key)

{
if (node == NULL)
{
return createNode(key);

}
if (key < node->key)
{
node->left = insertNode(node->left, key);

}
else if (key > node->key)
{
node->right = insertNode(node->right, key);

}
else
{
return node;
}

updateHeight(node);
node = rebalance(node);
return node;
}

void inorderTraversal(Node* node)


{
if (node != NULL)
{

inorderTraversal(node->left);
cout << node->key << " ";
inorderTraversal(node->right);
}

}
int main()
{
clrscr();

Node* root = NULL;


int keys[] = {10, 20, 30, 40, 50, 25};
int n = sizeof(keys) / sizeof(keys[0]);
for (int i = 0; i < n; i++)

{
root = insertNode(root, keys[i]);
}
cout << "Inorder Traversal: ";

inorderTraversal(root);
cout << endl;
getch();
return 0;
}

OUTPUT:
Inorder Traversal: 10 20 25 30 40 50
6)b)Program to perform deletion from an AVL-Tree
#include <iostream.h>
#include <conio.h>
struct Node
{

int key;
Node* left;
Node* right;
int height;

};
int height(Node* node)
{
return node ? node->height : 0;

}
int max(int a, int b)
{
return (a > b) ? a : b;
}

Node* rightRotate(Node* y)
{
Node* x = y->left;
y->left = x->right;

x->right = y;
y->height = max(height(y->left), height(y->right)) + 1;
x->height = max(height(x->left), height(x->right)) + 1;
return x;

}
Node* leftRotate(Node* x)
{
Node* y = x->right;
x->right = y->left;
y->left = x;

x->height = max(height(x->left), height(x->right)) + 1;


y->height = max(height(y->left), height(y->right)) + 1;
return y;
}

int getBalance(Node* node)


{
return node ? height(node->left) - height(node->right) : 0;
}

Node* insert(Node* node, int key)


{
if (!node)
{
Node* newNode = new Node;

newNode->key = key;
newNode->left = NULL;
newNode->right = NULL;
newNode->height = 1;

return newNode;
}
if (key < node->key)
{

node->left = insert(node->left, key);


}
else
{

node->right = insert(node->right, key);


}
node->height = 1 + max(height(node->left), height(node->right));
int balance = getBalance(node);

if (balance > 1 && key < node->left->key)


{
return rightRotate(node);
}

if (balance < -1 && key > node->right->key)


{
return leftRotate(node);

}
if (balance > 1 && key > node->left->key)
{
node->left = leftRotate(node->left);
return rightRotate(node);

}
if (balance < -1 && key < node->right->key)
{
node->right = rightRotate(node->right);

return leftRotate(node);
}
return node;
}

Node* deleteNode(Node* root, int key)


{
if (!root) 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 || !root->right)
{
Node* temp = root->left ? root->left : root->right;

delete root;
return temp;
}
Node* temp = root->right;
while (temp->left)

{
temp = temp->left;
}
root->key = temp->key;

root->right = deleteNode(root->right, temp->key);


}
root->height = 1 + max(height(root->left), height(root->right));
int balance = getBalance(root);

if (balance > 1 && getBalance(root->left) >= 0)


{
return rightRotate(root);
}

if (balance < -1 && getBalance(root->right) <= 0)


{
return leftRotate(root);
}

if (balance > 1 && getBalance(root->left) < 0)


{
root->left = leftRotate(root->left);
return rightRotate(root);

}
if (balance < -1 && getBalance(root->right) > 0)
{
root->right = rightRotate(root->right);

return leftRotate(root);
}

return root;
}

void inOrder(Node* root)


{
if (root)
{

inOrder(root->left);
cout << root->key << " ";
inOrder(root->right);
}

}
int main()
{
clrscr();

Node* root = NULL;


root = insert(root, 30);
root = insert(root, 20);
root = insert(root, 40);

root = insert(root, 10);


root = insert(root, 25);
cout << "In-order traversal of the AVL tree is:\n";
inOrder(root);

cout << endl;


root = deleteNode(root, 20);
cout << "In-order traversal after deletion of 20:\n";
inOrder(root);

cout << endl;


getch();
return 0;
}

OUTPUT:
In-order traversal of the AVL tree is:
10 20 25 30 40

In-order traversal after deletion of 20:


10 25 30 40
7)Program for the implementation of BFS and DFS

#include<iostream.h>

#include<conio.h>

const int MAX = 5;

int graph[MAX][MAX] = {

{0, 1, 1, 0, 0},

{1, 0, 0, 1, 1},

{1, 0, 0, 0, 1},

{0, 1, 0, 0, 0},

{0, 1, 1, 0, 0}

};

void BFS(int start)

int visited[MAX] = {0};

visited[start] = 1;

int queue[MAX], front = 0, rear = 0;

queue[rear++] = start;

while (front < rear)

int current = queue[front++];

cout << current << " ";

for (int i = 0; i < MAX; i++)

if (graph[current][i] && !visited[i])

visited[i] = 1;
queue[rear++] = i;

void DFS(int start, int visited[MAX])

visited[start] = 1;

cout << start << " ";

for (int i = 0; i < MAX; i++)

if (graph[start][i] && !visited[i])

DFS(i, visited);

int main()

clrscr();

cout << "Graph Adjacency Matrix:" << endl;

for (int i = 0; i < MAX; i++)

for (int j = 0; j < MAX; j++)

cout << graph[i][j] << " ";


}

cout << endl;

cout << "\nBreadth-First Search (BFS):" << endl;

BFS(0);

cout << endl;

cout << "\nDepth-First Search (DFS):" << endl;

int visited[MAX] = {0};

DFS(0, visited);

cout << endl;

getch();

OUTPUT:

Graph Adjacency Matrix:

0 1 1 0 0

1 0 0 1 1

1 0 0 0 1

0 1 0 0 0

0 1 1 0 0

Breadth-First Search (BFS):

01234

Depth-First Search (DFS):

01342
8)a)Program for implementing linear search method
#include<iostream.h>
#include<conio.h>
int linear_search(int arr[], int n, int target)
{

for(int i=0;i<n;i++)
{
if(arr[i]==target)
{

return i;
}
}
return -1;

}
void main()
{
clrscr();
int arr[]={3,6,1,8,2,4};

int n=sizeof(arr)/sizeof(arr[0]);
int target=8;
int result=linear_search(arr,n,target);
if(result!=-1)

{
cout<<"Element "<<target<<" found at index "<<result<<endl;
}
else

{
cout<<"Element"<<target<<"not found in the array"<<endl;
}
getch();
}

OUTPUT:
Element 8 found at index 3
8)b)Program for implementing binary search method
#include<iostream.h>
#include<conio.h>
void main()
{

clrscr();
int i,arr[10],num,first,last,middle;
cout<<"Enter 10 elements (in ascending order):";
for(i=0;i<10;i++)

cin>>arr[i];
cout<<"\n Enter element to be search:";
cin>>num;
first=0;

last=9;
middle=(first+last)/2;
while(first<=last)
{
if(arr[middle]<num)

first=middle+1;
else if(arr[middle]==num)
{
cout<<"\n The number "<<num<<" found at position "<<middle+1;

break;
}
else
last=middle-1;

middle=(first+last)/2;
}
if(first>last)
cout<<"\n The number "<<num<<" is not found in given array";
cout<<endl;
getch();

OUTPUT:

Enter 10 Elements(in ascending order):1


3
5
7

11
13
15
17
19

21
Enter element to be searched:7
The number 7 found at position 4
9)a)Program for implementing bubble sort
#include<iostream.h>
#include<conio.h>
void bubbleSort(int arr[], int n)
{

for(int i=0; i<n-1; i++)


{
int swapped=0;
for(int j=0; j<n-i-1; j++)

{
if(arr[j]>arr[j+1])
{
int temp=arr[j];

arr[j]=arr[j+1];
arr[j+1]=temp;
swapped=1;
}
}

if(!swapped)
break;
}
}

void printArray(int arr[], int n)


{
for(int i=0; i<n; i++)
cout<<arr[i]<<"";

cout<<endl;
}
int main()
{
clrscr();
int arr[]={64, 34, 25, 12, 22, 11, 90};

int n=sizeof(arr)/sizeof(arr[0]);
cout<<"Original array:";
printArray(arr, n);
bubbleSort(arr, n);

cout<<"Sorted array:";
printArray(arr, n);
getch();
}

OUTPUT:
Original array: 64 34 25 12 22 11 90
Sorted array: 11 12 22 25 34 64 90
9)b)Program for implementing selection sort
#include<iostream.h>
#include<conio.h>
void swap(int*xp, int*yp)
{

int temp=*xp;
*xp=*yp;
*yp=temp;
}

void selectionSort(int arr[], int n)


{
int i, j, min_idx;
for(i=0; i<n-1; i++)

{
min_idx=i;
for(j=i+1; j<n; j++)
if(arr[j]<arr[min_idx])
min_idx=j;

swap(&arr[min_idx], &arr[i]);
}
}
void printArray(int arr[], int size)

{
int i;
for(i=0; i<size; i++)
cout<<arr[i]<<"";

cout<<endl;
}
int main()
{
clrscr();
int arr[]={64, 25, 12, 22, 11};

int n=sizeof(arr)/sizeof(arr[0]);
selectionSort(arr, n);
cout<<"Sorted array: ";
printArray(arr, n);

getch();
}

OUTPUT:
Sorted array: 11 12 22 25 64
9)c)Program for implementing insertion sort

#include<iostream.h>
#include<conio.h>
void insertionSort(int arr[], int n)

{
for(int i=1; i<n; i++)
{
int key=arr[i];

int j=i-1;
while(j>=0&&arr[j]>key)
{
arr[j+1]=arr[j];

j=j-1;
}
arr[j+1]=key;
}
}

void printArray(int arr[], int n)


{
for(int i=0; i<n; i++)
cout<<arr[i]<<"";

cout<<endl;
}
int main()
{

clrscr();
int arr[]={12, 11, 13, 5, 6};
int n=sizeof(arr)/sizeof(arr[0]);
cout<<"Original array:";
printArray(arr, n);
insertionSort(arr, n);

cout<<"Sorted array:";
printArray(arr, n);
getch();
}

OUTPUT:
Original array: 12 11 13 5 6

Sorted array: 5 6 11 12 13
9)d)Program for implementing radix sort
#include <iostream.h>
#include <conio.h>
#define MAX 100
void countingSort(int arr[], int n, int exp)

{
int output[MAX];
int count[10] = {0};
int i;

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


{
int index = (arr[i] / exp) % 10;
count[index]++;

}
for (i = 1; i < 10; i++)
{
count[i] += count[i - 1];
}

for (i = n - 1; i >= 0; i--)


{
int index = (arr[i] / exp) % 10;
output[count[index] - 1] = arr[i];

count[index]--;
}
for (i = 0; i < n; i++)
{

arr[i] = output[i];
}
}
void radixsort(int arr[], int n)
{
int max = arr[0];

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


{
if (arr[i] > max)
{

max = arr[i];
}
}
for (int exp = 1; max / exp > 0; exp *= 10)

{
countingSort(arr, n, exp);
}
}
void printArray(int arr[], int n)

{
for (int i = 0; i < n; i++)
{
cout << arr[i] <<"";

}
cout << endl;
}
void main()

{
clrscr();
int arr[] = {170, 45, 75, 90, 802, 24, 2, 66};
int n = sizeof(arr) / sizeof(arr[0]);

cout <<"Unsorted array: ";


printArray(arr, n);
radixsort(arr, n);
cout <<"Sorted array: ";

printArray(arr, n);
getch();
}

OUTPUT:
Unsorted array: 170 45 75 90 802 24 2 66
Sorted array: 2 24 45 66 75 90 170 802

You might also like