0% found this document useful (0 votes)
6 views4 pages

ADS Lab 1

Uploaded by

pathansahil00010
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)
6 views4 pages

ADS Lab 1

Uploaded by

pathansahil00010
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/ 4

BINARY SEARCH

import java.util.*;

import java.lang.*;

class Main {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

//Prompt user to enter size of array

System.out.print("Enter The Size OF Array : ");

int Z = sc.nextInt();

//initialize array with size N

int A[] = new int[Z];

System.out.print("Enter The Elements Of Array : ");//user to enter the

elements of array

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

A[i] = sc.nextInt();

Arrays.sort(A);

System.out.print("Enter The Element To be Searched : ");//user to enter the

element to searched

boolean f = false;

int X = sc.nextInt();

int S = 0, E = Z - 1, M = 0;

while (S <= E) {

M = (S + E) / 2;

if (A[M] == X) {

System.out.println("Element is Present");//print the index where the


element found

f = true;

break;

} else if (A[M] < X) {

S = M + 1;

} else {

E = M - 1;

if (f == false) {

System.out.println("Element is not Found");

//if count is still 0,then element is not found in index

}
LINEAR SEARCH
import java.util.*;

import java.lang.*;

public class Main {

public static void main(String[]args) {

Scanner sc = new Scanner(System.in);

//Prompt user to enter size of array

System.out.print("Enter The Size OF Array : ");

int M = sc.nextInt();

//initialize array with size N

int S[] = new int[M];

System.out.print("Enter The Elements Of Array : ");//user to enter the

elements of array

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

S[i] = sc.nextInt();

boolean f = false;

System.out.print("Enter The Element To be Searched : ");//user to enter the

element to searched

int X = sc.nextInt();

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

if (S[i] == X) {

System.out.println("Element is Present At Index " + i);//print the

index where the element found

f = true;

}
if (f == false) {

System.out.println("Element is Not Present At Index " + i);

//if count is still 0,then element is not found in index

You might also like