3) Max Heap Sort
3) Max Heap Sort
#include <stdio.h>
void main()
scanf("%d", &no);
scanf("%d", &heap[i]);
c = i;
do {
root = (c - 1) / 2;
temp = heap[root];
heap[root] = heap[c];
heap[c] = temp;
c = root;
} while (c != 0);
temp = heap[0];
heap[0] = heap[j];
heap[j] = temp;
root = 0;
do {
c = 2 * root + 1;
c++;
temp = heap[root];
heap[root] = heap[c];
heap[c] = temp;
root = c;
printf("\n Complexity : \n Best case = Avg case = Worst case = O(n logn) \n");
}
5)knapsack using dynamic programming
#include<stdio.h>
int i, w;
int K[n+1][W+1];
if (i==0 || w==0)
K[i][w] = 0;
else
K[i][w] = K[i-1][w];
return K[n][W];
int main()
{
int i, n, val[20], wt[20], W;
scanf("%d", &n);
scanf("%d", &W);
return 0;
}
6) chain matrix multiplication
#include <stdio.h>
#include <limits.h>
int dp[100][100];
if (i == j) {
return 0;
if (dp[i][j] != -1) {
return dp[i][j];
dp[i][j] = INT_MAX;
+ matrixChainMemoised(p, k + 1, j)
+ matrixChainMemoised(p, k + 1, j)
return dp[i][j];
int i = 1, j = n - 1;
}
int main() {
dp[i][j] = -1;
return 0;
}
7) Making Change Problem using Dynamic Programming.
#include <stdio.h>
table[i][j] = x + y;
int main() {
int n = 4;
printf("The total number of combinations of coins that sum up to %d is %d\n", n, coins(arr, m, n));
return 0;
}
8) implement of knapsack using greedy
#include<stdio.h>
int main()
float weight[50],profit[50],ratio[50],Totalvalue,temp,capacity,amount;
int n,i,j;
scanf("%d",&n);
scanf("%f",&capacity);
for(i=0;i<n;i++)
ratio[i]=profit[i]/weight[i];
temp = ratio[j];
ratio[j] = ratio[i];
ratio[i] = temp;
temp = weight[j];
weight[j] = weight[i];
weight[i] = temp;
temp = profit[j];
profit[j] = profit[i];
profit[i] = temp;
break;
else
if (i < n)
return 0;
}
9) implementation of graph and searching using bfs&dfs.
#include <stdio.h>
#include <stdlib.h>
int data;
} Node;
int numVertices;
Node** adjLists;
} Graph;
if (!newNode) {
printf("Memory error\n");
return NULL;
newNode->data = data;
newNode->next = NULL;
return newNode;
if (!graph) {
printf("Memory error\n");
return NULL;
}
graph->numVertices = numVertices;
graph->adjLists[i] = NULL;
return graph;
newNode->next = graph->adjLists[src];
graph->adjLists[src] = newNode;
visited[i] = 0;
visited[startVertex] = 1;
while (temp) {
if (!visited[temp->data]) {
visited[temp->data] = 1;
while (temp2) {
if (!visited[temp2->data]) {
temp2 = temp2->next;
temp = temp->next;
visited[startVertex] = 1;
while (temp) {
if (!visited[temp->data]) {
temp = temp->next;
int main() {
int numVertices = 5;
addEdge(graph, 0, 1);
addEdge(graph, 0, 2);
addEdge(graph, 1, 3);
addEdge(graph, 2, 4);
printf("BFS Traversal: ");
bfs(graph, 0);
visited[i] = 0;
dfs(graph, 0, visited);
return 0;