0% found this document useful (0 votes)
11 views33 pages

Lab Report

The document is a lab report from Madhav Institute of Technology and Science, detailing various sorting algorithms including Bubble Sort, Selection Sort, Insertion Sort, Quick Sort, Merge Sort, Bucket Sort, Heap Sort, Radix Sort, Linear Search, and Binary Search. It also covers dynamic programming topics such as Matrix Chain Multiplication and Longest Common Subsequence, along with the implementation of Optimal Binary Search Tree and Huffman Coding. Additionally, it discusses Dijkstra’s Algorithm and Bellman Ford Algorithm, providing code snippets and time complexity analysis for each algorithm.

Uploaded by

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

Lab Report

The document is a lab report from Madhav Institute of Technology and Science, detailing various sorting algorithms including Bubble Sort, Selection Sort, Insertion Sort, Quick Sort, Merge Sort, Bucket Sort, Heap Sort, Radix Sort, Linear Search, and Binary Search. It also covers dynamic programming topics such as Matrix Chain Multiplication and Longest Common Subsequence, along with the implementation of Optimal Binary Search Tree and Huffman Coding. Additionally, it discusses Dijkstra’s Algorithm and Bellman Ford Algorithm, providing code snippets and time complexity analysis for each algorithm.

Uploaded by

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

MADHAV INSTITUTE OF TECHNOLOGY AND

SCIENCE (GWL.)

LAB
REPORT

SUBMITTED TO: SUBMITTED BY:


Prof. Ranjeet Kumar Singh Nishika Garg
Prof. Khushi Agrawal (0901cs211077)
1. BUBBLE SORT

OUTPUT:
2. SELECTION SORT
OUTPUT:

3. INSERTION SORT
OUTPUT:

4. QUICK SORT
OUTPUT

5. MERGE SORT

while(i<nl) { //extra element in left array


array[k] =
larr[i];
} i++; k++;
while(j<nr) //extra element in right
array
{ array[k] = rarr[j];
j++; k++;
}
}
void mergeSort(int *array, int l, int
r) { int m;
if(l < r) {
int m = l+(r-l)/2;
// Sort first and second
arrays mergeSort(array, l,
m); mergeSort(array, m+1,
r); merge(array, l, m, r);
}
}
int main()
{ int n;
cout << "Enter the number of
elements: "; cin >> n;
int arr[n]; //create an array with given number of
elements cout << "Enter elements:" << endl;
for(int i = 0; i<n; i++)
{ cin >> arr[i];
}
cout << "Array before
Sorting: "; display(arr, n);
mergeSort(arr, 0, n-1); //(n-1) for last
index cout << "Array after Sorting: ";
display(arr, n);
}

OUTPUT:

