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

Assignment - 2 DAA LAB (0901CD231057)

Uploaded by

rohankalme4
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)
33 views9 pages

Assignment - 2 DAA LAB (0901CD231057)

Uploaded by

rohankalme4
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

Assignment-2

1. Write a program to find an element in an array using linear search


#include <iostream>
using namespace std;
int linearSearch(int arr[], int n, int target) {
for (int i = 0; i < n; i++) {
if (arr[i] == target) {
return i; // Return the index of the target element
}
}
return -1; // Return -1 if the target element is not found
}
int main() {
int n, target;
// Input size of array
cout << "Enter the number of elements in the array: ";
cin >> n;
int arr[n];
// Input elements of array
cout << "Enter the elements of the array: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
// Input target element
cout << "Enter the element to search for: ";
cin >> target;

// Perform linear search


int result = linearSearch(arr, n, target);

Rohan Kalme 0901CD231057


// Output result
if (result != -1) {
cout << "Element found at index " << result << endl;
} else {
cout << "Element not found in the array." << endl;
}
return 0;
}

Output :-

Time Complexity:
 Best Case: O(1) (when the target element is the first element in the array).
 Worst Case: O(n) (when the target element is the last element in the array or is not
present at all).

Space Complexity:

 The space complexity is O(1) because the algorithm uses a constant amount of extra
space regardless of the size of the input array. It only requires a few extra variables for
the index and target element.

Rohan Kalme 0901CD231057


2. Write a program to find an element in a sorted array using binary
search. (using iteration and recursion)

Binary Search (Iterative) :


#include <iostream>
using namespace std;
int binarySearchIterative(int arr[], int n, int target) {
int left = 0, right = n - 1;
while (left <= right) {
int mid = left + (right - left) / 2;

if (arr[mid] == target)
return mid; // Element found, return index
else if (arr[mid] < target)
left = mid + 1; // Search in the right half
else
right = mid - 1; // Search in the left half
}
return -1; // Element not found
}
int main() {
int n, target;
// Input size of array
cout << "Enter the number of elements in the array: ";
cin >> n;

int arr[n];

// Input elements of array (should be sorted)

Rohan Kalme 0901CD231057


cout << "Enter the elements of the array in sorted order: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}

// Input target element


cout << "Enter the element to search for: ";
cin >> target;

// Perform binary search


int result = binarySearchIterative(arr, n, target);

// Output result
if (result != -1) {
cout << "Element found at index " << result << endl;
} else {
cout << "Element not found in the array." << endl;
}

return 0;
}

Binary Search (Recursive) :


#include <iostream>
using namespace std;

int binarySearchRecursive(int arr[], int left, int right, int target) {


if (left <= right) {
int mid = left + (right - left) / 2;

Rohan Kalme 0901CD231057


if (arr[mid] == target)
return mid; // Element found, return index
else if (arr[mid] < target)
return binarySearchRecursive(arr, mid + 1, right, target); // Search in the right half
else
return binarySearchRecursive(arr, left, mid - 1, target); // Search in the left half
}
return -1; // Element not found
}

int main() {
int n, target;

// Input size of array


cout << "Enter the number of elements in the array: ";
cin >> n;

int arr[n];

// Input elements of array (should be sorted)


cout << "Enter the elements of the array in sorted order: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}

// Input target element


cout << "Enter the element to search for: ";
cin >> target;

// Perform binary search

Rohan Kalme 0901CD231057


int result = binarySearchRecursive(arr, 0, n - 1, target);

// Output result
if (result != -1) {
cout << "Element found at index " << result << endl;
} else {
cout << "Element not found in the array." << endl;
}
return 0;
}

Output :-

Time Complexity:
 Best Case: O(1)(when the target element is the middle element).
 Worst Case: O(log n) (as the problem size is halved at each step).
Space Complexity:
 Iterative Version: O(1), as it uses a constant amount of space.
 Recursive Version: O(log n), due to the recursive call stack that may go
as deep as log n levels, where n is the number of elements in the array.

Rohan Kalme 0901CD231057


3. Write a program to construct a binary tree from inorder and preorder
traversal. (using recursion)
#include <iostream>
#include <vector>
#include <unordered_map>

using namespace std;

struct Node {
int data;
Node *left, *right;
Node(int x) : data(x), left(nullptr), right(nullptr) {}
};

// Function to build the binary tree


Node* buildTree(vector<int>& inorder, vector<int>& preorder, int inStart, int inEnd, int&
preIndex, unordered_map<int, int>& inMap) {
if (inStart > inEnd)
return nullptr;

int currentVal = preorder[preIndex++];


Node* root = new Node(currentVal);

int inIndex = inMap[currentVal];

root->left = buildTree(inorder, preorder, inStart, inIndex - 1, preIndex, inMap);


root->right = buildTree(inorder, preorder, inIndex + 1, inEnd, preIndex, inMap);

return root;
}

Rohan Kalme 0901CD231057


// Function to build the binary tree from inorder and preorder traversals
Node* buildTree(vector<int>& inorder, vector<int>& preorder) {
unordered_map<int, int> inMap;
for (int i = 0; i < inorder.size(); i++) {
inMap[inorder[i]] = i; // Store the index of each value in the inorder traversal
}
int preIndex = 0;
return buildTree(inorder, preorder, 0, inorder.size() - 1, preIndex, inMap);
}

// Utility function to print the tree in inorder (for verification)


void inorderTraversal(Node* root) {
if (root == nullptr)
return;

inorderTraversal(root->left);
cout << root->data << " ";
inorderTraversal(root->right);
}

int main() {
vector<int> inorder = {4, 2, 5, 1, 3, 6};
vector<int> preorder = {1, 2, 4, 5, 3, 6};

Node* root = buildTree(inorder, preorder);

cout << "Inorder traversal of the constructed binary tree: ";


inorderTraversal(root);

Rohan Kalme 0901CD231057


return 0;
}

Output :-

 Time Complexity:
o The time complexity is now O(n), where n is the number of nodes
in the tree. This improvement comes from using an unordered_map
to find the index of elements in the inorder traversal in O(1) time.
 Space Complexity:
o The space complexity is O(n) due to:
o The recursion stack, which can go as deep as the height of the tree.
o The unordered_map that stores the indices of elements in the
inorder traversal.

Rohan Kalme 0901CD231057

You might also like