0% found this document useful (0 votes)
16 views9 pages

LAB Report

Uploaded by

me homer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views9 pages

LAB Report

Uploaded by

me homer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Lab Report 1: Implement Tower of Hanoi using Recursive Function

1. Problem Statement
The Tower of Hanoi is a mathematical puzzle where we have three rods and n disks of different sizes.
The objective is to move all disks from one rod to another, adhering to the following rules:
1. Only one disk can be moved at a time.
2. Each move involves taking the top disk from one of the stacks and placing it on another rod.
3. A larger disk may not be placed on top of a smaller disk.
2. Problem Analysis
This problem is a classic example of recursion. The base case is when there's only one disk, and we can
directly move it. For more than one disk, we break the problem into smaller sub-problems by moving
n-1 disks to an auxiliary rod, then moving the largest disk to the target rod, and finally moving the n-
1 disks from the auxiliary rod to the target rod.

3. Algorithm Overview
Tower of Hanoi Process:
1. Move n-1 disks from source rod to auxiliary rod.
2. Move the nth disk (largest) from the source rod to the destination rod.
3. Move n-1 disks from auxiliary rod to destination rod.

Time Complexity: The recursive nature of the algorithm results in an exponential time complexity of
O(2^n).
Space Complexity: Since it uses recursion, the space complexity is O(n) due to the call stack.
4. Source Code
#include<iostream>
using namespace std;

void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod) {


if (n == 1) {
cout << "Move disk 1 from rod " << from_rod << " to rod " << to_rod <<
endl;
return;
}
towerOfHanoi(n - 1, from_rod, aux_rod, to_rod);
cout << "Move disk " << n << " from rod " << from_rod << " to rod " << to_rod
<< endl;
towerOfHanoi(n - 1, aux_rod, to_rod, from_rod);
}

int main() {
int n;
cout << "Enter the number of disks: ";
cin >> n;
towerOfHanoi(n, 'A', 'C', 'B');
return 0;
}
5. Sample Input / Output
Input:
Enter the number of disks: 3

Output:
Move disk 1 from rod A to rod C
Move disk 2 from rod A to rod B
Move disk 1 from rod C to rod B
Move disk 3 from rod A to rod C
Move disk 1 from rod B to rod A
Move disk 2 from rod B to rod C
Move disk 1 from rod A to rod C

6. Remark:
The Tower of Hanoi is a recursive solution with a time complexity of O(2^n), making it inefficient for
large n. However, it demonstrates the power of recursion.

Lab Report 2: Bubble Sort


1. Problem Statement
Bubble Sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent
elements, and swaps them if they are in the wrong order. This process is repeated until the list is sorted.
2. Problem Analysis
The algorithm repeatedly compares adjacent elements and swaps them if necessary. After each pass, the
largest unsorted element is moved to its correct position. The algorithm continues until no more swaps
are needed.
3. Algorithm Overview
Bubble Sort Process:
1. Traverse the array and compare adjacent elements.
2. If an element is larger than the next one, swap them.
3. Repeat the process until no swaps are made in a complete pass.
Time Complexity:
 Worst/Average case: O(n^2) (when the array is reversed or partially sorted).
 Best case: O(n) (when the array is already sorted).
Space Complexity: O(1) – In-place sorting with no extra memory required.
4. Source Code
#include<iostream>
using namespace std;

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


bool swapped;
for (int i = 0; i < n-1; i++) {
swapped = false;
for (int j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
swap(arr[j], arr[j+1]);
swapped = true;
}
}
if (!swapped)
break;
}
}

int main() {
int n;
cout << "Enter the array size: ";
cin >> n;
int arr[n];
cout << "Enter the array elements: ";
for (int i = 0; i < n; i++) cin >> arr[i];
bubbleSort(arr, n);
cout << "Sorted array: ";
for (int i = 0; i < n; i++) cout << arr[i] << " ";
return 0;
}

5. Sample Input / Output


Input:
Enter the array size: 5
Enter the array elements: 64 34 25 12 22

