DAA lab file solutions
DAA lab file solutions
#include <math.h>
#include <stdio.h>
int main() {
int arr[] = { 12, 11, 13, 5, 6 };
int N = sizeof(arr) / sizeof(arr[0]);
return 0;
}
Shell sort
#include <stdio.h>
// Shell sort
int j;
array[j] = temp;
// Print an array
printf("\n");
// Driver code
int main() {
shellSort(data, size);
printArray(data, size);
Merge sort
// Merge sort in C
#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++;
arr[k] = M[j];
j++;
k++;
// Divide the array into two subarrays, sort them and merge them
if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
// Print the array
printf("\n");
// Driver program
int main() {
printArray(arr, size);
Quick sort
// Quick sort in C
#include <stdio.h>
int t = *a;
*a = *b;
*b = t;
i++;
swap(&array[i], &array[j]);
}
}
return (i + 1);
quickSort(array, pi + 1, high);
}
// function to print array elements
printf("\n");
// main function
int main() {
printf("Unsorted Array\n");
printArray(data, n);
quickSort(data, 0, n - 1);
printArray(data, n);
Count sort
int output[10];
max = array[i];
int count[10];
count[i] = 0;
count[array[i]]++;
// Find the index of each element of the original array in count array, and
output[count[array[i]] - 1] = array[i];
count[array[i]]--;
array[i] = output[i];
printf("\n");
// Driver code
int main() {
countingSort(array, n);
printArray(array, n);
Heap sort
#include <stdio.h>
// Heapify function
maximum = i;
right_index = 2 * i + 2;
// value
maximum = left_index;
// value
maximum = right_index;
if (maximum != i) {
temp = arr[i];
arr[i] = arr[maximum];
arr[maximum] = temp;
heapify(arr, n, maximum);
// HeapSorting function
int i, temp;
// performing heapify on the non leaf nodes so n/2 - 1
heapify(arr, n, i);
temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
heapify(arr, i, 0);
// Driver code
int main()
int n = 6;
printf("\n");
heapsort(arr, n);
return 0;
#include <limits.h>
#include <stdio.h>
// for i = 1 . . . n
if (i == j)
return 0;
int k;
int count;
count = MatrixChainOrder(p, i, k)
+ MatrixChainOrder(p, k + 1, j)
min = count;
return min;
int main()
int arr[] = { 1, 2, 3, 4, 3 };
// Function call
MatrixChainOrder(arr, 1, N - 1));
getchar();
return 0;
}
LONGEST COMMON SEQUENCE
// C program to find longest common subsequence using
// recursion
#include <stdio.h>
#include <string.h>
int main()
{
// First string
char X[] = "AGGTAB";
// Second string
char Y[] = "GXTXAYB";
return 0;
}
int main() {
// Example 1
char txt1[] = "AABAACAADAABAABA";
char pat1[] = "AABA";
printf("Example 1:\n");
search(pat1, txt1);
// Example 2
char txt2[] = "agd";
char pat2[] = "g";
printf("\nExample 2:\n");
search(pat2, txt2);
return 0;
}
FLOYDD WARSHAL
#include <stdio.h>
#define nV 4
int matrix[nV][nV], i, j, k;
matrix[i][j] = graph[i][j];
printMatrix(matrix);
if (matrix[i][j] == INF)
printf("%4s", "INF");
else
printf("%4d", matrix[i][j]);
printf("\n");
int main() {
{INF, 1, 0, INF},
floydWarshall(graph);