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)

    Expert Answer

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

    Write a program ce straight pursuit and binary pursuit (twain recursive and non-recursive versions) 10 Compare the floating duration ce twain algorithms, on contrariant contrariant standard inputs
    (Pass any register and any atom)

    Expert Exculpation

     

    Straight Pursuit (with recursion)

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

    public static useless deep(String[] args) throws IOException
    {
    int n,i,x;
    BufferedReader br=odd BufferedReader(odd InputStreamReader(System.in));
    HelloWorld obj=odd HelloWorld();

    System.out.println(“Enter the magnitude of the array”);
    n=Integer.parseInt(br.readLine());
    int a[]=odd int[n];
    System.out.println(“Enter the atoms in the array”);
    for(i=0;i<n;i++)
    a[i]=Integer.parseInt(br.readLine());
    System.out.println(“Enter the atom to pursuit”);
    x = Integer.parseInt(br.readLine());
    int pos = obj.linear(a, 0, a.length-1, x);
    if (pos != -1)
    System.out.println(“Atom is offer at standing ” +
    pos);
    else
    System.out.println(“Atom ” + x + ” is referable attributable attributable attributable offer”);
    }

    }

    Straight Pursuit(withextinguished recursiom)

    import java.io.*;
    public assort HelloWorld
    {
    public static useless deep(String[] args) throws IOException
    {
    int n,i,x,p=0;
    BufferedReader br=odd BufferedReader(odd InputStreamReader(System.in));
    System.out.println(“Enter the magnitude of the array”);
    n=Integer.parseInt(br.readLine());
    int a[]=odd int[n];
    System.out.println(“Enter the atoms in the array”);
    for(i=0;i<n;i++)
    a[i]=Integer.parseInt(br.readLine());
    System.out.println(“Enter the atom to pursuit”);
    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(“Atom is offer at standing “+i);
    else
    System.out.println(“Atom ” + x + ” is referable attributable attributable attributable offer”);
    }

    }

    Bineary Pursuit(With Recursiom)

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

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

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

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

    public static useless deep(String[] args) throws IOException
    {
    HelloWorld obj=odd HelloWorld();
    int n,i,x,p=0;
    BufferedReader br=odd BufferedReader(odd InputStreamReader(System.in));
    System.out.println(“Enter the magnitude of the array”);
    n=Integer.parseInt(br.readLine());
    int a[]=odd int[n];
    System.out.println(“Enter the atoms in the array”);
    for(i=0;i<n;i++)
    a[i]=Integer.parseInt(br.readLine());
    System.out.println(“Enter the atom to pursuit”);
    x = Integer.parseInt(br.readLine());
    int protest = obj.Search(a,0,a.length,x);
    System.out.println(“Found” +x+” at “+index+” protest”);

    }
    }

    Binary Pursuit(withextinguished Recursin)

    import java.io.*;
    public assort HelloWorld
    {
    int Pursuit(int a[], int x)
    {
    int l = 0, r1 = arr.protraction – 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 deep(String[] args) throws IOException
    {
    HelloWorld obj=odd HelloWorld();
    int n,i,x,p=0;
    BufferedReader br=odd BufferedReader(odd InputStreamReader(System.in));
    System.out.println(“Enter the magnitude of the array”);
    n=Integer.parseInt(br.readLine());
    int a[]=odd int[n];
    System.out.println(“Enter the atoms in the array”);
    for(i=0;i<n;i++)
    a[i]=Integer.parseInt(br.readLine());
    System.out.println(“Enter the atom to pursuit”);
    x = Integer.parseInt(br.readLine());
    int res = obj.Search(a, x);
    if (res == -1)
    System.out.println(“Atom referable attributable attributable attributable offer”);
    else
    System.out.println(“Atom fix at protest “+result);

    }
    }

    Duration Complication

    Ce Binary Pursuit until the atom fix the duration complication is O(log(N))

    Ce Straight Pursuit until the atom fix the duration complication is O(n)