Abhishek DAA
Abhishek DAA
Abhishek DAA
(LC-DS-345G)
V SEMESTER
CSE-DS ENGINEERING
Attendance 5
Conduction of Program 10
Record Maintenance 5
Internal Viva 5
Total 25
Experiment 1
Program – Write a Program for Linear Search.
#include <iostream>
using namespace std;
// Function to perform linear search
int search(int array[], int n, int x) {
for (int i = 0; i < n; i++) {
if (array[i] == x) {
return i; // Return the index where the element is found
}
}
return -1; // Element not found
}
int main() {
int array[] = {2, 4, 7, 8, 0, 1, 9};
int x = 8;
int n = sizeof(array) / sizeof(array[0]); // Calculate the number of
elements in the array
int result = search(array, n, x); // Perform the search
if (result == -1) {
cout << "Element not found";
} else {
cout << "Element found at index: " << result;
}
return 0;
}
OUTPUT:
Element found at index: 3
Experiment 2
Program – Write a Program for Binary Search (Iterative Method).
#include <iostream>
using namespace std;
// Recursive function for binary search
int binarySearch(int arr[], int left, int right, int target) {
if (right >= left) {
int mid = left + (right - left) / 2; // Find the middle index
if (arr[mid] == target)
return mid;
if (arr[mid] > target)
return binarySearch(arr, left, mid - 1, target);
return binarySearch(arr, mid + 1, right, target);
}
// Return -1 if the target is not present in the array
return -1;
}
int main() {
int arr[] = {2, 3, 4, 10, 40, 50, 70};
int n = sizeof(arr) / sizeof(arr[0]);
int target = 10; // Element to search fo
int result = binarySearch(arr, 0, n - 1, target);
if (result != -1)
cout << "Element " << target << " found at index " << result <<
endl;
else
cout << "Element " << target << " not found in array" << endl;
return 0;
}
OUTPUT:
Element 10 found at index 3
Experiment 4
Program – Write a Program for Quick Sort.
OUTPUT:
Unsorted Array:
8761092
Sorted array in ascending order:
0126789
Experiment 5
Program – Write a Program for Merge Sort.
OUTPUT:
Sorted array:
1 5 6 9 10 12
Experiment 6
Program – Write a Program for Fractional Knapsack.
#include <iostream>
#include <vector>
#include <algorithm>
OUTPUT:
Maximum value in the knapsack = 240
Experiment 7
Program – Write a Program for 0/1 Knapsack.
#include <iostream>
#include <vector>
using namespace std;
OUTPUT:
Maximum value in the knapsack = 220