Lab Manual Daa
Lab Manual Daa
#include <math.h>
#include <stdio.h>
int main() {
int arr[] = { 12, 11, 13, 5, 6 };
int N = sizeof(arr) / sizeof(arr[0]);
return 0;
}
Output
Unsorted array: 12 11 13 5 6
Sorted array: 5 6 11 12 13
EXPERIMENT 2
#include <stdio.h>
#include <stdlib.h>
int main() {
int arr[] = { 12, 11, 13, 5, 6, 7 };
int n = sizeof(arr) / sizeof(arr[0]);
Output
Given array is
12 11 13 5 6 7
Sorted array is
5 6 7 11 12 13
EXPERIMENT 3
#include <stdio.h>
// Function to swap two elements
void swap(int* a, int* b) {
int t = *a;
*a = *b;
*b = t;
}
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = (low - 1);
int main() {
int arr[] = { 12, 17, 6, 25, 1, 5 };
int n = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, n - 1);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}
Output
Sorted array:
1 5 6 12 17 25
EXPERIMENT 4
#include <iostream>
using namespace std;
// left = 2*i + 1
int l = 2 * i + 1;
// right = 2*i + 2
int r = 2 * i + 2;
// Driver's code
int main()
{
int arr[] = { 12, 11, 13, 5, 6, 7 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function call
heapSort(arr, N);
Output
Sorted array is
5 6 7 11 12 13
EXPERIMENT 5
#include <iostream>
using namespace std;
int main()
{
int arr[] = {12, 34, 54, 2, 3}, i;
int n = sizeof(arr)/sizeof(arr[0]);
shellSort(arr, n);
return 0;
}
Output
Array before sorting:
12 34 54 2 3
Array after sorting:
2 3 12 34 54
EXPERIMENT 6
#include <iostream>
using namespace std;
#include <limits.h>
return min_index;
}
// driver's code
int main()
{
// Function call
dijkstra(graph, 0);
return 0;
}
Output
EXPERIMENT 7
#include <bits/stdc++.h>
using namespace std;
return min_index;
}
// Driver's code
int main()
{
int graph[V][V] = { { 0, 2, 0, 6, 0 },
{ 2, 0, 3, 8, 5 },
{ 0, 3, 0, 0, 7 },
{ 6, 8, 0, 0, 9 },
{ 0, 5, 7, 9, 0 } };
return 0;
}
Output
Edge Weight
0-1 2
1-2 3
0-3 6
1-4 5
EXPERIMENT 8
public:
DSU(int n)
{
parent = new int[n];
rank = new int[n];
// Find function
int find(int i)
{
if (parent[i] == -1)
return i;
// Union function
void unite(int x, int y)
{
int s1 = find(x);
int s2 = find(y);
if (s1 != s2) {
if (rank[s1] < rank[s2]) {
parent[s1] = s2;
}
else if (rank[s1] > rank[s2]) {
parent[s2] = s1;
}
else {
parent[s2] = s1;
rank[s1] += 1;
}
}
}
};
class Graph {
vector<vector<int> > edgelist;
int V;
public:
Graph(int V) { this->V = V; }
void kruskals_mst()
{
// Sort all edges
sort(edgelist.begin(), edgelist.end());
// Driver code
int main()
{
Graph g(4);
g.addEdge(0, 1, 10);
g.addEdge(1, 3, 15);
g.addEdge(2, 3, 4);
g.addEdge(2, 0, 6);
g.addEdge(0, 3, 5);
// Function call
g.kruskals_mst();
return 0;
}
Output
Following are the edges in the constructed MST
2 -- 3 == 4
0 -- 3 == 5
0 -- 1 == 10
Minimum Cost Spanning Tree: 19
EXPERIMENT 9
#include <bits/stdc++.h>
using namespace std;
// Base Case
if (n == 0 || W == 0)
return 0;
// Driver code
int main()
{
int profit[] = { 60, 100, 120 };
int weight[] = { 10, 20, 30 };
int W = 50;
int n = sizeof(profit) / sizeof(profit[0]);
cout << knapSack(W, weight, profit, n);
return 0;
}
Output
220
EXPERIMENT 10
#include<stdio.h>
int main(){
int start[] = {1 , 5 , 12};
int finish[] = {10, 13, 23};
int activities = sizeof(start)/sizeof(start[0]);
int i, j;
printf ("Following activities are selected \t");
i = 0;
printf("%d\t", i);
for (j = 1; j < activities; j++){
if (start[j] >= finish[i]){
printf ("%d ", j);
i = j;
}
}
return 0;
}
Output
Following activities are selected 0 2