The document contains 10 programs related to sorting and graph algorithms. Program 1-7 implement different sorting algorithms - insertion sort, selection sort, heap sort, quick sort, counting sort, merge sort and radix sort. Program 8 implements the greedy knapsack problem. Program 9 implements the travelling salesman problem. Program 10 implements Kruskal's algorithm to find the minimum spanning tree of a graph.
These slides present various sorting algorithms: Insertion, Selection, Heap, Quick, Counting, Merge, and Radix Sort, detailing their implementation in C with functions and array manipulations.
These slides present various sorting algorithms: Insertion, Selection, Heap, Quick, Counting, Merge, and Radix Sort, detailing their implementation in C with functions and array manipulations.
These slides present various sorting algorithms: Insertion, Selection, Heap, Quick, Counting, Merge, and Radix Sort, detailing their implementation in C with functions and array manipulations.
These slides present various sorting algorithms: Insertion, Selection, Heap, Quick, Counting, Merge, and Radix Sort, detailing their implementation in C with functions and array manipulations.
These slides present various sorting algorithms: Insertion, Selection, Heap, Quick, Counting, Merge, and Radix Sort, detailing their implementation in C with functions and array manipulations.
These slides present various sorting algorithms: Insertion, Selection, Heap, Quick, Counting, Merge, and Radix Sort, detailing their implementation in C with functions and array manipulations.
These slides present various sorting algorithms: Insertion, Selection, Heap, Quick, Counting, Merge, and Radix Sort, detailing their implementation in C with functions and array manipulations.
This group covers optimization problems using greedy methods, including the Knapsack Problem, Traveling Salesman Problem, Minimum Spanning Tree via Kruskal’s Algorithm, and the N Queen Problem, all implemented in C.
This group covers optimization problems using greedy methods, including the Knapsack Problem, Traveling Salesman Problem, Minimum Spanning Tree via Kruskal’s Algorithm, and the N Queen Problem, all implemented in C.
This group covers optimization problems using greedy methods, including the Knapsack Problem, Traveling Salesman Problem, Minimum Spanning Tree via Kruskal’s Algorithm, and the N Queen Problem, all implemented in C.
This group covers optimization problems using greedy methods, including the Knapsack Problem, Traveling Salesman Problem, Minimum Spanning Tree via Kruskal’s Algorithm, and the N Queen Problem, all implemented in C.
Program 7
Writea program to perform radix sort.
#include<stdio.h>
#include<conio.h>
radix_sort(int array[], int n);
void main()
{
int array[100],n,i;
clrscr();
printf("tttRadix Sortnnnn");
printf("Enter the number of elements to be sorted: ");
scanf("%d",&n);
printf("nEnter the elements to be sorted: n");
for(i = 0 ; i < n ; i++ )
{
printf("tArray[%d] = ",i);
scanf("%d",&array[i]);
}
printf("nArray Before Radix Sort:"); //Array Before Radix Sort
for(i = 0; i < n; i++)
{
printf("%8d", array[i]);
}
printf("n");
radix_sort(array,n);
printf("nArray After Radix Sort: "); //Array After Radix Sort
for(i = 0; i < n; i++)
{
printf("%8d", array[i]);
}
printf("n");
getch();
}
radix_sort(int arr[], int n)
{
int bucket[10][5],buck[10],b[10];
int i,j,k,l,num,div,large,passes;
div=1;
num=0;
large=arr[0];
for(i=0 ; i<n ; i++)
{
if(arr[i] > large)
{
large = arr[i];
}
while(large > 0)
Program 8
Writea program to perform Knapsack Problem using Greedy Solution
#include<stdio.h>
#include<conio.h>
void knapsack(int n, float weight[], float profit[], float capacity)
{
float x[20], tp = 0;
int i, j, u;
u = capacity;
for (i = 0; i < n; i++)
x[i] = 0.0;
for (i = 0; i < n; i++)
{
if (weight[i] > u)
break;
else
{
x[i] = 1.0;
tp = tp + profit[i];
u = u - weight[i];
}
}
if (i < n)
x[i] = u / weight[i];
tp = tp + (x[i] * profit[i]);
printf("nThe result vector is:- ");
for (i = 0; i < n; i++)
printf("%ft", x[i]);
printf("nMaximum profit is:- %f", tp);
}
void main()
{
clrscr();
float weight[20], profit[20], capacity;
int num, i, j;
float ratio[20], temp;
printf("nEnter the no. of objects:- ");
scanf("%d", &num);
printf("nEnter the weights and profits of each object:- ");
for (i = 0; i < num; i++)
{
scanf("%f %f", &weight[i], &profit[i]);
}
printf("nEnter the capacity of knapsack:- ");
scanf("%f", &capacity);
for (i = 0; i < num; i++)
{
Program 11
Writea program to implement N Queen Problem using Backtracking
#include<stdio.h>
#include<conio.h>
#include<math.h>
char a[10][10];
int n;
void printmatrix()
{
int i, j;
printf("n");
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
printf("%ct", a[i][j]);
printf("nn");
}
}
int getmarkedcol(int row)
{
int i;
for (i = 0; i < n; i++)
if (a[row][i] == 'Q')
{
return (i);
break;
}
}
int feasible(int row, int col)
{
int i, tcol;
for (i = 0; i < n; i++)
{
tcol = getmarkedcol(i);
if (col == tcol || abs(row - i) == abs(col - tcol))
return 0;
}
return 1;
}
void nqueen(int row)
{
int i, j;
if (row < n)
{
for (i = 0; i < n; i++)
{
if (feasible(row, i))
29.
{
a[row][i] ='Q';
nqueen(row + 1);
a[row][i] = '.';
}
}
}
else
{
printf("nThe solution is:- ");
printmatrix();
}
}
void main()
{
clrscr();
int i, j;
printf("nEnter the no. of queens:- ");
scanf("%d", &n);
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
a[i][j] = '.';
nqueen(0);
getch();
}