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

Daa Experiments

The document discusses different sorting and optimization algorithms like insertion sort, merge sort, quicksort, 0/1 knapsack problem, fractional knapsack problem and Floyd-Warshall algorithm for finding all pair shortest paths. Code implementations and explanations are provided for each algorithm.

Uploaded by

wot
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 views9 pages

Daa Experiments

The document discusses different sorting and optimization algorithms like insertion sort, merge sort, quicksort, 0/1 knapsack problem, fractional knapsack problem and Floyd-Warshall algorithm for finding all pair shortest paths. Code implementations and explanations are provided for each algorithm.

Uploaded by

wot
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/ 9

DAA EXPERIMENTS

Insertion sort

#include <stdio.h>

int main() {
int key,length,i,j;
int arr[100];
printf("enter length of array:");
scanf("%d",&length);
printf("enter elements of array:\n");
for(i=0;i<length;i++){
scanf("%d",&arr[i]);
}
for(j=0;j<length;j++){
for(i=j;i>0;i--){
if(arr[i]<arr[i-1]){
key=arr[i];
arr[i]=arr[i-1];
arr[i-1]=key;
}
}
}
printf("after insertion sort\n");
for(i=0;i<length;i++){
printf("%d\n",arr[i]);
}

return 0;
}
Merge sort

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
void merge(int arr[], int l, int m, int r) {
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
int *L = (int *)malloc(n1 * sizeof(int));
int *R = (int *)malloc(n2 * sizeof(int));

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


L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];

i = 0; // Initial index of first subarray


j = 0; // Initial index of second subarray
k = l; // Initial index of merged subarray
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}

while (i < n1) {


arr[k] = L[i];
i++;
k++;
}

while (j < n2) {


arr[k] = R[j];
j++;
k++;
}

// Free dynamically allocated memory


free(L);
free(R);
}

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


if (l < r) {
int m = l + (r - l) / 2; // Same as (l+r)/2, but avoids overflow for large l and
r
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}

void printArray(int arr[], int size) {


int i;
for (i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}

int main() {
int arr[] = {5, 8, 2, 1, 4, 6, 7, 9};
int arr_size = sizeof(arr) / sizeof(arr[0]);

printf("Before sorting:\n");
printArray(arr, arr_size);

mergeSort(arr, 0, arr_size - 1);

printf("After sorting:\n");
printArray(arr, arr_size);

getch();
return 0;
}
Kth smallest:

#include <stdio.h>
#include<conio.h>
#include<time.h>
void swap(int *a, int *b) {
int t = *a;
*a = *b;
*b = t;
}
int partition(int array[], int low, int high) {
int j;
int pivot = array[high];
int i = (low - 1);
for (j = low; j < high; j++) {
if (array[j] <= pivot) {
i++;
swap(&array[i], &array[j]);
}

}
swap(&array[i + 1], &array[high]);
return (i + 1);
}
void quickSort(int array[], int low, int high) {
if (low < high) {
int pi = partition(array, low, high);
quickSort(array, low, pi - 1);
quickSort(array, pi + 1, high);
}
}
void kth_element(int array[],int k) {
printf("%d",array[k]);
}
void main() {
int k;
int data[] = {8, 7, 2, 1, 0, 9, 6};
int n = sizeof(data) / sizeof(data[0]);
quickSort(data, 0, n - 1);
printf("Enter the index: ");
scanf("%d",&k);
kth_element(data,k);
getch();
clrscr();
}
0/1 knapsack:

#include<stdio.h>
#include<conio.h>
int max(int a, int b){
if (a>b)
return a;
else
return b;
}
int knapsack(int W,int wt[],int val[], int n){
if(n==0||W==0)
return 0;
if(wt[n-1]>W)
return knapsack(W,wt,val,n-1);
else
return max(val[n-1]+knapsack(W-wt[n-1],wt,val,n-1), knapsack(W,wt,val,n-1));
}
int main(){
int wt[200],val[200],i,W,n;
printf("\nEnter the no of items: ");
scanf("%d",&n);
printf("\nEnter the total weight of the knapsack: ");
scanf("%d",&W);
for(i=0;i<n;i++)
{
printf("\nEnter the %dth item weight: ",i+1);
scanf("%d",&wt[i]);
}
for(i=0;i<n;i++)
{
printf("\nEnter the %dth item value: ",i+1);
scanf("%d",&val[i]);
}
printf("\nMaximum profit for the given values and weights is:
%d\n",knapsack(W,wt,val,n));
getch();
return 0;
}
Fractional Knapsack :
#include<stdio.h>
#include<conio.h>
void fknapsack(int n,float wt[],float pf[],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(wt[i] > u){
break;
}
else{
x[i]=1.0;
tp=tp+pf[i];
u=u-wt[i];
}
}

if(i<n){
x[i]=u/wt[i];
}
tp=tp+(x[i] * pf[i]);
printf("The result vector is:");
for(i=0;i<n;i++){
printf("%f\t",x[i]);
}
printf("Maximum Profit is: %f",tp);
}

void main(){
float capacity,weight[20],profit[20];
int n,i,j;
float ratio[20],temp;
printf("Enter no of Objects: ");
scanf("%d",&n);

printf("Enter capacity of knapsack: ");


scanf("%f",&capacity);

printf("Enter wts and profits of items:\n");


for(i=0;i<n;i++){
scanf("%f%f",&weight[i],&profit[i]);
}

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;
}
}
}
fknapsack(n,weight,profit,capacity);
getch();
clrscr();
}
All Pair shortest path (floyd warshall):

#include <stdio.h>
#define V 4
#define INF 999999

void floydWarshall(int dist[][V])


{
int i, j, k;
for (k = 0; k < V; k++) {
for (i = 0; i < V; i++) {
for (j = 0; j < V; j++) {
if (dist[i][k] + dist[k][j] < dist[i][j]){
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
}
printSolution(dist);
}

void printSolution(int dist[][V])


{ int i,j;
printf(
"The following matrix shows the shortest distances"
" between every pair of vertices \n");
for ( i = 0; i < V; i++) {
for ( j = 0; j < V; j++) {
if (dist[i][j] ==INF){
printf("%s\t", "INF");
}
else{
printf("%d\t", dist[i][j]);
}
}
printf("\n");
}
}
int main()
{
int graph[V][V] = { { 0, 3, INF, 7 },
{ 8, 0, 2, INF },
{ 5, INF, 0, 1 },
{ 2, INF, INF, 0 } };

// Function call
clrscr();
floydWarshall(graph);

getch();
return 0;
}

You might also like