DAA
DAA
#include <stdio.h>
arr[i] = arr[j];
arr[j] = temp;
int i, j;
swap(arr, j, j + 1);
int main() {
int arr[] = { 6, 0, 3, 5 };
printf("\n");
// Calling bubble sort on array arr
bubbleSort(arr, N);
printf("\n");
return 0;
#include <math.h>
#include <stdio.h>
int j = i - 1;
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
int main() {
printf("\n");
insertionSort(arr, N);
printf("\n");
return 0;
int min_idx = i;
min_idx = j;
arr[min_idx] = arr[i];
arr[i] = temp;
int main() {
}
printf("\n");
selectionSort(arr, N);
printf("\n");
return 0;
#include <stdio.h>
#include <stdlib.h>
int i, j, k;
i = 0;
j = 0;
k = left;
arr[k] = leftArr[i];
i++;
else {
arr[k] = rightArr[j];
j++;
k++;
arr[k] = leftArr[i];
i++;
k++;
arr[k] = rightArr[j];
j++;
k++;
int main() {
mergeSort(arr, 0, n - 1);
return 0;
}
5. // code for binary search
include <stdio.h>
int main()
scanf("%d", &n);
scanf("%d", &array[c]);
scanf("%d", &search);
first = 0;
last = n - 1;
middle = (first+last)/2;
first = middle + 1;
break;
else
last = middle - 1;
middle = (first + last)/2;
return 0;
#include<stdio.h>
int main(){
scanf("%d", &a[i][j]);
scanf("%d", &b[i][j]);
printf("\n");
printf("%d\t", a[i][j]);
}
printf("\nThe second matrix is\n");
printf("\n");
printf("%d\t", b[i][j]);
c[0][1] = m3 + m5;
c[1][0] = m2 + m4;
c[1][1] = m1 - m2 + m3 + m6;
printf("\n");
printf("%d\t", c[i][j]);
return 0;
}
Output :
12
34
56
78
1 2
3 4
5 6
7 8
19 22
43 50
7 . // A C++ program for Prim's Minimum Spanning Tree (MST) algorithm. The program is for
adjacency matrix representation of the graph .
#include <bits/stdc++.h>
#define V 5
// A utility function to find the vertex with minimum key value, from the set of vertices not yet
included in MST
int minKey(vector<int> &key, vector<bool> &mstSet) {
return min_index;
cout << parent[i] << " - " << i << " \t"
// Function to construct and print MST for a graph represented using adjacency matrix
representation
vector<int> parent(V);
vector<int> key(V);
// Always include first 1st vertex in MST.Make key 0 so that this vertex is picked as first vertex.
key[0] = 0;
parent[0] = -1;
// Pick the minimum key vertex from the set of vertices not yet included in MST
mstSet[u] = true;
// Update key value and parent index of the adjacent vertices of the picked vertex .Consider
only those vertices which are not yet included in MST
// graph[u][v] is non zero only for adjacent vertices of m mstSet[v] is false for vertices not
yet included in MST Update the key only
}
// Print the constructed MST
printMST(parent, graph);
// Driver's code
int main() {
{ 0, 5, 7, 9, 0 } };
primMST(graph);
return 0;
8 . //Kruskal Algorithm
#include <stdio.h>
#include <stdlib.h>
int k, a, b, u, v, n, ne = 1;
int mincost = 0;
int applyfind(int i)
while(p[i] != 0)
i=p[i];
return i;
if(i!=j) {
p[j]=i;
return 1;
return 0;
int main()
n = 3;
int i, j;
if (cost[i][j] == 0) {
cost[i][j] = inf;
while(ne < n) {
min_val = cost[i][j];
a = u = i;
b = v = j;
u = applyfind(u);
v = applyfind(v);
if(applyunion(u, v) != 0) {
mincost +=min_val;
ne++;
return 0;
Output:
0 -> 1
1 -> 2
Minimum cost = 25
9 .// C program for Dijkstra's single source shortest path algorithm. The program is for adjacency
matrix representation of the graph.
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#define V 9
// A utility function to find the vertex with minimum distance value, from the set of vertices not
yet included in shortest path tree.
return min_index;
// Function that implements Dijkstra's single source shortest path algorithm for a graph
represented using adjacency matrix representation
int dist[V]; // The output array. dist[i] will hold the shortest distance from src to i
bool sptSet[V]; // sptSet[i] will be true if vertex i is included in shortest path tree or shortest
distance from src to i is finalized
dist[src] = 0;
sptSet[u] = true;
// Update dist[v] only if is not in sptSet, there is an edge from u to v, and total weight of
path from src to v throughu is smaller than current value of dist[v]
if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX && dist[u] + graph[u][v] < dist[v])
printSolution(dist);
// driver's code
int main()
{
/* Let us create the example graph discussed above */
// Function call
dijkstra(graph, 0);
return 0;
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
// deadline
} Job;
// profit
}
// Find minimum between two numbers.
slot[i] = false;
if (slot[j] == false) {
if (slot[i])
// Driver's code
int main()
{ 'b', 1, 19 },
{ 'c', 2, 27 },
{ 'd', 1, 25 },
{ 'e', 3, 15 } };
printf(
// Function call
printJobScheduling(arr, n);
return 0;
}
11 . Code for Queen Problem .
#include <bits/stdc++.h>
int currCol) {
int placedCol = i + 1;
if(abs(placedRow - currRow) ==
abs(placedCol - currCol)) {
return true;
result.push_back(board)
return;
if(!visited[row]) {
visited[row] = true;
board.push_back(row);
nQueenUtil(col + 1, n, board,
result, visited);
board.pop_back();
}
// Main function to find all distinct
vector<vector<int>> nQueen(int n) {
vector<vector<int>> result;
vector<int> board;
return result;
int main() {
int n = 4;
return 0;
}
#include <stdio.h>
// Base Cases
int main() {
int sum = 9;
return 0;