Question

Any programming language may be used. Please indicate what language you have chosen. You are an...

Any programming language may be used. Please indicate what language you have chosen.


You are an analytics developer, and you need to write the searching algorithm to find the element. Your program should perform the following:

Implement the Binary Search function.


Write a random number generator that creates 1,000 elements, and store them in the array.


Write a random number generator that generates a single element called searched value.


Pass the searched value and array into the Binary Search function.


If the searched value is available in the array, then the output is “Element found,” or else the output is “Not found.”


You need to create a program, which should include the following methods:

Binary search: The Binary Search method should accept the array as the input parameter and the element that needs to be searched.


Random number generator: The random number generator method should generate the random numbers and store all of the elements into the array.


Random number generator for a single element: It will generate a single element and store it in the search value.


assignment must contain:

Output screenshots


Actual program


0 0
Add a comment Improve this question Transcribed image text
Answer #1

I have done code in Java , pls check the code below

Code - Main.java

import java.util.*;

class Main {

    //it return index of search element if present else return -1

    public static int binarySearch(int a[], int leftIndx, int rightIndx, int searchElement)

    {

        //check if rightIndx is greater then leftIndx

        if (rightIndx >= leftIndx) {

            //get midIndx

            int midIndx = leftIndx + (rightIndx - leftIndx) / 2;

            //check elem is present on midIndx

            if (a[midIndx] == searchElement)

                return midIndx;

            //if element is smaller then mid indx then search in left array

            if (a[midIndx] > searchElement)

                return binarySearch(a, leftIndx, midIndx - 1, searchElement);

            //else element is present in right array

            return binarySearch(a, midIndx + 1, rightIndx, searchElement);

        }

        //return -1

        return -1;

    }

    //binary function take array a and seachElement as parameter

    public static int binary(int a[], int  searchElement){

        //call binarySearch function

        return binarySearch(a, 0, a.length - 1, searchElement);

    }

    public static void main(String args[])

    {

        //initialize array of size 1000

        int a[] = new int[1000];

        //create random object to generate random number

        Random rand = new Random();

        //initialzie max to 1000 and min to 1

        int max = 1000, min = 1;

        //generate random number between 1 to 1000 and store in array a[]

        for(int i = 0 ; i < a.length;i++){

            a[i] = rand.nextInt((max - min) + 1) + min;

        }

        //generate random element to be searched

        int searchElement =  rand.nextInt((max - min) + 1) + min;

        //sort the array a

        Arrays.sort(a);

        //call binary search function and pass array a and seach element

        int isFound = binary(a, searchElement);

        //check if isFound is -1 then element not found

        if (isFound == -1)

            System.out.println("Element "+searchElement+" not present");

            

        //else element found

        else

            System.out.println("Element "+searchElement+" found at index " + isFound);

    }

}

Screenshots -

Element 878 found at index 885 ...Program finished with exit code o Press ENTER to exit console.

Element 800 not present ... Program finished with exit code o Press ENTER to exit console. I

pls do give a like , thank you

Add a comment
Know the answer?
Add Answer to:
Any programming language may be used. Please indicate what language you have chosen. You are an...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Program with generic merge sort and binary search method help. The programming language I'm using is...

    Program with generic merge sort and binary search method help. The programming language I'm using is Java. This program should show understanding generic merge sort methods and generic binary search methods in java. The execution should include at least 5 found items including one from the first three items in the sorted array and one from the last three items in the sorted array as well as at least two items not found Create a generic merge sort method that...

  • In C language Write a program that includes a function search() that finds the index of...

    In C language Write a program that includes a function search() that finds the index of the first element of an input array that contains the value specified. n is the size of the array. If no element of the array contains the value, then the function should return -1. The program takes an int array, the number of elements in the array, and the value that it searches for. The main function takes input, calls the search()function, and displays...

  • In C language Write a program that includes a function search() that finds the index of...

    In C language Write a program that includes a function search() that finds the index of the first element of an input array that contains the value specified. n is the size of the array. If no element of the array contains the value, then the function should return -1. The program takes an int array, the number of elements in the array, and the value that it searches for. The main function takes input, calls the search()function, and displays...

  • The purpose of this assignment is to familiarize you with sort algorithms. Problem Description is as follows: 8. Search Benchmarks Write a program that has at least 20 250 integers stored in an ar...

    The purpose of this assignment is to familiarize you with sort algorithms. Problem Description is as follows: 8. Search Benchmarks Write a program that has at least 20 250 integers stored in an array in ascending order. It should call a function that uses the linear search algorithm to locate one of the values. The function should keep a count of the number of comparisons it makes until it finds the value. The program then should call a function that...

  • Using Arrays with Sorting and Searching Algorithms 1) This program has six required outputs and involves...

    Using Arrays with Sorting and Searching Algorithms 1) This program has six required outputs and involves searching and sorting an array of integers. Write a java application that initializes an array with the following numbers, in this order: 23, 17, 5, 90, 12, 44, 38, 84, 77, 3, 66, 55, 1, 19, 37, 88, 8, 97, 25, 50, 75, 61, and 49. Then display the unsorted values. This is required output #1 of 6 for this program. 2) Using a...

  • c++. please show screenshots of output This project will continue to familiarize the student with using...

    c++. please show screenshots of output This project will continue to familiarize the student with using arrays. Store all the function proto-types in project11_funch and store all the functions in project11_func.cpp. Make sure you have proper header comments in each.h,.cpp file. When you run your program, your sample output should show your name. Your report should include the following: 1) project11_func.h 2) project11_func.cpp 3) project11.cpp 4) sample output Write the following functions: a) void fill_array_with_random_nums(int array(), int N): Fill array...

  • Please answer using C ++ programming language ONLY! We have only used <stdio.h> if that helps....

    Please answer using C ++ programming language ONLY! We have only used <stdio.h> if that helps. This is a basic course and the furthest we have gone is BASIC arrays. Write the code for the following problems using arrays. Write what would go in the “int main()” part of the program. For these problems, the entire program and program output is not needed. Just include what goes in the int main() part of the program. 1. Declare an integer array...

  • Please Use C++ Language. And Note that please donot post the answer already there Because there...

    Please Use C++ Language. And Note that please donot post the answer already there Because there is similar answer code but I need with modified Quick sort algorithm ​. Update your program from ... Create an array that holds 1000 random integers between 1-1000. Allow the user to enter an integer to search. Create and implement modified Quick sort algorithm which will sort the array before the Binary Search algorithm is executed. Create and implement a Binary Search Algorithm ....

  • Write a program in which you create an array of random real numbers with a size...

    Write a program in which you create an array of random real numbers with a size of 10001. The values should fall between 900 and 1000. Using the provided statistics library, calculate all statistical values represented by the functions in the library and output the results. The median value can only be correctly found after the array is sorted. After sorting using the selection sort method, search for the median value using both the linear search and binary search methods....

  • The name of the C++ file must be search.cpp Write a program that will read data...

    The name of the C++ file must be search.cpp Write a program that will read data from a file. The program will allow the user to specify the filename. Use a loop that will check if the file is opened correctly, otherwise display an error message and allow the user to re-enter a filename until successful. Read the values from the file and store into an integer array. The program should then prompt the user for an integer which will...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT