DS Lab Assignment 5
DS Lab Assignment 5
Enrollment no : 0801IT231074
Btech 2nd Year (A3)
Lab Assignment 5
Q-1) Write a program to search the desired element from the given data using linear search.
Code :
#include <stdio.h>
int main() {
printf("Shubham Maurya\n0801IT230174\n\n");
int n, target;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
if (result != -1) {
printf("Element found at index %d\n", result);
} else {
printf("Element not found in the array\n");
}
return 0;
}
Q-2) Write a program to search the desired element from the given data using binary search.
Code :
#include <stdio.h>
if (arr[mid] == target) {
return mid;
}
if (arr[mid] < target) {
left = mid + 1;
}
else {
right = mid - 1;
}
}
return -1;
}
int main() {
printf("Shubham Maurya\n0801IT231074\n\n");
int n, target;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
if (result != -1) {
printf("Element found at index %d\n", result);
} else {
printf("Element not found in the array\n");
}
return 0;
}