6. BUCKET SORT
#include<iostream>
#include<vector>
#include<algorithm
> using namespace
std;
void display(float *array, int
size) { for(int i = 0; i<size;
i++)
cout << array[i] << "
"; cout << endl;
}
void bucketSort(float *array, int
size) { vector<float>
bucket[size];
for(int i = 0; i<size; i++) { //put elements into
different buckets
bucket[int(size*array[i])].push_back(array[i]);
}
for(int i = 0; i<size; i++) {
sort(bucket[i].begin(), bucket[i].end()); //sort
individual vectors
}
int index = 0;
for(int i = 0; i<size; i++) {
while(!bucket[i].empty()) {
array[index++] =
*(bucket[i].begin());
bucket[i].erase(bucket[i].begin())
;
}
}
}
int main()
{ int n;
cout << "Enter the number of
elements: "; cin >> n;
float arr[n]; //create an array with given number of
elements cout << "Enter elements:" << endl;
for(int i = 0; i<n; i++)
{ cin >> arr[i];
}
cout << "Array before
Sorting: "; display(arr, n);

OUTPUT
7. HEAP SORT
#include <iostream>
using namespace
std;
// A function to heapify the
array. void MaxHeapify(int a[],
int i, int n)
{
int j,
temp; temp
= a[i]; j
= 2*i;
while (j <= n)
{
if (j < n && a[j+1] >
a[j]) j = j+1;
// Break if parent value is already greater than child
value. if (temp > a[j])
break;
// Switching value with the parent node if temp <
a[j]. else if (temp <= a[j])
{
a[j/2] =
a[j]; j =
2*j;
}
}
a[j/2] =
temp;
return;
}
void HeapSort(int a[], int n)
{
int i, temp;
for (i = n; i >= 2; i--)
{
// Storing maximum value at the
end. temp = a[i];
}
}
void Build_MaxHeap(int a[], int n)
{
int i;
for(i = n/2; i >= 1;
i--) MaxHeapify(a,
i, n);
}
int main()
{
int n, i;
cout<<"\nEnter the number of data element to be
sorted: "; cin>>n;
n++;
int arr[n];
for(i = 1; i < n; i++)
{
cout<<"Enter element
"<<i<<": "; cin>>arr[i];
}
// Building max heap.
Build_MaxHeap(arr, n-
1); HeapSort(arr, n-
1);

// Printing the sorted


data. cout<<"\nSorted
Data ";

for (i = 1; i < n;
i++) cout<<"-
>"<<arr[i];

OUTPUT:

8. RADIX SORT
#include<iostream>
#include<list>
#include<cmath>
using namespace
std;
void display(int *array, int
size) { for(int i = 0;
i<size; i++)
cout << array[i] << "
"; cout << endl;
}
void radixSort(int *arr, int n, int max)
{ int i, j, m, p = 1, index, temp,
count = 0;
list<int> pocket[10]; //radix of decimal number
is 10 for(i = 0; i< max; i++) {
m = pow(10,
i+1); p =
pow(10, i);
for(j = 0; j<n; j+
+) { temp =
arr[j]%m;
index = temp/p; //find index for pocket
array pocket[index].push_back(arr[j]);
}
count = 0;
for(j = 0; j<10; j++) {
//delete from linked lists and store to
array while(!pocket[j].empty()) {
arr[count] = *(pocket[j].begin());
pocket[j].erase(pocket[j].begin());
count++;
}
}
}
}
int main() {
int n,
max;
cout << "Enter the number of
elements: "; cin >> n;
cout << "Enter the maximum digit of
elements: "; cin >> max;
int arr[n]; //create an array with given number of
elements cout << "Enter elements:" << endl;
for(int i = 0; i<n; i+
+) { cin >> arr[i];
}
cout << "Data before
Sorting: "; display(arr, n);
radixSort(arr, n, max);
cout << "Data after
Sorting: "; display(arr, n);
}
OUTPUT:

9. LINEAR SEARCH
//NG
//LINEAR SEARCH
#include<iostream>
using namespace
std; int
item,size,i;
void display(int*arr,int
n){ for(i=0;i<n;i++){
cout<<arr[i]<<endl;
}
}
int linearSearch(int *arr,int item,int
size){ for(i=0;i<size;i++){
if(arr[i]==item){
cout<<"element found at" <<
i+1<<"position"; return 1;

}
else{
return 0;

}
}
}

int main(){
int n,m;
cout<<"enter number of elements
: "; cin>>n;
int arr[n];
cout<<"enter elements :
"; for(int i=0;i<n;i++)
{
cin>>arr[i];
cout<<"array
is:"<<endl;
display(arr,n);
cout<<"enter element you want to search
: "; cin>>m;
linearSearch(arr,m,n);
return 0;

10. BINARY SEARCH


//NG
//BINARY SEARCH
#include<iostream>
using namespace
std; int
size,item;
void display(int *arr,int
size){ for(int
i=0;i<size;i++){
cout<<arr[i];
}

}
int binarySearch(int *arr,int item,int
size ){ int mid;
int l=0;
int u=size-
1;
while(l<=u)
{
mid=(l+u)/2;
if(item==arr[mid]){
cout<<"element found at
"<<mid<<"position"; return 1;

}
else
if(item<arr[mid])
{ u=mid-1;

}
else
if(item>arr[mid])
{ l=mid+1;

}
else{
int main(){
int n,m;
cout<<"enter number of elements
: "; cin>>n;
int arr[n];
cout<<"enter elements :
"; for(int i=0;i<n;i++)
cin>>arr[i];
display(arr,n);
cout<<"enter element to be searched
: "; cin>>m;
binarySearch(arr,m,n);

return 0;

}
EXPERIMENT 2
TOPIC: MATRIX CHAIN MULTIPLICATION

OUTPUT:

TIME COMPLEXITY:
The matrix chain multiplication is a dynamic
programming paradigm and takes O(n3) computational
complexity.
EXPERIMENT 3
TOPIC: LONGEST COMMON SUBSEQUENCE

OUTPUT:

TIME COMPEXITY:
The general algorithms which are followed to solve the
Longest Common Subsequence (LCS) problems have both
time complexity and space complexity of O(m * n)
EXPERIMENT 4
WAP to implement Optimal Binary Search Tree Problem
#include <bits/stdc+
+.h> using namespace
std;
int sum(int freq[], int i, int
j); int optCost(int freq[], int
i, int j)
{if (j <
i) return
0;
if (j == i)
return freq[i];
int fsum = sum(freq, i,
j); int min = INT_MAX;
for (int r = i; r <= j; ++r)
{int cost = optCost(freq, i, r -
1) + optCost(freq, r + 1, j);
if (cost <
min) min =
cost;}
return min + fsum;
}
int optimalSearchTree(int
keys[], int freq[], int n)
{
return optCost(freq, 0, n - 1);
}
int sum(int freq[], int i, int j)
{int s = 0;
for (int k = i; k <= j;
k++) s += freq[k];
return s;
}
int main()
{
int keys[] = {10, 12, 20};
int freq[] = {34, 8, 50};
int n = sizeof(keys) /
sizeof(keys[0]); cout << "Cost of
HUFFMANN CODING
// C program for Huffman
Coding #include <stdio.h>
#include <stdlib.h>

#define MAX_TREE_HT

100 struct

MinHeapNode {

char data;

unsigned

freq;

struct MinHeapNode *left, *right;


};

struct MinHeap {
unsigned size;
unsigned
capacity;
struct MinHeapNode** array;
};
struct MinHeapNode* newNode(char data, unsigned freq)
{
struct MinHeapNode* temp = (struct
MinHeapNode*)malloc( sizeof(struct
MinHeapNode));
temp->left = temp->right =
NULL; temp->data = data;
temp->freq = freq;

return temp;
}

struct MinHeap* createMinHeap(unsigned capacity)

struct MinHeap* minHeap


= (struct MinHeap*)malloc(sizeof(struct MinHeap));

// current size is
0 minHeap->size =
0;
}

void swapMinHeapNode(struct MinHeapNode** a,


struct MinHeapNode** b)

struct MinHeapNode* t = *a;


*a = *b;
*b = t;
}

// The standard minHeapify function.


void minHeapify(struct MinHeap* minHeap, int idx)

int smallest = idx;


int left = 2 * idx +
1;
int right = 2 * idx + 2;

if (left < minHeap->size


&& minHeap->array[left]->freq
< minHeap->array[smallest]-
>freq) smallest = left;

if (right < minHeap->size


&& minHeap->array[right]->freq
< minHeap->array[smallest]-
>freq) smallest = right;

if (smallest != idx) {
swapMinHeapNode(&minHeap-
>array[smallest],
&minHeap->array[idx]);
minHeapify(minHeap, smallest);
}
}

int isSizeOne(struct MinHeap* minHeap)


{

return (minHeap->size == 1);


}

struct MinHeapNode* extractMin(struct MinHeap* minHeap)

{
struct MinHeapNode* temp = minHeap->array[0];
minHeap->array[0] = minHeap->array[minHeap->size - 1];

--minHeap->size;
minHeapify(minHeap,
0);

return temp;
}

void insertMinHeap(struct MinHeap* minHeap,


struct MinHeapNode* minHeapNode)

++minHeap->size;
int i = minHeap->size - 1;

while (i
&& minHeapNode->freq
< minHeap->array[(i - 1) / 2]->freq) {

minHeap->array[i] = minHeap->array[(i - 1)
/ 2]; i = (i - 1) / 2;
}

minHeap->array[i] = minHeapNode;
}

// A standard function to build min


heap void buildMinHeap(struct
MinHeap* minHeap)

int n = minHeap->size -
1; int i;

for (i = (n - 1) / 2; i >= 0;
--i) minHeapify(minHeap,
i);
}

void printArr(int arr[], int n)


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

printf("\n");
}
// Utility function to check if this node is
leaf int isLeaf(struct MinHeapNode* root)

return !(root->left) && !(root->right);


}

struct MinHeap* createAndBuildMinHeap(char data[],


int freq[], int size)

struct MinHeap* minHeap = createMinHeap(size);

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


minHeap->array[i] = newNode(data[i], freq[i]);

minHeap->size = size;
buildMinHeap(minHeap);

return minHeap;
}

