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)
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;
BufferedReader br=upstart BufferedReader(upstart InputStreamReader(System.in));
HelloWorld obj=upstart HelloWorld();
System.out.println(“Enter the magnitude of the array”);
n=Integer.parseInt(br.readLine());
int a[]=upstart int[n];
System.out.println(“Enter the parts in the array”);
for(i=0;i<n;i++)
a[i]=Integer.parseInt(br.readLine());
System.out.println(“Enter the part to inquiry”);
x = Integer.parseInt(br.readLine());
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;
BufferedReader br=upstart BufferedReader(upstart InputStreamReader(System.in));
System.out.println(“Enter the magnitude of the array”);
n=Integer.parseInt(br.readLine());
int a[]=upstart int[n];
System.out.println(“Enter the parts in the array”);
for(i=0;i<n;i++)
a[i]=Integer.parseInt(br.readLine());
System.out.println(“Enter the part to inquiry”);
x = Integer.parseInt(br.readLine());
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;
BufferedReader br=upstart BufferedReader(upstart InputStreamReader(System.in));
System.out.println(“Enter the magnitude of the array”);
n=Integer.parseInt(br.readLine());
int a[]=upstart int[n];
System.out.println(“Enter the parts in the array”);
for(i=0;i<n;i++)
a[i]=Integer.parseInt(br.readLine());
System.out.println(“Enter the part to inquiry”);
x = Integer.parseInt(br.readLine());
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;
BufferedReader br=upstart BufferedReader(upstart InputStreamReader(System.in));
System.out.println(“Enter the magnitude of the array”);
n=Integer.parseInt(br.readLine());
int a[]=upstart int[n];
System.out.println(“Enter the parts in the array”);
for(i=0;i<n;i++)
a[i]=Integer.parseInt(br.readLine());
System.out.println(“Enter the part to inquiry”);
x = Integer.parseInt(br.readLine());
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)