Question
in JAVA please thanks. (need answer in a few hours !!)
Exercise #1: Design and implement a program (name it LinearBinarySearch) to implement and test the linear and binary search a
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program:

import java.util.Arrays;
import java.util.Scanner;

public class SearchAlgorithm {

   public static void main(String[] args) {
       int noOfComparisons;
   Scanner scannerObj=new Scanner(System.in); //scanner object creation - read user input
   System.out.println("Enter Array Length:");
   int size=scannerObj.nextInt(); //input Array size
int arr[] = new int[size];
int sortedArray[]=new int[size];
System.out.println("Enter Array Elements:");
for(int index=0;index<size;index++)
{
   arr[index]=scannerObj.nextInt(); //input Array Elements
}
System.out.println("Enter Element to Search:");
int key = scannerObj.nextInt(); //user input Element to Search
int last=arr.length-1;
noOfComparisons=linearSearch(arr,key); //calling linearsearch method
System.out.println("No of Comparisons in LinearSearch:"+noOfComparisons); //printing no of comparisons
Arrays.sort(arr); //For binary search - need to sort elements
for(int index=0;index<size;index++)
   sortedArray[index]=arr[index];
noOfComparisons= binarySearch(sortedArray,0,last,key); //calling binary search method
System.out.println("No of Comparisons in Binary Search:"+noOfComparisons);
  
}
   public static int binarySearch(int arr[], int first, int last, int key){
       int noOfComparisons=0;
       System.out.println("\t\t Binary Search- Array Elements: \t\t");
   for(int index=0;index<arr.length;index++)
       System.out.print(arr[index]+","); //display array elements
       int mid = (first + last)/2;
       while( first <= last ){
       if ( arr[mid] < key ){
       first = mid + 1; //binary search logic
       noOfComparisons++;
       }else if ( arr[mid] == key ){
       System.out.println("Element is found at index: " + mid);
       break;
       }else{
       last = mid - 1;
       }
       mid = (first + last)/2;
       }
       if ( first > last ){
       System.out.println("Element is not found!");
       }
       return noOfComparisons;
       }
     
   public static int linearSearch(int arr[],int key)
   { boolean elementStatus=false;
   int noOfComparisons=0;
   int index;
   System.out.println("\t\t Linear Search- Array Elements: \t\t");
   for(index=0;index<arr.length;index++)
       System.out.print(arr[index]+",");
       for(index=0;index<arr.length;index++)
       { noOfComparisons++; //linear search logic
           if(arr[index]==key)
           {
               elementStatus=true;
               break;
           }
       }
       if(elementStatus==true) {
           System.out.println("Element is found at index: " + index);
       }
       else
           System.out.println("Element is not found!");
       return noOfComparisons;
   }
}

Output:

Enter Array Length: Enter Array Elements: 18 Enter Element to Search: 17 Linear Search- Array Elements: 2,3,17,13,1,18, Eleme

Add a comment
Know the answer?
Add Answer to:
in JAVA please thanks. (need answer in a few hours !!) Exercise #1: Design and implement...
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
  • In JAVA please (need answers in a few hours!) Exercise #2: Design and implement a program...

    In JAVA please (need answers in a few hours!) Exercise #2: Design and implement a program (name it SimpleSort) to implement and test the three sort algorithms (Bubble, Insertion, Selection) discussed in the lecture slides. Define method BubbleSort() to implement Bubble sort of an array of integers. Modify the algorithm implementation to count number of swaps it takes to sort the array. Define method InsertionSort() to implement insertion sort of an array of integers. Modify the algorithm implementation to count...

  • C++ Search & Sort

    Exercise #2: Design and implement a program (name it SimpleSort) to implement and test the three sort algorithms (Bubble, Insertion, Selection) discussed in the lecture slides.  Define method BubbleSort() to implement Bubble sort of an array of integers. Modify the algorithm implementation to count number of swaps it takes to sort the array.  Define method InsertionSort() to implement insertion sort of an array of integers. Modify the algorithm implementation to count number of swaps it takes to sort the array. Define...

  • Looking for a solution done in a Flowgorithm flowchart please. Design an application that has an...

    Looking for a solution done in a Flowgorithm flowchart please. Design an application that has an array of at least 20 integers. It should call a module that uses the sequential search algorithm to locate one of the values. The module should keep a count of the number of comparisons it makes until it finds the value. Then the program should call another module that uses the binary search algorithm to locate the same value. It should also keep a...

  • Design and implement a program that implements an Interpolation Search method. Interpolation search is similar to...

    Design and implement a program that implements an Interpolation Search method. Interpolation search is similar to binary search, except it tries to begin the search nearer to the location of the item. Instead of the using the middle value of the sorted array, interpolation search estimates the location of the target with respect to the first & last values in the array. The implementation is the same as binary search except that you should calculate the mid value as: mid...

  • 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...

  • 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 ....

  • Benchmark Searching and Sorting Write a program that has an array of at least 20 strings...

    Benchmark Searching and Sorting Write a program that has an array of at least 20 strings that you will have your user enter. 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 uses the binary search algorithm to locate the same value. It should also keep...

  • Copy the following java codes and compile //// HighArray.java //// HighArrayApp.java Study carefully the design and...

    Copy the following java codes and compile //// HighArray.java //// HighArrayApp.java Study carefully the design and implementation HighArray class and note the attributes and its methods.    Create findAll method which uses linear search algorithm to return all number of occurrences of specified element. /** * find an element from array and returns all number of occurrences of the specified element, returns 0 if the element does not exit. * * @param foundElement   Element to be found */ int findAll(int...

  • 1. a. Using C++, represent the following graph using adjacency matrix, and implement depth first searching...

    1. a. Using C++, represent the following graph using adjacency matrix, and implement depth first searching (DFS) by stack (define it with class) to traverse the graph. 6 7 2 4 b. If starting with node 2, when node 7 is printed, what numbers are in the stack (for DFS)? Please draw the stack step by step to show how the numbers are pushed into and popped out of it. 2. a. Given a set of integer numbers as int...

  • In Java, Implement a class MyArray as defined below, to store an array of integers (int)....

    In Java, Implement a class MyArray as defined below, to store an array of integers (int). Many of its methods will be implemented using the principle of recursion. Users can create an object by default, in which case, the array should contain enough space to store 10 integer values. Obviously, the user can specify the size of the array s/he requires. Users may choose the third way of creating an object of type MyArray by making a copy of another...

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
ADVERTISEMENT