Daa Lab Form 1 - 10

Download as pdf or txt
Download as pdf or txt
You are on page 1of 38

LAB -1 :

RECURSIVE BINARY SEARCH


#include <stdio.h>

int binarySearch(int arr[], int low, int high, int target) {


if (low > high) {
return -1; // Element not found
}

int mid = (low + high) / 2;

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];

printf("Enter %d elements in sorted order:\n", n);


for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

int target;

printf("Enter the element you want to search for: ");


scanf("%d", &target);

int result = binarySearch(arr, 0, n - 1, 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>

int binarySearch(int arr[], int n, int target) {


int low = 0;
int high = n - 1;

while (low <= high) {


int mid = (low + high) / 2;

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
}
}

return -1; // Element not found


}

int main() {
int n;
printf("Enter the number of elements in the sorted array: ");
scanf("%d", &n);

int arr[n];

printf("Enter %d elements in sorted order:\n", n);


for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

int target;

printf("Enter the element you want to search for: ");


scanf("%d", &target);

int result = binarySearch(arr, n, 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>

void insertionSort(int arr[], int n) {


for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;

while (j >= 0 && arr[j] > key) {


arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}

int main() {
int n;

printf("Enter the number of elements: ");


scanf("%d", &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>

void selectionSort(int arr[], int n) {


for (int i = 0; i < n - 1; i++) {
int minIndex = i;

for (int j = i + 1; j < n; j++) {


if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}

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];

printf("Enter %d elements:\n", n);


for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

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>

void bubbleSort(int arr[], int n) {


for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap arr[j] and arr[j + 1]
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

int main() {
int n;

printf("Enter the number of elements: ");


scanf("%d", &n);

int arr[n];

printf("Enter %d elements:\n", n);


for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

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>

// Merge two subarrays arr[low..mid] and arr[mid+1..high]


void merge(int arr[], int low, int mid, int high) {
int n1 = mid - low + 1;
int n2 = high - mid;

// Create temporary arrays


int left[n1], right[n2];

// Copy data to temporary arrays left[] and right[]


for (int i = 0; i < n1; i++) {
left[i] = arr[low + i];
}
for (int i = 0; i < n2; i++) {
right[i] = arr[mid + 1 + i];
}

// Merge the temporary arrays back into arr[low..high]


int i = 0, j = 0, k = low;
while (i < n1 && j < n2) {
if (left[i] <= right[j]) {
arr[k] = left[i];
i++;
} else {
arr[k] = right[j];
j++;
}
k++;
}

// Copy the remaining elements of left[], if any


while (i < n1) {
arr[k] = left[i];
i++;
k++;
}

// Copy the remaining elements of right[], if any


while (j < n2) {
arr[k] = right[j];
j++;
k++;
}
}

// Main function to perform merge sort


void mergeSort(int arr[], int low, int high) {
if (low < high) {
int mid = low + (high - low) / 2; // Calculate the middle of the
array
// Sort first and second halves
mergeSort(arr, low, mid);
mergeSort(arr, mid + 1, high);

// Merge the sorted halves


merge(arr, low, mid, high);
}
}

int main() {
int n;

printf("Enter the number of elements: ");


scanf("%d", &n);

int arr[n];

printf("Enter %d elements:\n", n);


for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

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>

// Function to swap two elements in an array


void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}

// Partition function for the quick sort algorithm


int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = (low - 1);

for (int j = low; j < high; j++) {


if (arr[j] < pivot) {
i++;
swap(&arr[i], &arr[j]);
}
}

swap(&arr[i + 1], &arr[high]);


return (i + 1);
}
// Main quick sort function
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pivotIndex = partition(arr, low, high);

quickSort(arr, low, pivotIndex - 1);


quickSort(arr, pivotIndex + 1, high);
}
}

int main() {
int n;

printf("Enter the number of elements: ");


scanf("%d", &n);

int arr[n];

printf("Enter %d elements:\n", n);


for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

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>

// Function to heapify a subtree rooted with node i, where n is the size


of the heap
void heapify(int arr[], int n, int i) {
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;

if (left < n && arr[left] > arr[largest]) {


largest = left;
}

if (right < n && arr[right] > arr[largest]) {


largest = right;
}

if (largest != i) {
// Swap arr[i] and arr[largest]
int temp = arr[i];
arr[i] = arr[largest];
arr[largest] = temp;

// Recursively heapify the affected sub-tree


heapify(arr, n, largest);
}
}

// Main heap sort function


void heapSort(int arr[], int n) {
// Build a max heap
for (int i = n / 2 - 1; i >= 0; i--) {
heapify(arr, n, i);
}

// Extract elements from the heap one by one


for (int i = n - 1; i > 0; i--) {
// Move the current root to the end
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;

// Call max heapify on the reduced heap


heapify(arr, i, 0);
}
}

int main() {
int n;

printf("Enter the number of elements: ");


scanf("%d", &n);

int arr[n];

printf("Enter %d elements:\n", n);


for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

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>

#define V 100 // Maximum number of vertices

// Function to find the vertex with the minimum distance value


int minDistance(int dist[], int sptSet[], int vertices) {
int min = INT_MAX, min_index;

for (int v = 0; v < vertices; v++) {


if (!sptSet[v] && dist[v] <= min) {
min = dist[v];
min_index = v;
}
}

return min_index;
}

// Function to print the constructed distance array


void printSolution(int dist[], int vertices) {
printf("Vertex Distance from Source\n");
for (int i = 0; i < vertices; i++) {
printf("%d \t\t %d\n", i, dist[i]);
}
}

// Function to implement Dijkstra's algorithm for a given graph


void dijkstra(int graph[V][V], int src, int vertices) {
int dist[V];
int sptSet[V];

for (int i = 0; i < vertices; i++) {


dist[i] = INT_MAX;
sptSet[i] = 0;
}

dist[src] = 0;

for (int count = 0; count < vertices - 1; count++) {


int u = minDistance(dist, sptSet, vertices);
sptSet[u] = 1;

for (int v = 0; v < vertices; v++) {


if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX &&
dist[u] + graph[u][v] < dist[v]) {
dist[v] = dist[u] + graph[u][v];
}
}
}
printSolution(dist, vertices);
}

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]);
}
}

printf("Enter the source vertex: ");


scanf("%d", &src);

dijkstra(graph, src, vertices);

return 0;
}
LAB 7
KNAPSACK PROBLEM
#include <stdio.h>

// Function to find the maximum of two integers


int max(int a, int b) {
return (a > b) ? a : b;
}

// Function to solve the 0/1 Knapsack problem


int knapsack(int W, int weights[], int values[], int n) {
int dp[n + 1][W + 1];

for (int i = 0; i <= n; i++) {


for (int w = 0; w <= W; w++) {
if (i == 0 || w == 0) {
dp[i][w] = 0;
} else if (weights[i - 1] <= w) {
dp[i][w] = max(values[i - 1] + dp[i - 1][w - weights[i - 1]],
dp[i - 1][w]);
} else {
dp[i][w] = dp[i - 1][w];
}
}
}

return dp[n][W];
}

int main() {
int n, W;

printf("Enter the number of items: ");


scanf("%d", &n);

int weights[n], values[n];

printf("Enter the weights of the items:\n");


for (int i = 0; i < n; i++) {
scanf("%d", &weights[i]);
}

printf("Enter the values of the items:\n");


for (int i = 0; i < n; i++) {
scanf("%d", &values[i]);
}

printf("Enter the knapsack's capacity: ");


scanf("%d", &W);

int result = knapsack(W, weights, values, n);

printf("The maximum value that can be obtained is: %d\n", result);


return 0;
}
LAB 8 -- BFS (Breadth-First Search)
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#define MAX_VERTICES 100

// Structure for a queue


struct Queue {
int items[MAX_VERTICES];
int front;
int rear;
};

// Function to initialize a queue


void initializeQueue(struct Queue* q) {
q->front = -1;
q->rear = -1;
}

// Function to check if the queue is empty


bool isEmpty(struct Queue* q) {
return q->rear == -1;
}

// Function to add an item to the queue


void enqueue(struct Queue* q, int item) {
if (q->rear == MAX_VERTICES - 1) {
printf("Queue is full\n");
} else {
if (q->front == -1) {
q->front = 0;
}
q->rear++;
q->items[q->rear] = item;
}
}

// Function to remove an item from the queue


int dequeue(struct Queue* q) {
int item;
if (isEmpty(q)) {
printf("Queue is empty\n");
item = -1;
} else {
item = q->items[q->front];
q->front++;
if (q->front > q->rear) {
q->front = q->rear = -1;
}
}
return item;
}

// Structure for a graph


struct Graph {
int vertices;
int** adjMatrix;
};

// Function to create a graph with the given number of vertices


struct Graph* createGraph(int vertices) {
struct Graph* graph = (struct Graph*)malloc(sizeof(struct Graph));
graph->vertices = vertices;

graph->adjMatrix = (int**)malloc(vertices * sizeof(int*));


for (int i = 0; i < vertices; i++) {
graph->adjMatrix[i] = (int*)malloc(vertices * sizeof(int));
}

for (int i = 0; i < vertices; i++) {


for (int j = 0; j < vertices; j++) {
graph->adjMatrix[i][j] = 0;
}
}

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;
}

// Function to perform Breadth-First Search (BFS) on the graph


void BFS(struct Graph* graph, int startVertex) {
struct Queue q;
initializeQueue(&q);

bool visited[MAX_VERTICES];
for (int i = 0; i < MAX_VERTICES; i++) {
visited[i] = false;
}

printf("Breadth-First Search starting from vertex %d: ",


startVertex);

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);

struct Graph* graph = createGraph(vertices);

for (int i = 0; i < edges; i++) {


int start, end;
printf("Enter edge %d (format: start end): ", i + 1);
scanf("%d %d", &start, &end);
addEdge(graph, start, end);
}

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 max(int a, int b) {


return (a > b) ? a : b;
}

int lcs(char* X, char* Y, int m, int n) {


if (m == 0 || n == 0) {
return 0;
}

if (X[m - 1] == Y[n - 1]) {


return 1 + lcs(X, Y, m - 1, n - 1);
} else {
return max(lcs(X, Y, m, n - 1), lcs(X, Y, m - 1, n));
}
}

int main() {
char X[100], Y[100];

printf("Enter the first string: ");


scanf("%s", X);
printf("Enter the second string: ");
scanf("%s", Y);

int m = strlen(X);
int n = strlen(Y);

int result = lcs(X, Y, m, n);

printf("Length of Longest Common Subsequence: %d\n", result);

return 0;
}
LAB 9 –
LCS (LONGEST COMMON SUBSEQUENCE) ITERATIVE
#include <stdio.h>
#include <string.h>

int max(int a, int b) {


return (a > b) ? a : b;
}

int lcs(char* X, char* Y, int m, int n) {


int dp[m + 1][n + 1];

for (int i = 0; i <= m; i++) {


for (int j = 0; j <= n; j++) {
if (i == 0 || j == 0) {
dp[i][j] = 0;
} else if (X[i - 1] == Y[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
}
}
}

return dp[m][n];
}

int main() {
char X[100], Y[100];

printf("Enter the first string: ");


scanf("%s", X);
printf("Enter the second string: ");
scanf("%s", Y);

int m = strlen(X);
int n = strlen(Y);

int result = lcs(X, Y, m, n);

printf("Length of Longest Common Subsequence: %d\n", result);

return 0;
}
LAB 10 –
BINOM IAL COEFFICIENT

#include <stdio.h>

// Function to calculate the binomial coefficient using dynamic


programming
unsigned long long binomialCoefficient(int n, int k) {
unsigned long long C[n + 1][k + 1];
int i, j;

for (i = 0; i <= n; i++) {


for (j = 0; j <= k && j <= i; j++) {
if (j == 0 || j == i)
C[i][j] = 1;
else
C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
}
}

return C[n][k];
}

int main() {
int n, k;

printf("Enter the value of 'n': ");


scanf("%d", &n);

printf("Enter the value of 'k': ");


scanf("%d", &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;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy