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

School of Computing Gehu, Bhimtal: Lab File

The document is a lab file for Mahi Pandey, a BCA student at Graphic Era Hill University, certifying the completion of the Design and Analysis of Algorithms Laboratory. It includes acknowledgments to faculty and a detailed index of algorithms implemented in C programming, such as Linear Search, Binary Search, GCD, Tower of Hanoi, Merge Sort, and Quick Sort. Each algorithm is accompanied by its code and output results.

Uploaded by

mahip7249
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 views24 pages

School of Computing Gehu, Bhimtal: Lab File

The document is a lab file for Mahi Pandey, a BCA student at Graphic Era Hill University, certifying the completion of the Design and Analysis of Algorithms Laboratory. It includes acknowledgments to faculty and a detailed index of algorithms implemented in C programming, such as Linear Search, Binary Search, GCD, Tower of Hanoi, Merge Sort, and Quick Sort. Each algorithm is accompanied by its code and output results.

Uploaded by

mahip7249
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/ 24

School of Computing

GEHU, BHIMTAL

Lab File

Name - Mahi Pandey


Univ. Roll No - 2371210
Course - BCA
Sem - IV
Subject :-
Design and Analysis of Algorithms Laboratory
Subject code: PBC 401
Session: 2024-25
CERTIFICATE

This is to certify that Mahi Pandey has satisfactorily completed all the
experiment in the laboratory of this college. The Term Work of Design
and Analysis of Algorithms Laboratory. In partial fulfilment of the
requirement in 4th semester of Bachelor of Computer Applications
degree course prescribed by Graphic Era Hill University, Bhimtal.

Ms. Vaishali Dev Dr. Sandeep K Budhani

(Faculty-in-Charge) (HOD, SOC Dept.)


ACKNOWLEDGEMENT

I express my gratitude to teachers, for coming up with this interesting


and thought- provoking course, compiling the best contents and the
most important working towards changing the words, making the
differences and inspiring us to become the climate saver ourselves,
knowledge that I gained will last a lifetime.

Many thanks to my institute, Graphic Era Hill University, Bhimtal and a


special thanks to our Faculty Ms. Vaishali Dev for motivating us to look
beyond the syllabus, to take the opportunity and to learn as much as
we can. I also thank Mam for helping us in this Term-work.

Student Name – Mahi Pandey


University Roll NO. - 2371210
Index

S.NO. TITLE PAGE NO. REMARK


Algorithm 1:
Program 1

Program Title: Write an algorithm and a program to implement the


Linear Search algorithms.

#include <stdio.h>

int linear_search(int arr[], int target, int index, int size) {


if (index >= size) {
return -1;
}
if (arr[index] == target) {
return index;
}
return linear_search(arr, target, index + 1, size);
}

int main() {
int arr[] = {5, 3, 7, 9, 2, 4};
int target = 7;
int size = sizeof(arr) / sizeof(arr[0]);
int result = linear_search(arr, target, 0, size);

if (result != -1) {
printf("Element found at index %d\n", result);
} else {
printf("Element not found\n");
}

return 0;
Output:
Algorithm 2:
Program 2

Program Title: Write an algorithm and a program to implement the


Binary Search algorithms.

#include <stdio.h>

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

if (low > high) {

return -1;

int mid = low + (high - low) / 2;

if (arr[mid] == target) {

return mid;

} else if (arr[mid] > target) {

return binary_search(arr, target, low, mid - 1);

} else {

return binary_search(arr, target, mid + 1, high);

}
int main() {

int arr[] = {2, 3, 4, 5, 7, 9};

int target = 7;

int size = sizeof(arr) / sizeof(arr[0]);

int result = binary_search(arr, target, 0, size - 1);

if (result != -1) {

printf("Element found at index %d\n", result);

} else {

printf("Element not found\n");

return 0;

}
Output:
Algorithm 3:
Program 3

Program Title: Write an algorithm and a program to implement GCD


using recursion .

#include <stdio.h>

int gcd(int a, int b) {


if (b == 0) {
return a;
}
return gcd(b, a % b);
}

int main() {
int a = 56, b = 98;
printf("GCD of %d and %d is %d\n", a, b, gcd(a, b));

return 0;
}
Output:
Algorithm 4:
Program 4

Program Title: Write an algorithm and a program to implement Tower


Of Hanoi using recursion .

#include <stdio.h>

void towerOfHanoi(int n, char source, char auxiliary, char target) {


if (n == 1) {
printf("Move disk 1 from %c to %c\n", source, target);
return;
}
towerOfHanoi(n - 1, source, target, auxiliary);
printf("Move disk %d from %c to %c\n", n, source, target);

towerOfHanoi(n - 1, auxiliary, source, target);


}

int main() {
int n = 3;
printf("Tower of Hanoi solution for %d disks:\n", n);
towerOfHanoi(n, 'A', 'B', 'C');

return 0;
}

Output:
Algorithm 5:
Program 5

Program Title: Write an algorithm and a program to implement Merge


Sort algorithm .

#include <stdio.h>

void merge(int arr[], int left, int mid, int right) {


int n1 = mid - left + 1;
int n2 = right - mid;
int L[n1], R[n2];

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


L[i] = arr[left + i];
for (int j = 0; j < n2; j++)
R[j] = arr[mid + 1 + j];

int i = 0, j = 0, k = left;
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++;
}
}

void merge_sort(int arr[], int left, int right) {


if (left < right) {
int mid = left + (right - left) / 2;
merge_sort(arr, left, mid);
merge_sort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
}

int main() {
int arr[] = {12, 11, 13, 5, 6, 7};
int size = sizeof(arr) / sizeof(arr[0]);
merge_sort(arr, 0, size - 1);
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Output:
Algorithm 6:
Program 6

Program Title: Write an algorithm and a program to implement Quick


Sort algorithm .

#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++)
{
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;
}

void quick_sort(int arr[], int low, int high)


{
if (low < high) {
int pi = partition(arr, low, high);
quick_sort(arr, low, pi - 1);
quick_sort(arr, pi + 1, high);
}
}

int main() {
int arr[] = {10, 7, 8, 9, 1, 5};
int size = sizeof(arr) / sizeof(arr[0]);

quick_sort(arr, 0, size - 1);

for (int i = 0; i < size; i++)


{
printf("%d ", arr[i]);
}
return 0;

Output :

You might also like