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

Dsa Lab Manuals

Uploaded by

krish.work001
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)
16 views

Dsa Lab Manuals

Uploaded by

krish.work001
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/ 12

CGC College of Engineering

Landran, Mohali, Punjab

EXPERIMENT NO. 16

OBJECTIVE: Program to sort an array of integers in ascending order using


insertion sort.

SOLUTION: -

#include<iostream>
using namespace std;
int main()
{
int arr[50], tot, i, j, k, elem, index;
cout<<"Enter the Size for Array: ";
cin>>tot;
cout<<"Enter "<<tot<<" Array Elements: ";
for(i=0; i<tot; i++)
cin>>arr[i];
for(i=1; i<tot; i++)
{
elem = arr[i];
if(elem<arr[i-1])
{
for(j=0; j<=i; j++)
{
if(elem<arr[j])
{
index = j;
for(k=i; k>j; k--)
arr[k] = arr[k-1];
break;
}
}
}
else
continue;
arr[index] = elem;
}
cout<<"By insertion sorting..."<<endl;
cout<<"The Sorted Array:\n";
for(i=0; i<tot; i++)
cout<<arr[i]<<" ";
cout<<endl;
return 0;
}
CGC College of Engineering
Landran, Mohali, Punjab

Output:-
CGC College of Engineering
Landran, Mohali, Punjab

EXPERIMENT NO. 17

OBJECTIVE: Program to sort an array of integers in ascending order using Quick


Sort.

SOLUTION

#include <iostream>
using namespace std;
void swap(int *a, int *b) {
int t = *a;
*a = *b;
*b = t;
}
void printArray(int array[], int size) {
int i;
for (i = 0; i < size; i++)
cout << array[i] << " ";
cout << endl;
}
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);
}
}
int main() {
int data[10] ;
cout<<"Enter the 10 numbers to be sorted : "<<endl;
CGC College of Engineering
Landran, Mohali, Punjab

for(int i=0 ; i<10 ; i++){


cin>>data[i];
}
int n = sizeof(data) / sizeof(data[0]);

cout << "Unsorted Array: \n";


printArray(data, n);

// perform quicksort on data


quickSort(data, 0, n - 1);

cout << "Sorted array in ascending order: \n";


printArray(data, n);
}

Output:
CGC College of Engineering
Landran, Mohali, Punjab

EXPERIMENT NO 18

OBJECTIVE: Program to traverse a Binary search tree in Pre-order, In-order and


Post-order.

SOLUTION:

Inorder Traversal:
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* left;
Node* right;
Node(int v)
{
this->data = v;
this->left = this->right = NULL;
}
};
void printInorder(Node* node)
{
if (node == NULL)
return;
printInorder(node->left);
cout << node->data << " ";
printInorder(node->right);
}
int main()
{
Node* root = new Node(100);
root->left = new Node(20);
root->right = new Node(200);
root->left->left = new Node(10);
root->left->right = new Node(30);
root->right->left = new Node(150);
root->right->right = new Node(300);
cout << "Inorder Traversal: ";
printInorder(root);
return 0;
}
CGC College of Engineering
Landran, Mohali, Punjab

Output
Inorder Traversal: 10 20 30 100 150 200 300
Preorder Traversal:
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* left;
Node* right;
Node(int v)
{
this->data = v;
this->left = this->right = NULL;
}
};
void printPreOrder(Node* node)
{
if (node == NULL)
return;
cout << node->data << " ";
printPreOrder(node->left);
printPreOrder(node->right);
}
int main()
{
Node* root = new Node(100);
root->left = new Node(20);
root->right = new Node(200);
root->left->left = new Node(10);
root->left->right = new Node(30);
root->right->left = new Node(150);
root->right->right = new Node(300);
cout << "Preorder Traversal: ";
printPreOrder(root);
return 0;
}

Output
CGC College of Engineering
Landran, Mohali, Punjab

Preorder Traversal: 100 20 10 30 200 150 300

Postorder Traversal:
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* left;
Node* right;
Node(int v)
{
this->data = v;
this->left = this->right = NULL;
}
};
void printPostOrder(Node* node)
{
if (node == NULL)
return;
printPostOrder(node->left);
printPostOrder(node->right);
cout << node->data << " ";
}
int main()
{
Node* root = new Node(100);
root->left = new Node(20);
root->right = new Node(200);
root->left->left = new Node(10);
root->left->right = new Node(30);
root->right->left = new Node(150);
root->right->right = new Node(300);
cout << "PostOrder Traversal: ";
printPostOrder(root);
cout << "\n";
return 0;
}

Output
PostOrder Traversal: 10 30 20 150 300 200 100
CGC College of Engineering
Landran, Mohali, Punjab
CGC College of Engineering
Landran, Mohali, Punjab

EXPERIMENT NO 19

OBJECTIVE: Program to traverse graphs using BFS.

SOLUTION:

#include <bits/stdc++.h>
using namespace std;
class Graph {
int V;
vector<list<int> > adj;
public:
Graph(int V);
void addEdge(int v, int w);
void BFS(int s);
};

Graph::Graph(int V)
{
this->V = V;
adj.resize(V);
}

void Graph::addEdge(int v, int w)


{
adj[v].push_back(w);
}

void Graph::BFS(int s)
{
vector<bool> visited;
visited.resize(V, false);
list<int> queue;
visited[s] = true;
queue.push_back(s);
while (!queue.empty()) {
s = queue.front();
cout << s << " ";
queue.pop_front();
for (auto adjacent : adj[s]) {
if (!visited[adjacent]) {
visited[adjacent] = true;
CGC College of Engineering
Landran, Mohali, Punjab

queue.push_back(adjacent);
}
}
}
}
int main()
{
Graph g(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);
cout << "Following is Breadth First Traversal "
<< "(starting from vertex 2) \n";
g.BFS(2);
return 0;
}

Output
Following is Breadth First Traversal (starting from vertex 2)
2 0 3 1
CGC College of Engineering
Landran, Mohali, Punjab

EXPERIMENT NO 20

OBJECTIVE: Program to traverse graphs using DFS.

SOLUTION:

#include <bits/stdc++.h>
using namespace std;
class Graph {
public:
map<int, bool> visited;
map<int, list<int> > adj;
void addEdge(int v, int w);
void DFS(int v);
};

void Graph::addEdge(int v, int w)


{
adj[v].push_back(w);
}

void Graph::DFS(int v)
{
visited[v] = true;
cout << v << " ";
list<int>::iterator i;
for (i = adj[v].begin(); i != adj[v].end(); ++i)
if (!visited[*i])
DFS(*i);
}

int main()
{
Graph g;
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);
cout << "Following is Depth First Traversal"
" (starting from vertex 2) \n";
CGC College of Engineering
Landran, Mohali, Punjab

g.DFS(2);
return 0;
}

Output
Following is Depth First Traversal (starting from vertex 2)
2 0 1 3

You might also like