Output:
Sorted array: 12 22 25 34 64

6. Remark:
Bubble sort is a simple sorting algorithm but inefficient for large datasets due to its quadratic time
complexity.

Lab Report 3: Binary Search on Unsorted Data


1. Problem Statement
Binary Search is efficient only for sorted datasets. This problem investigates the inefficiency of using
binary search on unsorted data. To perform a binary search on an unsorted array, we first need to sort it,
which incurs additional computational cost.
2. Problem Analysis
If we apply binary search on an unsorted array, the result is incorrect because binary search assumes
that the input array is sorted. Thus, sorting the array first (using any sorting algorithm) is necessary.
3. Algorithm Overview
Steps:
1. Sort the array using a sorting algorithm.
2. Perform binary search as usual.
Time Complexity:
 Sorting the array: O(n log n).
 Binary Search: O(log n).
 Overall: O(n log n).
4. Source Code
#include<iostream>
#include<algorithm>
using namespace std;

int binarySearch(int arr[], int l, int r, int x) {


while (l <= r) {
int mid = l + (r - l) / 2;
if (arr[mid] == x) return mid;
if (arr[mid] < x) l = mid + 1;
else r = mid - 1;
}
return -1;
}

int main() {
int n, x;
cout << "Enter the array size: ";
cin >> n;
int arr[n];
cout << "Enter the array elements: ";
for (int i = 0; i < n; i++) cin >> arr[i];
cout << "Enter the element to be searched: ";
cin >> x;

sort(arr, arr + n);


int res = binarySearch(arr, 0, n-1, x);
(res == -1) ? cout << "Element not found" : cout << "Element found at index: "
<< res;
return 0;
}

5. Sample Input / Output


Input:
Enter the array size: 6
Enter the array elements: 3 9 1 7 10 6
Enter the element to be searched: 6

Output:
Element found at index: 2
6. Remark:
Binary search on unsorted data requires an initial sorting step, making the overall complexity O(n log
n). Hence, it's not efficient compared to linear search for unsorted datasets.

Lab Report 4: Quick Sort


1. Problem Statement
Quick Sort is an efficient, in-place, comparison-based sorting algorithm. It uses the divide-and-
conquer approach to sort elements by partitioning the array and then recursively sorting the subarrays.
2. Problem Analysis
The algorithm picks a pivot element from the array and partitions the other elements into two subarrays
according to whether they are less than or greater than the pivot. It recursively applies the same process
to the subarrays. The base case is when the subarray has one or zero elements, in which case it is
already sorted.
3. Algorithm Overview
Quick Sort Process:
1. Choose a pivot element (commonly the last element).
2. Partition the array such that elements smaller than the pivot are on the left, and elements larger
than the pivot are on the right.
3. Recursively apply quicksort to the left and right subarrays.
Time Complexity:
 Best/Average case: O(n log n).
 Worst case: O(n^2) (when the pivot is the smallest or largest element in the array).
Space Complexity:
 O(log n) for the recursive call stack.
4. Source Code
#include<iostream>
using namespace std;

int partition(int arr[], int low, int high) {


int pivot = arr[high];
int i = low - 1;
for (int j = low; j <= high - 1; j++) {
if (arr[j] < pivot) {
i++;
swap(arr[i], arr[j]);
}
}
swap(arr[i + 1], arr[high]);
return (i + 1);
}
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}

int main() {
int n;
cout << "Enter the array size: ";
cin >> n;
int arr[n];
cout << "Enter the array elements: ";
for (int i = 0; i < n; i++) cin >> arr[i];

quickSort(arr, 0, n - 1);

cout << "Sorted array: ";


for (int i = 0; i < n; i++) cout << arr[i] << " ";

return 0;
}

5. Sample Input / Output


Input:
Enter the array size: 5
Enter the array elements: 10 7 8 9 1

Output:
Sorted array: 1 7 8 9 10

6. Remark:
Quick sort is one of the fastest sorting algorithms for large datasets. Its average-case complexity is O(n
log n), but it may degrade to O(n^2) in the worst case.

