DAA FILE AVIRAL Chirag
DAA FILE AVIRAL Chirag
OF INSTITUTIONS
Submitted to :
Prof. Niharika Namdev
#include <stdio.h>
if (r >= l) {
int mid = l + (r - l) / 2
if (arr[mid] == x)
return mid;
if (arr[mid] > x) {
return -1;
int i;
if(n>0)
i=n-1;
if(arr[i]==Search_ele)
temp=1;
Linear_search(arr,Search_ele,i);
return temp;
}
int main(void)
int x = 10;
if (index == -1) {
else {
if(Linear_search(arr,Search_ele,n)==1)
printf("Element found....");
else
return 0;
}
EXPERIMENT-2
PROGRAM FOR HEAP SORT
#include <stdio.h>
*a = *b;
*b = temp;
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
largest = left;
largest = right;
if (largest != i) {
swap(&arr[i], &arr[largest]);
heapify(arr, N, largest);
heapify(arr, N, i);
swap(&arr[0], &arr[i]);
heapify(arr, i, 0);
printf("\n");
int main()
heapSort(arr, N);
printArray(arr, N);
}
EXPERIMENT-3
PROGRAM FOR MERGE SORT
#include <stdio.h>
int n1 = q - p + 1;
int n2 = r - q;
int i, j, k;
i = 0;
j = 0;
k = p;
arr[k] = L[i];
i++;
} else {
arr[k] = M[j];
j++;
k++;
arr[k] = L[i];
i++;
k++;
j++;
k++;
if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
printf("\n");
int main() {
printArray(arr, size);
}
EXPERIMENT-4
PROGRAM FOR SLEECTION SORT
#include <stdio.h>
*xp = *yp;
*yp = temp;
int i, j, min_idx;
min_idx = i;
min_idx = j;
swap(&arr[min_idx], &arr[i]);
int i;
printf("\n");
int main()
printArray(arr, n);
return 0;
}
EXPERIMENT-5
PROGRAM FOR INSERTION SORT
#include <math.h>
#include <stdio.h>
int i, key, j;
key = arr[i];
j = i - 1;
arr[j + 1] = arr[j];
j = j - 1;
arr[j + 1] = key;
int i;
printf("\n");
int main()
insertionSort(arr, n);
printArray(arr, n);
return 0;
}
EXPERIMENT-6
PROGRAM FOR QUICKSORT
#include <stdio.h>
int t = *a;
*a = *b;
*b = t;
i++;
swap(&array[i], &array[j]);
return (i + 1);
quickSort(array, pi + 1, high);
}
printf("\n");
int main() {
printf("Unsorted Array\n");
printArray(data, n);
quickSort(data, 0, n - 1);
printArray(data, n);
}
EXPERIMENT-7
KNAPSACK PROBLEM USING GREEDY SOLUTION
#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;
}
EXPERIMENT-8
MINIMUM SPANNING TREE USING KRUSKAL’S ALGORITHM
#include <stdio.h>
#include <stdlib.h>
parent[i] = i;
rank[i] = 0;
if (parent[component] == component)
return component;
u = findParent(parent, u);
v = findParent(parent, v);
parent[u] = v;
else {
parent[v] = u;
rank[u]++;
int parent[n];
int rank[n];
int minCost = 0;
int wt = edge[i][2];
if (v1 != v2) {
minCost += wt;
edge[i][1], wt);
int main()
int edge[5][3] = { { 0, 1, 10 },
{ 0, 2, 6 },
{ 0, 3, 5 },
{ 1, 3, 15 },
{ 2, 3, 4 } };
kruskalAlgo(5, edge);
return 0;
}
EXPERIMENT-9
PROGRAM FOR TRAVELLING SALESMAN PROBLEM
#include <stdio.h>
int tsp(int c)
temp = matrix[c][count];
nearest_city = count;
if(minimum != 999)
return nearest_city;
int nearest_city;
visited_cities[city] = 1;
printf("%d ", city + 1);
nearest_city = tsp(city);
if(nearest_city == 999)
nearest_city = 0;
return;
minimum_cost(nearest_city);
int main()
int i, j;
scanf("%d", &limit);
scanf("%d", &matrix[i][j]);
visited_cities[i] = 0;
printf("\n");
printf("\n\nPath:\t");
minimum_cost(0);
printf("%d\n", cost);
return 0;
}
EXPERIMENT-10
PROGRAM FOR MINIMUM SPANNING TREE OF AN UNDIRECTED GRAPH USING
PRIMS ALGORITHM
#include <stdio.h>
#include <limits.h>
#define V 5
int v;
return min_index;
int i;
printf("Edge Weight\n");
int parent[V];
int mstSet[V];
key[0] = 0;
parent[0] = -1;
mstSet[u] = 1;
printMST(parent, V, graph);
int main() {
int graph[V][V] = { { 0, 2, 0, 6, 0 }, { 2, 0, 3, 8, 5 },
{ 0, 3, 0, 0, 7 }, { 6, 8, 0, 0, 9 }, { 0, 5, 7, 9, 0 }, };
primMST(graph);
return 0;
}
EXPERIENT-11
(I) PROGRAM FOR 0-1 KNAPSACK PROBLEM 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()
scanf("%d", &n);
}
printf("Enter size of knapsack:");
scanf("%d", &W);
return 0;