// The main function that builds Huffman


tree struct MinHeapNode*
buildHuffmanTree(char data[],
int freq[], int size)

struct MinHeap* minHeap


= createAndBuildMinHeap(data, freq, size);

while (!isSizeOne(minHeap)) {

left =
extractMin(minHeap);
right =
extractMin(minHeap);

top = newNode('$', left->freq + right->freq);

top->left = left;
top->right =
right;

insertMinHeap(minHeap, top);
}

return extractMin(minHeap);
}

void printCodes(struct MinHeapNode* root, int arr[],


int top)

if (root->left) {

arr[top] = 0;
printCodes(root->left, arr, top + 1);
}

if (root->right) {

arr[top] = 1;
printCodes(root->right, arr, top + 1);
}

if (isLeaf(root)) {

printf("%c: ", root-


>data); printArr(arr,
top);
}
}

void HuffmanCodes(char data[], int freq[], int size)

{
struct MinHeapNode* root
= buildHuffmanTree(data, freq, size);

int arr[MAX_TREE_HT], top = 0;

printCodes(root, arr, top);


}

// Driver
code int
main()
{

char arr[] = { 'a', 'b', 'c', 'd', 'e',


'f' }; int freq[] = { 5, 9, 12, 13, 16,
45 };

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

HuffmanCodes(arr, freq, size);


EXPERIMENT 5

TOPIC : Dijkstra’s Algorithm


Time complexity:
O(v^2) {v=no. of vertices in graph}
EXPERIMENT-6
Bellman Ford Algorithm
#include <bits/stdc+
+.h> struct Edge {
int
u;
int
v;
int
w;
};
struct Graph
{ int V;
int E;
struct Edge* edge;
};
struct Graph* createGraph(int V, int
E) { struct Graph* graph = new
Graph;
graph->V = V; // Total
Vertices graph->E = E; //
Total edges

// Array of edges for


graph graph->edge = new
Edge[E]; return graph;
}

void printArr(int arr[], int


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

void BellmanFord(struct Graph* graph, int


u) { int V = graph->V;
int E = graph-
>E; int
dist[V];

for (int i = 0; i < V; i+


+) dist[i] = INT_MAX;
dist[u] = 0;
for (int i = 1; i <= V - 1; i+
dist[v] = dist[u] + w;
}
}
for (int i = 0; i < E; i+
+) { int u = graph-
>edge[i].u; int v =
graph->edge[i].v; int w
= graph->edge[i].w;
if (dist[u] != INT_MAX && dist[u] + w <
dist[v]) { printf("Graph contains negative
w cycle"); return;
}
}printArr(dist, V);

return;
}
int main()
{ int V =
5;
int E = 8;
struct Graph* graph = createGraph(V,
E); graph->edge[0].u = 0;
graph->edge[0].v = 1;
graph->edge[0].w = 5;

//edge 0 --> 2
graph->edge[1].u = 0;
graph->edge[1].v = 2;
graph->edge[1].w = 4;

//edge 1 --> 3
graph->edge[2].u = 1;
graph->edge[2].v = 3;
graph->edge[2].w = 3;

//edge 2 --> 1
graph->edge[3].u = 2;
graph->edge[3].v = 1;
graph->edge[3].w = 6;

//edge 3 --> 2
graph->edge[4].u = 3;
graph->edge[4].v = 2;
graph->edge[4].w = 2;

BellmanFord(graph, 0); //0 is the source vertex

return 0;
}
TIME COMPLEXITY
Time complexity of Bellman-Ford is
O(VE), which is more than Dijkstra.
where V is a number of vertices and E
is a number of edges. For a complete
graph with n vertices, V = n, E = O(n2).
So overall time complexity becomes
O(n3).
EXPERIMENT 7
WAP to implement DFS and analyze their time
complexities.

#include
<iostream>
#include <list>
using namespace
std; class Graph {
int numVertices;
list<int>
*adjLists; bool
*visited; public:
Graph(int V);
void addEdge(int src, int
dest); void DFS(int vertex);
};
// Initialize graph
Graph::Graph(int
vertices) {
numVertices = vertices;
adjLists = new
list<int>[vertices]; visited =
new bool[vertices];
}
// Add edges
void Graph::addEdge(int src, int
dest) {
adjLists[src].push_front(dest);
}
// DFS algorithm
void Graph::DFS(int
vertex) {
visited[vertex] = true;
list<int> adjList =
adjLists[vertex]; cout << vertex
<< " "; list<int>::iterator i;
for (i = adjList.begin(); i != adjList.end();
++i) if (!visited[*i])
DFS(*i);
}
int main()
{ Graph
g.DFS(2);
return 0;
}

TC=Time Complexity of DFS is -


OP(V+E) where V is vertices and E is
edges.

EXPERIMENT 8
Wap to implement BFS
TIME COMPLEXITY:
The Time complexity of BFS is O(V + E) when Adjacency List is used and O(V^2)
when Adjacency Matrix is used, where V stands for vertices and E stands for edges
EXPERIMENT 9
. WAP to Implement 0/1 knapsack using
dynamic programming.

Output:
TIME COMPLEXITY:
Time complexity for 0/1 Knapsack problem solved
using DP is O(N*W) where N denotes number of
items available and W denotes the capacity of the
knapsack.

THANK YOU

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy