0% found this document useful (0 votes)
3 views5 pages

DAA Practical Quick Revision Kit

The document provides implementations of various sorting algorithms (Insertion Sort, Merge Sort, Heap Sort, Quick Sort, Count Sort) and matrix multiplication (Strassen's method), along with graph traversal techniques (BFS, DFS) and algorithms for finding minimum spanning trees (Prim's Algorithm) and solving the 0-1 Knapsack Problem. Each algorithm includes C++ code and highlights the number of comparisons made during execution. This serves as a practical quick revision kit for data structures and algorithms.

Uploaded by

mussu0224
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)
3 views5 pages

DAA Practical Quick Revision Kit

The document provides implementations of various sorting algorithms (Insertion Sort, Merge Sort, Heap Sort, Quick Sort, Count Sort) and matrix multiplication (Strassen's method), along with graph traversal techniques (BFS, DFS) and algorithms for finding minimum spanning trees (Prim's Algorithm) and solving the 0-1 Knapsack Problem. Each algorithm includes C++ code and highlights the number of comparisons made during execution. This serves as a practical quick revision kit for data structures and algorithms.

Uploaded by

mussu0224
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/ 5

DAA Practical Quick Revision Kit

1. Insertion Sort (with comparisons)

#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;
}

2. Merge Sort (with comparisons)

#include<iostream>
using namespace std;
int comparisons = 0;

void merge(int arr[], int l, int m, int r) {


int n1 = m-l+1, n2 = r-m;
int L[n1], R[n2];
for (int i=0; i<n1; i++) L[i]=arr[l+i];
for (int j=0; j<n2; j++) R[j]=arr[m+1+j];
int i=0, j=0, k=l;
while (i<n1 && j<n2) {
comparisons++;
arr[k++] = (L[i] <= R[j]) ? L[i++] : R[j++];
}
while (i<n1) arr[k++] = L[i++];
while (j<n2) arr[k++] = R[j++];
}

void mergeSort(int arr[], int l, int r) {


if (l < r) {
int m = l+(r-l)/2;
mergeSort(arr, l, m);
mergeSort(arr, m+1, r);
merge(arr, l, m, r);
}
}
3. Heap Sort (with comparisons)

#include<iostream>
using namespace std;
int comparisons = 0;

void heapify(int arr[], int n, int i) {


int largest = i, l = 2*i+1, r = 2*i+2;
if (l < n && ++comparisons && arr[l] > arr[largest]) largest = l;
if (r < n && ++comparisons && arr[r] > arr[largest]) largest = r;
if (largest != i) {
swap(arr[i], arr[largest]);
heapify(arr, n, largest);
}
}

void heapSort(int arr[], int n) {


for (int i=n/2-1; i>=0; i--) heapify(arr, n, i);
for (int i=n-1; i>=0; i--) {
swap(arr[0], arr[i]);
heapify(arr, i, 0);
}
}

4. Quick Sort (with comparisons)

#include<iostream>
using namespace std;
int comparisons = 0;

int partition(int arr[], int low, int high) {


int pivot = arr[high], i = low-1;
for (int j=low; j<high; j++) {
comparisons++;
if (arr[j] < pivot) {
i++; swap(arr[i], arr[j]);
}
}
swap(arr[i+1], arr[high]);
return i+1;
}

void quickSort(int arr[], int low, int high) {


if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi-1);
quickSort(arr, pi+1, high);
}
}
5. Strassen’s Matrix Multiplication

#include<iostream>
using namespace std;

void strassen(int A[2][2], int B[2][2], int C[2][2]) {


int M1 = (A[0][0]+A[1][1])*(B[0][0]+B[1][1]);
int M2 = (A[1][0]+A[1][1])*B[0][0];
int M3 = A[0][0]*(B[0][1]-B[1][1]);
int M4 = A[1][1]*(B[1][0]-B[0][0]);
int M5 = (A[0][0]+A[0][1])*B[1][1];
int M6 = (A[1][0]-A[0][0])*(B[0][0]+B[0][1]);
int M7 = (A[0][1]-A[1][1])*(B[1][0]+B[1][1]);

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;

void countSort(int arr[], int n) {


int max = *max_element(arr, arr+n);
int count[max+1] = {}, output[n];

for (int i=0; i<n; i++) count[arr[i]]++;


for (int i=1; i<=max; i++) count[i] += count[i-1];
for (int i=n-1; i>=0; i--) output[--count[arr[i]]] = arr[i];
for (int i=0; i<n; i++) arr[i] = output[i];
}

7. BFS Traversal

#include<iostream>
#include<queue>
using namespace std;

void BFS(int graph[][5], int start, int n) {


bool visited[5] = {};
queue<int> q;
visited[start] = true;
q.push(start);

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;

void DFS(int graph[][5], int v, bool visited[], int n) {


visited[v] = true;
cout << v << " ";
for (int i=0; i<n; i++) {
if (graph[v][i] && !visited[i]) DFS(graph, i, visited, n);
}
}

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;
}

void primMST(int graph[V][V]) {


int parent[V], key[V]; bool mstSet[V] = {};
for (int i=0; i<V; i++) key[i] = 9999;
key[0] = 0; parent[0] = -1;

for (int count=0; count<V-1; count++) {


int u = minKey(key, mstSet);
mstSet[u] = true;
for (int v=0; v<V; v++)
if (graph[u][v] && !mstSet[v] && graph[u][v] < key[v])
parent[v]=u, key[v]=graph[u][v];
}
for (int i=1; i<V; i++)
cout << parent[i] << " - " << i << "\n";
}

10. 0-1 Knapsack Problem

#include<iostream>
using namespace std;

int knapsack(int W, int wt[], int val[], int n) {


int dp[n+1][W+1];
for (int i=0; i<=n; i++) {
for (int w=0; w<=W; w++) {
if (i==0 || w==0) dp[i][w] = 0;
else if (wt[i-1] <= w)
dp[i][w] = max(val[i-1] + dp[i-1][w-wt[i-1]], dp[i-1]
[w]);
else dp[i][w] = dp[i-1][w];
}
}
return dp[n][W];
}

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