Daa Pratical File
Daa Pratical File
Lab Manual
Design And Analysis Of Algorithm
(BCS-553)
Session 2024-2025
Code:
#include <stdio.h>
int binarySearch(int arr[], int target, int low, int high) {
if (low <= high) {
int mid = (low + high) / 2;
// If the target is found
if (arr[mid] == target)
return mid;
// If target is smaller, search in the left half
if (arr[mid] > target)
return binarySearch(arr, target, low, mid - 1)
// If target is larger, search in the right half
return binarySearch(arr, target, mid + 1, high);
}
return -1; // Element not found
NAME-:Harsh Upadhyay ROLL NO.-:2203611650020 BRANCH-:CSD YEAR-:3rd
}
int main() {
int arr[] = {2, 3, 4, 10, 40};
int n = sizeof(arr) / sizeof(arr[0]);
int target = 10;
int result = binarySearch(arr, target, 0, n - 1);
if (result != -1)
printf("Element found at index %d\n", result);
else
printf("Element not found\n");
return 0;
}
Time Complexity:
- Best Case:O(1)
- Worst Case: O(log n)
-------------------------------------------------------------------------------------------------------------------------------------------------
Code:
NAME-:Harsh Upadhyay ROLL NO.-:2203611650020 BRANCH-:CSD YEAR-:3rd
#include <stdio.h>
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) {
int temp = arr[i];
arr[i] = arr[largest];
arr[largest] = temp;
heapify(arr, n, largest);
}
}
int main() {
Time Complexity:
- Best Case: O(n log n)
- Worst Case: O(n log n)
------------------------------------------------------------------------------------------------------------------------------------------------
Code:
#include <stdio.h>
void merge(int arr[], int l, int m, int r) {
int n1 = m - l + 1;
int n2 = r - m;
int L[n1], R[n2];
NAME-:Harsh Upadhyay ROLL NO.-:2203611650020 BRANCH-:CSD YEAR-:3rd
for (int i = 0; i< n1; i++)
L[i] = arr[l + i];
for (int j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];
int i = 0, j = 0, k = l;
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++;
}
}
int main() {
int arr[] = {12, 11, 13, 5, 6, 7};
int n = sizeof(arr) / sizeof(arr[0]);
mergeSort(arr, 0, n - 1);
printf("Sorted array is: ");
for (int i = 0; i< n; i++)
printf("%d ", arr[i]);
printf("\n");
return 0;
}
Time Complexity:
- Best Case: O(n log n)
- Worst Case: O(n log n)
-------------------------------------------------------------------------------------------------------------------------------------------------
Code:
#include <stdio.h>
void selectionSort(int arr[], int n) {
for (int i = 0; i< n - 1; i++) {
int minIdx = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] <arr[minIdx])
minIdx = j;
}
int temp = arr[minIdx];
arr[minIdx] = arr[i];
arr[i] = temp;
}
}
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);
selectionSort(arr, n);
printf("Sorted array is: ");
for (int i = 0; i< n; i++)
printf("%d ", arr[i]);
printf("\n");
return 0;
}
Time Complexity:
--------------------------------------------------------------------------------------------------------------------------------------------------
Algorithm:
1. Start with the second element. Compare it with the elements before it.
2. Shift all larger elements to the right and insert the current element in its correct position.
3. Repeat for all elements in the array.
Code:
#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--;
}
arr[j + 1] = key;
}
}
int main() {
int arr[] = {12, 11, 13, 5, 6};
NAME-:Harsh Upadhyay ROLL NO.-:2203611650020 BRANCH-:CSD YEAR-:3rd
int n = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, n);
printf("Sorted array is: ");
for (int i = 0; i< n; i++)
printf("%d ", arr[i]);
printf("\n");
return 0;
}
Time Complexity:
-Best Case: O(n)
- Worst Case: O(n^2)
--------------------------------------------------------------------------------------------------------------------------------------------------
Code:
#include <stdio.h>
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = low - 1;
for (int j = low; j < high; j++) {
NAME-:Harsh Upadhyay ROLL NO.-:2203611650020 BRANCH-:CSD YEAR-:3rd
if (arr[j] <= pivot) {
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
return i + 1;
}
int main() {
int arr[] = {10, 7, 8, 9, 1, 5};
int n = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, n - 1);
printf("Sorted array is: ");
for (int i = 0; i< n; i++)
printf("%d ", arr[i]);
printf("\n");
Time Complexity:
- Best Case: O(n log n)
- Worst Case: O(n^2)
--------------------------------------------------------------------------------------------------------------------------------------------------
Code:
#include <stdio.h>
struct Item {
int value;
int weight;
};
int main() {
struct Item items[] = {{60, 10}, {100, 20}, {120, 30}};
int n = sizeof(items) / sizeof(items[0]);
int capacity = 50;
printf("Maximum value we can obtain = %.2f\n", knapsack(items, n, capacity));
return 0;
}
Time Complexity:
--------------------------------------------------------------------------------------------------------------------------------------------------
Code:
#include <stdio.h>
#include <limits.h>
#define V 4
int min(int a, int b) {
return (a < b) ? a : b;
}
int tsp(int dist[][V], int visited[], int currPos, int count, int cost, int ans) {
if (count == V) {
if (dist[currPos][0] != 0)
return min(ans, cost + dist[currPos][0]);
return ans;
}
for (int i = 0; i< V; i++) {
if (!visited[i] &&dist[currPos][i] != 0) {
int main() {
int dist[V][V] = {{0, 10, 15, 20},
{10, 0, 35, 25},
{15, 35, 0, 30},
{20, 25, 30, 0}};
int visited[V] = {0};
visited[0] = 1;
int ans = INT_MAX;
printf("Minimum cost of traveling: %d\n", tsp(dist, visited, 0, 1, 0, ans));
return 0;
}
Time Complexity:
- Best Case: O(n!)
- Worst Case: O(n!)
--------------------------------------------------------------------------------------------------------------------------------------------------
Code:
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
struct Edge {
int u, v, weight;
};
struct Subset {
int parent;
int rank;
};
int main() {
int V = 4, E = 5;
struct Edge edges[] = {{0, 1, 10}, {0, 2, 6}, {0, 3, 5}, {1, 3, 15}, {2, 3, 4}};
kruskal(edges, V, E);
return 0;
}
Time Complexity:
- Best Case: O(E log E)
- Worst Case: O(E log E)
--------------------------------------------------------------------------------------------------------------------------------------------------
Code:
#include <stdio.h>
#define N 4
int main() {
if (solveNQueens(0) == 0) {
printf("Solution does not exist");
return 0;
}
printSolution();
return 0;
}
Time Complexity:
- Worst Case: O(N!)
--------------------------------------------------------------------------------------------------------------------------------------------------
Code:
#include <stdio.h>
#include <stdlib.h>
NAME-:Harsh Upadhyay ROLL NO.-:2203611650020 BRANCH-:CSD YEAR-:3rd
#include <time.h>
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pivot = arr[high];
int i = (low - 1);
for (int j = low; j < high; j++) {
if (arr[j] < pivot) {
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
int pi = i + 1;
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
int main() {
int n = 5000; // Can be varied
int arr[n];
for (int i = 0; i< n; i++) {
arr[i] = rand() % 10000; // Generating random numbers
}
clock_t start = clock();
Time Complexity:
- Best Case: O(n log n)
- Worst Case: O(n²)