Daa Lab Form 1 - 10
Daa Lab Form 1 - 10
Daa Lab Form 1 - 10
if (arr[mid] == target) {
return mid; // Element found at index mid
} else if (arr[mid] > target) {
return binarySearch(arr, low, mid - 1, target); // Search in the left
half
} else {
return binarySearch(arr, mid + 1, high, target); // Search in the
right half
}
}
int main() {
int n;
printf("Enter the number of elements in the sorted array: ");
scanf("%d", &n);
int arr[n];
int target;
if (result != -1) {
printf("Element %d found at index %d.\n", target, result);
} else {
printf("Element %d not found in the array.\n", target);
}
return 0;
}
ITERATIVE BINARY SEARCH CODE
#include <stdio.h>
if (arr[mid] == target) {
return mid; // Element found at index mid
} else if (arr[mid] > target) {
high = mid - 1; // Search in the left half
} else {
low = mid + 1; // Search in the right half
}
}
int main() {
int n;
printf("Enter the number of elements in the sorted array: ");
scanf("%d", &n);
int arr[n];
int target;
if (result != -1) {
printf("Element %d found at index %d.\n", target, result);
} else {
printf("Element %d not found in the array.\n", target);
}
return 0;
}
LAB 2 SORTING
INSERTION SORT
#include <stdio.h>
int main() {
int n;
int arr[n];
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
insertionSort(arr, n);
printf("Sorted array:\n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
SELECTION SORT :
#include <stdio.h>
if (minIndex != i) {
// Swap arr[i] and arr[minIndex]
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
}
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
selectionSort(arr, n);
printf("Sorted array:\n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
BUBBLE SORT :
#include <stdio.h>
int main() {
int n;
int arr[n];
bubbleSort(arr, n);
printf("Sorted array:\n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
LAB 3 -- MERGE SORT
#include <stdio.h>
int main() {
int n;
int arr[n];
mergeSort(arr, 0, n - 1);
printf("Sorted array:\n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
LAB 4 -- QUICK SORT :
#include <stdio.h>
int main() {
int n;
int arr[n];
quickSort(arr, 0, n - 1);
printf("Sorted array:\n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
LAB 5 -- HEAP SORT
#include <stdio.h>
if (largest != i) {
// Swap arr[i] and arr[largest]
int temp = arr[i];
arr[i] = arr[largest];
arr[largest] = temp;
int main() {
int n;
int arr[n];
heapSort(arr, n);
printf("Sorted array:\n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
LAB 6
Dijkstra algorithm
#include <stdio.h>
#include <limits.h>
return min_index;
}
dist[src] = 0;
int main() {
int vertices, src;
printf("Enter the number of vertices: ");
scanf("%d", &vertices);
int graph[V][V];
printf("Enter the adjacency matrix for the graph (0 for no
edge):\n");
for (int i = 0; i < vertices; i++) {
for (int j = 0; j < vertices; j++) {
scanf("%d", &graph[i][j]);
}
}
return 0;
}
LAB 7
KNAPSACK PROBLEM
#include <stdio.h>
return dp[n][W];
}
int main() {
int n, W;
return graph;
}
// Function to add an edge to the graph
void addEdge(struct Graph* graph, int start, int end) {
graph->adjMatrix[start][end] = 1;
graph->adjMatrix[end][start] = 1;
}
bool visited[MAX_VERTICES];
for (int i = 0; i < MAX_VERTICES; i++) {
visited[i] = false;
}
visited[startVertex] = true;
printf("%d ", startVertex);
enqueue(&q, startVertex);
while (!isEmpty(&q)) {
int currentVertex = dequeue(&q);
for (int i = 0; i < graph->vertices; i++) {
if (graph->adjMatrix[currentVertex][i] == 1 && !visited[i]) {
printf("%d ", i);
visited[i] = true;
enqueue(&q, i);
}
}
}
printf("\n");
}
int main() {
int vertices, edges;
printf("Enter the number of vertices: ");
scanf("%d", &vertices);
printf("Enter the number of edges: ");
scanf("%d", &edges);
int startVertex;
printf("Enter the starting vertex for BFS: ");
scanf("%d", &startVertex);
BFS(graph, startVertex);
return 0;
}
LAB 9 –
LCS (LONGEST COMMON SUBSEQUENCE) USING RECURSION
#include <stdio.h>
#include <string.h>
int main() {
char X[100], Y[100];
int m = strlen(X);
int n = strlen(Y);
return 0;
}
LAB 9 –
LCS (LONGEST COMMON SUBSEQUENCE) ITERATIVE
#include <stdio.h>
#include <string.h>
return dp[m][n];
}
int main() {
char X[100], Y[100];
int m = strlen(X);
int n = strlen(Y);
return 0;
}
LAB 10 –
BINOM IAL COEFFICIENT
#include <stdio.h>
return C[n][k];
}
int main() {
int n, k;
if (k < 0 || k > n) {
printf("Invalid input: 'k' should be between 0 and 'n'.\n");
} else {
unsigned long long result = binomialCoefficient(n, k);
printf("The binomial coefficient C(%d, %d) is %llu\n", n, k,
result);
}
return 0;
}