0% found this document useful (0 votes)
15 views6 pages

DSA Assignment - 6

Uploaded by

aryan.23bce8252
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)
15 views6 pages

DSA Assignment - 6

Uploaded by

aryan.23bce8252
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/ 6

DSA ASSIGNMENT - 6

Thopireddy Aryan Reddy


23BCE8252
1Q. Write a program to implement liner search, binary search.
import java.util.Arrays;

public class SearchExample {

public static int linearSearch(int[] arr, int target) {

for (int i = 0; i < arr.length; i++) {

if (arr[i] == target) {

return i;

return -1;

public static int binarySearch(int[] arr, int target) {

int left = 0, right = arr.length - 1;

while (left <= right) {

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

if (arr[mid] == target) {

return mid;

}
if (arr[mid] < target) {

left = mid + 1;

} else {

right = mid - 1;

return -1;

public static void main(String[] args) {

int[] numbers = {3, 6, 8, 12, 14, 17, 25, 30, 36,


40};

int target = 17;

int linearResult = linearSearch(numbers, target);

System.out.println("Linear Search: " +


(linearResult != -1 ? "Found at index " + linearResult : "Not
found"));

Arrays.sort(numbers);

int binaryResult = binarySearch(numbers, target);

System.out.println("Binary Search: " +


(binaryResult != -1 ? "Found at index " + binaryResult : "Not
found"));

}
2Q.Write a program that takes the details of
a person (name, age, and city) and search
for a specific name using linear search
import java.util.Scanner;

class Person {

String name;

int age;

String city;

public Person(String name, int age, String city) {

this.name = name;

this.age = age;

this.city = city;

public class PersonSearch {

public static int linearSearch(Person[] people, String


targetName) {

for (int i = 0; i < people.length; i++) {

if (people[i].name.equalsIgnoreCase(targetName))
{

return i;

}
return -1;

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the number of people: ");

int n = scanner.nextInt();

scanner.nextLine();

Person[] people = new Person[n];

for (int i = 0; i < n; i++) {

System.out.println("Enter details for person " +


(i + 1));

System.out.print("Name: ");

String name = scanner.nextLine();

System.out.print("Age: ");

int age = scanner.nextInt();

scanner.nextLine();

System.out.print("City: ");

String city = scanner.nextLine();

people[i] = new Person(name, age, city);

System.out.print("Enter the name to search: ");

String targetName = scanner.nextLine();


int index = linearSearch(people, targetName);

if (index != -1) {

System.out.println("Person found: " +


people[index].name + ", Age: " + people[index].age + ", City:
" + people[index].city);

} else {

System.out.println("Person not found");

scanner.close();

3Q.Write a Program to implement Binary


search on the given sequence
12,33,42,51,66,73,87,99,101

public class BinarySearchExample {

public static int binarySearch(int[] arr, int target) {

int left = 0, right = arr.length - 1;

while (left <= right) {

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

if (arr[mid] == target) {

return mid;

}
if (arr[mid] < target) {

left = mid + 1;

} else {

right = mid - 1;

return -1;

public static void main(String[] args) {

int[] numbers = {12, 33, 42, 51, 66, 73, 87, 99,
101};

int target = 73;

int result = binarySearch(numbers, target);

if (result != -1) {

System.out.println("Element found at index " +


result);

} else {

System.out.println("Element not found");

You might also like