DAA Practical Quick Revision Kit
DAA Practical Quick Revision Kit
#include<iostream>
using namespace std;
int insertionSort(int arr[], int n) {
int comparisons = 0;
for (int i = 1; i < n; i++) {
int key = arr[i], j = i - 1;
while (j >= 0 && ++comparisons && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
return comparisons;
}
#include<iostream>
using namespace std;
int comparisons = 0;
#include<iostream>
using namespace std;
int comparisons = 0;
#include<iostream>
using namespace std;
int comparisons = 0;
#include<iostream>
using namespace std;
C[0][0] = M1 + M4 - M5 + M7;
C[0][1] = M3 + M5;
C[1][0] = M2 + M4;
C[1][1] = M1 - M2 + M3 + M6;
}
6. Count Sort
#include<iostream>
using namespace std;
7. BFS Traversal
#include<iostream>
#include<queue>
using namespace std;
while (!q.empty()) {
int v = q.front(); q.pop();
cout << v << " ";
for (int i=0; i<n; i++) {
if (graph[v][i] && !visited[i]) {
visited[i] = true; q.push(i);
}
}
}
}
8. DFS Traversal
#include<iostream>
using namespace std;
9. Prim’s Algorithm
#include<iostream>
using namespace std;
#define V 5
int minKey(int key[], bool mstSet[]) {
int min = 9999, min_index;
for (int v = 0; v < V; v++)
if (!mstSet[v] && key[v] < min)
min = key[v], min_index = v;
return min_index;
}
#include<iostream>
using namespace std;