Lab Report
Lab Report
SCIENCE (GWL.)
LAB
REPORT
OUTPUT:
2. SELECTION SORT
OUTPUT:
3. INSERTION SORT
OUTPUT:
4. QUICK SORT
OUTPUT
5. MERGE SORT
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);
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;
}
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 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;
}
// current size is
0 minHeap->size =
0;
}
if (smallest != idx) {
swapMinHeapNode(&minHeap-
>array[smallest],
&minHeap->array[idx]);
minHeapify(minHeap, smallest);
}
}
{
struct MinHeapNode* temp = minHeap->array[0];
minHeap->array[0] = minHeap->array[minHeap->size - 1];
--minHeap->size;
minHeapify(minHeap,
0);
return temp;
}
++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;
}
int n = minHeap->size -
1; int i;
for (i = (n - 1) / 2; i >= 0;
--i) minHeapify(minHeap,
i);
}
printf("\n");
}
// Utility function to check if this node is
leaf int isLeaf(struct MinHeapNode* root)
minHeap->size = size;
buildMinHeap(minHeap);
return minHeap;
}
while (!isSizeOne(minHeap)) {
left =
extractMin(minHeap);
right =
extractMin(minHeap);
top->left = left;
top->right =
right;
insertMinHeap(minHeap, top);
}
return extractMin(minHeap);
}
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)) {
{
struct MinHeapNode* root
= buildHuffmanTree(data, freq, size);
// Driver
code int
main()
{
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;
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;
}
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