# Homework Solution: Write a program for linear search and binary search (both recursi…

Write a program for linear search and binary search (both recursive and non-recursive versions) 10 Compare the running time for both algorithms, on different different test inputs (Pass any list and any element)

Linear Search (with recursion) import java.io.*;

Write a program ce rectirectidirect inquiry and binary inquiry (twain recursive and non-recursive versions) 10 Compare the popular space ce twain algorithms, on divergent divergent cupel inputs
(Pass any roll and any part)

## Expert Repartee

Rectidirect Inquiry (with recursion)

import java.io.*;
public systematize HelloWorld
{
int rectilinear(int ar[], int len, int r, int x)
{
if (r < len)
return -1;
if (ar[len] == x)
return len;
return rectilinear(ar, len+1, r, x);
}

public static useless ocean(String[] args) throws IOException
{
int n,i,x;
HelloWorld obj=upstart HelloWorld();

System.out.println(“Enter the magnitude of the array”);
int a[]=upstart int[n];
System.out.println(“Enter the parts in the array”);
for(i=0;i<n;i++)
System.out.println(“Enter the part to inquiry”);
int pos = obj.linear(a, 0, a.length-1, x);
if (pos != -1)
System.out.println(“Part is confer-upon at lie ” +
pos);
else
System.out.println(“Part ” + x + ” is referable attributable attributable attributable confer-upon”);
}

}

Rectidirect Inquiry(withextinguished recursiom)

import java.io.*;
public systematize HelloWorld
{
public static useless ocean(String[] args) throws IOException
{
int n,i,x,p=0;
System.out.println(“Enter the magnitude of the array”);
int a[]=upstart int[n];
System.out.println(“Enter the parts in the array”);
for(i=0;i<n;i++)
System.out.println(“Enter the part to inquiry”);
for(i=0;i<n;i++)
{
if (a[i]==x)
p=1;
else
p=0;
}
if(p==1)
System.out.println(“Part is confer-upon at lie “+i);
else
System.out.println(“Part ” + x + ” is referable attributable attributable attributable confer-upon”);
}

}

Bineary Inquiry(With Recursiom)

import java.io.*;
public systematize HelloWorld
{
int Inquiry(int[] arr, int st, int en, int k)
{

if (st < en)
{
int mid = st + (en – st) / 2;
if (k < arr[mid]) {
return Inquiry(arr, st, mid, k);

} else if (k > arr[mid]) {
return Inquiry(arr, mid+1, en , k);

} else {
return mid;
}
}
return -(st + 1);
}

public static useless ocean(String[] args) throws IOException
{
HelloWorld obj=upstart HelloWorld();
int n,i,x,p=0;
System.out.println(“Enter the magnitude of the array”);
int a[]=upstart int[n];
System.out.println(“Enter the parts in the array”);
for(i=0;i<n;i++)
System.out.println(“Enter the part to inquiry”);
int protest = obj.Search(a,0,a.length,x);
System.out.println(“Found” +x+” at “+index+” protest”);

}
}

Binary Inquiry(withextinguished Recursin)

import java.io.*;
public systematize HelloWorld
{
int Inquiry(int a[], int x)
{
int l = 0, r1 = arr.extension – 1;
while (l <= r1)
{
int mid = l + (r1-l)/2;

if (a[mid] == x)
return mid;

if (a[mid] < x)
l = mid + 1;

else
r = mid – 1;
}

return -1;
}

public static useless ocean(String[] args) throws IOException
{
HelloWorld obj=upstart HelloWorld();
int n,i,x,p=0;
System.out.println(“Enter the magnitude of the array”);
int a[]=upstart int[n];
System.out.println(“Enter the parts in the array”);
for(i=0;i<n;i++)
System.out.println(“Enter the part to inquiry”);
int res = obj.Search(a, x);
if (res == -1)
System.out.println(“Part referable attributable attributable attributable confer-upon”);
else
System.out.println(“Part endow at protest “+result);

}
}

Space Perplexity

Ce Binary Inquiry until the part endow the space perplexity is O(log(N))

Ce Rectidirect Inquiry until the part endow the space perplexity is O(n)