Lab Report 5: Stack Implementation (Without Class)


1. Problem Statement
A Stack is a linear data structure that follows the Last In First Out (LIFO) principle. Elements can
only be inserted and removed from the top of the stack.
2. Problem Analysis
We will implement a stack using an array and functions for the basic stack operations:
1. Push: Insert an element at the top.
2. Pop: Remove the top element.
3. Peek/Top: Access the top element without removing it.
4. isEmpty: Check if the stack is empty.
3. Algorithm Overview
1. Push operation adds an element to the top of the stack.
2. Pop operation removes the top element from the stack.
3. Peek operation returns the top element without removing it.
4. isEmpty operation checks if the stack contains any elements.
Time Complexity:
 Push, Pop, Peek, and isEmpty operations: O(1).
Space Complexity: O(n) for storing n elements.

4. Source Code
#include<iostream>
using namespace std;

#define MAX 1000


int stack[MAX];
int top = -1;

bool isEmpty() {
return top == -1;
}

bool push(int x) {
if (top >= MAX - 1) {
cout << "Stack Overflow\n";
return false;
}
stack[++top] = x;
return true;
}

int pop() {
if (isEmpty()) {
cout << "Stack Underflow\n";
return -1;
}
return stack[top--];
}

int peek() {
if (isEmpty()) {
cout << "Stack is Empty\n";
return -1;
}
return stack[top];
}

int main() {
push(10);
push(20);
push(30);
cout << pop() << " Popped from stack\n";
cout << "Top element is: " << peek() << endl;
cout << "Stack is empty: " << (isEmpty() ? "Yes" : "No") << endl;
return 0;
}

5. Sample Input / Output


Output:
30 Popped from stack
Top element is: 20
Stack is empty: No

6. Remark:
This stack implementation is functional and works efficiently for managing small datasets. Larger data
sets may require dynamic memory allocation.

Lab Report 6: Queue Implementation (Without Class)


1. Problem Statement
A Queue is a linear data structure that follows the First In First Out (FIFO) principle. Elements are
inserted at the rear and removed from the front.
2. Problem Analysis
We will implement a queue using an array and functions for the basic queue operations:
1. Enqueue: Insert an element at the rear.
2. Dequeue: Remove the element from the front.
3. Front: Get the front element.
4. isEmpty: Check if the queue is empty.
3. Algorithm Overview
1. Enqueue operation inserts an element at the rear.
2. Dequeue operation removes the element from the front.
3. Front operation retrieves the front element without removing it.
4. isEmpty operation checks if the queue is empty.
Time Complexity:
 Enqueue, Dequeue, Front, and isEmpty operations: O(1).
Space Complexity: O(n) for storing n elements.

4. Source Code
#include<iostream>
using namespace std;

#define MAX 1000


int queue[MAX];
int front = -1, rear = -1;

bool isEmpty() {
return (front == -1 || front > rear);
}

bool enqueue(int x) {
if (rear >= MAX - 1) {
cout << "Queue Overflow\n";
return false;
}
if (front == -1) front = 0; // First element to be inserted
queue[++rear] = x;
return true;
}

int dequeue() {
if (isEmpty()) {
cout << "Queue Underflow\n";
return -1;
}
return queue[front++];
}

int getFront() {
if (isEmpty()) {
cout << "Queue is Empty\n";
return -1;
}
return queue[front];
}

int main() {
enqueue(10);
enqueue(20);
enqueue(30);
cout << dequeue() << " Dequeued from queue\n";
cout << "Front element is: " << getFront() << endl;
cout << "Queue is empty: " << (isEmpty() ? "Yes" : "No") << endl;
return 0;
}

5. Sample Input / Output


Output:
10 Dequeued from queue
Front element is: 20
Queue is empty: No

6. Remark:
This simple queue implementation works for fixed-sized arrays. For dynamic use cases, a circular
queue or dynamically resizable array would be more efficient.

You might also like