Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

» Try the following commands and figure out what they dio make ./bsearch 100 ./l

ID: 3743116 • Letter: #

Question

» Try the following commands and figure out what they dio make ./bsearch 100 ./lsearch 100 python3 bsearch.py 100 python3 lsearch . py 100 Program 1 and 2: Java Versions (60 points) Develop two programs bsearch.java and lsearch.java Replicate the CPP/Python implementations as closely as you can. Your output should match exactly You will also need to make a search.java class Modify the makefile to build your java code as part of make Here is a template of how to pass a function into another function in java. search.java public class search FunctionalInterface public boolean apply (int a, int[] b, int c, int d); public static void test_search(int size, SearchInterface search) interface SearchInterfacet if (search.apply(i,array, 0,size-1)) bsearch.java public class bsearch public static void main(String[ ] args) if ( args. length ! =1 ) System.out.println( "Usage: java bsearch array_size" return; int size- Integer.parseInt (args [0]); System.out.print ( "Testing binary_search with Array of size"); System.out.print(size); System.out.println() search.test_search(size, search: :binary_search);

Explanation / Answer

Please find the Java files for Binary Search and Linear Search below:-

1. BinarySearch.java

//Java implementation of recursive Binary Search

public class BinarySearch {

// Returns index of x if it is present in arr[l..

// r], else return -1

public int binarySearch(int arr[], int l, int r, int x) {

if (r >= l) {

int mid = l + (r - l) / 2;

// If the element is present at the

// middle itself

if (arr[mid] == x)

return mid;

// If element is smaller than mid, then

// it can only be present in left subarray

if (arr[mid] > x)

return binarySearch(arr, l, mid - 1, x);

// Else the element can only be present in right subarray

return binarySearch(arr, mid + 1, r, x);

}

// We reach here when element is not present in array

return -1;

}

}

2. LinearSearch.java

//Java code for linearly search x in arr[]. If x is present then return its location, otherwise return -1

public class LinearSearch {

// This function returns index of element x in arr[]

public int search(int arr[], int n, int x) {

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

// Return the index of the element if the element

// is found

if (arr[i] == x)

return i;

}

// return -1 if the element is not found

return -1;

}

}

The Python code for the same is given below:-

1. BinarySearch.py

# Returns index of x in arr if present, else -1

def binarySearch (arr, l, r, x):

  

    # Check base case

    if r >= l:

  

        mid = l + (r - l)/2

  

        # If element is present at the middle itself

        if arr[mid] == x:

            return mid

          

        # If element is smaller than mid, then it

        # can only be present in left subarray

        elif arr[mid] > x:

            return binarySearch(arr, l, mid-1, x)

  

        # Else the element can only be present

        # in right subarray

        else:

            return binarySearch(arr, mid+1, r, x)

  

    else:

        # Element is not present in the array

        return -1

  

# Test array

arr = [ 2, 3, 4, 10, 40 ]

x = 10

  

# Function call

result = binarySearch(arr, 0, len(arr)-1, x)

if result != -1:

    print "Element is present at index %d" % result

else:

    print "Element is not present in array"

2. LinearSearch.py

Please let me know in case of any clarifications required. Thanks!