0% found this document useful (0 votes)
20 views13 pages

3) Max Heap Sort

Ada pdf

Uploaded by

7yczo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views13 pages

3) Max Heap Sort

Ada pdf

Uploaded by

7yczo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

3) max heap sort

#include <stdio.h>

void main()

int heap[10], no, i, j, c, root, temp;

printf("\n Enter no of elements :");

scanf("%d", &no);

printf("\n Enter the nos : ");

for (i = 0; i < no; i++)

scanf("%d", &heap[i]);

for (i = 1; i < no; i++)

c = i;

do {

root = (c - 1) / 2;

if (heap[root] < heap[c]) {

temp = heap[root];

heap[root] = heap[c];

heap[c] = temp;

c = root;

} while (c != 0);

printf("Heap array : ");

for (i = 0; i < no; i++)

printf("%d\t ", heap[i]);

for (j = no - 1; j >= 0; j--) {

temp = heap[0];
heap[0] = heap[j];

heap[j] = temp;

root = 0;

do {

c = 2 * root + 1;

if ((heap[c] < heap[c + 1]) && c < j-1)

c++;

if (heap[root]<heap[c] && c<j) {

temp = heap[root];

heap[root] = heap[c];

heap[c] = temp;

root = c;

} while (c < j);

printf("\n The sorted array is : ");

for (i = 0; i < no; i++)

printf("\t %d", heap[i]);

printf("\n Complexity : \n Best case = Avg case = Worst case = O(n logn) \n");

}
5)knapsack using dynamic programming

#include<stdio.h>

int max(int a, int b) { return (a > b)? a : b; }

int knapSack(int W, int wt[], int val[], int n)

int i, w;

int K[n+1][W+1];

for (i = 0; i <= n; i++)

for (w = 0; w <= W; w++)

if (i==0 || w==0)

K[i][w] = 0;

else if (wt[i-1] <= w)

K[i][w] = max(val[i-1] + K[i-1][w-wt[i-1]], K[i-1][w]);

else

K[i][w] = K[i-1][w];

return K[n][W];

int main()

{
int i, n, val[20], wt[20], W;

printf("Enter number of items:");

scanf("%d", &n);

printf("Enter value and weight of items:\n");

for(i = 0;i < n; ++i){

scanf("%d%d", &val[i], &wt[i]);

printf("Enter size of knapsack:");

scanf("%d", &W);

printf("%d", knapSack(W, wt, val, n));

return 0;

}
6) chain matrix multiplication

#include <stdio.h>

#include <limits.h>

int dp[100][100];

int matrixChainMemoised(int p[], int i, int j) {

if (i == j) {

return 0;

if (dp[i][j] != -1) {

return dp[i][j];

dp[i][j] = INT_MAX;

for (int k = i; k < j; k++) {

dp[i][j] = (dp[i][j] < (matrixChainMemoised(p, i, k)

+ matrixChainMemoised(p, k + 1, j)

+ p[i - 1] * p[k] * p[j])) ? dp[i][j] : (matrixChainMemoised(p, i, k)

+ matrixChainMemoised(p, k + 1, j)

+ p[i - 1] * p[k] * p[j]);

return dp[i][j];

int MatrixChainOrder(int p[], int n) {

int i = 1, j = n - 1;

return matrixChainMemoised(p, i, j);

}
int main() {

int arr[] = {1, 2, 3, 4};

int n = sizeof(arr) / sizeof(arr[0]); // Corrected this line

for (int i = 0; i < 100; i++) {

for (int j = 0; j < 100; j++) {

dp[i][j] = -1;

printf("Minimum number of multiplications is %d\n", MatrixChainOrder(arr, n));

return 0;

}
7) Making Change Problem using Dynamic Programming.

#include <stdio.h>

int coins(int S[], int m, int n) {

int table[n + 1][m]; // Create a table to store results of subproblems

for (int i = 0; i < m; i++) {

table[0][i] = 1; // There is one way to make 0 amount: use no coins

for (int i = 1; i <= n; i++) {

for (int j = 0; j < m; j++) {

int x = (i - S[j] >= 0) ? table[i - S[j]][j] : 0;

int y = (j >= 1) ? table[i][j - 1] : 0;

table[i][j] = x + y;

return table[n][m - 1];

int main() {

int arr[] = {1, 2, 3};

int m = sizeof(arr) / sizeof(arr[0]);

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;

printf("Enter the number of items :");

scanf("%d",&n);

for (i = 0; i < n; i++)

printf("Enter Weight and Profit for item[%d] :\n",i);

scanf("%f %f", &weight[i], &profit[i]);

printf("Enter the capacity of knapsack :\n");

scanf("%f",&capacity);

for(i=0;i<n;i++)

ratio[i]=profit[i]/weight[i];

for (i = 0; i < n; i++)

for (j = i + 1; j < n; j++)

if (ratio[i] < ratio[j])

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;

printf("Knapsack problems using Greedy Algorithm:\n");

for (i = 0; i < n; i++)

if (weight[i] > capacity)

break;

else

Totalvalue = Totalvalue + profit[i];

capacity = capacity - weight[i];

if (i < n)

Totalvalue = Totalvalue + (ratio[i]*capacity);

printf("\nThe maximum value is :%f\n",Totalvalue);

return 0;

}
9) implementation of graph and searching using bfs&dfs.

#include <stdio.h>

#include <stdlib.h>

typedef struct Node {

int data;

struct Node* next;

} Node;

typedef struct Graph {

int numVertices;

Node** adjLists;

} Graph;

Node* createNode(int data) {

Node* newNode = (Node*) malloc(sizeof(Node));

if (!newNode) {

printf("Memory error\n");

return NULL;

newNode->data = data;

newNode->next = NULL;

return newNode;

Graph* createGraph(int numVertices) {

Graph* graph = (Graph*) malloc(sizeof(Graph));

if (!graph) {

printf("Memory error\n");

return NULL;
}

graph->numVertices = numVertices;

graph->adjLists = (Node**) malloc(numVertices * sizeof(Node*));

for (int i = 0; i < numVertices; i++) {

graph->adjLists[i] = NULL;

return graph;

void addEdge(Graph* graph, int src, int dest) {

Node* newNode = createNode(dest);

newNode->next = graph->adjLists[src];

graph->adjLists[src] = newNode;

void bfs(Graph* graph, int startVertex) {

int* visited = (int*) malloc(graph->numVertices * sizeof(int));

for (int i = 0; i < graph->numVertices; i++) {

visited[i] = 0;

visited[startVertex] = 1;

printf("%d ", startVertex);

Node* temp = graph->adjLists[startVertex];

while (temp) {

if (!visited[temp->data]) {

printf("%d ", temp->data);

visited[temp->data] = 1;

Node* temp2 = graph->adjLists[temp->data];

while (temp2) {

if (!visited[temp2->data]) {

printf("%d ", temp2->data);


visited[temp2->data] = 1;

temp2 = temp2->next;

temp = temp->next;

// Function to perform DFS traversal

void dfs(Graph* graph, int startVertex, int* visited) {

visited[startVertex] = 1;

printf("%d ", startVertex);

Node* temp = graph->adjLists[startVertex];

while (temp) {

if (!visited[temp->data]) {

dfs(graph, temp->data, visited);

temp = temp->next;

int main() {

int numVertices = 5;

Graph* graph = createGraph(numVertices);

addEdge(graph, 0, 1);

addEdge(graph, 0, 2);

addEdge(graph, 1, 3);

addEdge(graph, 2, 4);
printf("BFS Traversal: ");

bfs(graph, 0);

printf("\nDFS Traversal: ");

int* visited = (int*) malloc(numVertices * sizeof(int));

for (int i = 0; i < numVertices; i++) {

visited[i] = 0;

dfs(graph, 0, visited);

return 0;

You might also like