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

Linear vs Binary Search with Bubblesort: This program passes an argument (“y” or

ID: 3709318 • Letter: L

Question

Linear vs Binary Search with Bubblesort: This program passes an argument (“y” or “n”) to the main method to determine if it will print out the random list of integers or not (“y” to print the list, and “n” to not. Anything else will end the program with an “Invalid argument” message). The program initially asks for the length of the list to be created. An array of randomly generated integers is then created as well as a random integer key. Both are determined by the list length. For example, a list length of 7 will generate 7 random integers and a random key all between 0 and 6. The program does a linear search on the key and reports its position (first occurrence). Then, the program sorts the list in ascending order for the binary search. The elapsed sort time is to be captured in milliseconds. The binary search captures the number of reads to find the key. All methods should be completely written by the student without the use of utilities (e.g. Arrays.sort, Arrays.binarysearch, Arrays.toString).

This question has been asked multiple times on chegg and given a solution that doesnt work every time. Thank you very much for your thorough response!

Methods headers are:

public static void printArray(int[] array)

public static int linearSearch(int[] list, int key)

public static void bubbleSort(int[] arrayBS)

public static int binarySearch(int[] list, int key)

Sample Java output: (printList = “y”)

Please enter the list length:

7

Key = 3

Unsorted list: 5 4 2 3 3 1 4

Key 3 found in linear search at position: 4

Sort time is 0 milliseconds

Sorted list: 1 2 3 3 4 4 5

Key 3 found in binary search after 1 reads.

Sample Java output: (printList = “n”)

Please enter the list length:

10000

Key = 6002

Key 6002 found in linear search at position: 670

Sort time is 143 milliseconds

Key 6002 found in binary search after 11 reads.

Sample Java output (printList = "n")

Please enter the list length:

100000

Key = 78187

Key 78187 found in linear search at position: 86427

Sort time is 18637 milliseconds

Key 78187 found in binary search after 16 reads.

Explanation / Answer

import java.io.*;
import java.util.Random;
import java.util.Scanner;
public class ElepsedTime
{
   //print array
   public static void printArray(int[] array)
   {
       for(int i=0;i<array.length;i++)
           System.out.print(array[i]+" ");
       System.out.println();
   }
   //linear search
   public static int linearSearch(int[] list,int key)
   {
       int index=-1;
       for(int i=0;i<list.length;i++)
       {
           //if key found
           if(list[i]==key){
               index=i;
               break;
           }
       }
       return index;
   }
   //bubble sort
   public static void bubbleSort(int[] arrayBS)
   {
       long startTime=System.currentTimeMillis();
       for(int i=0;i<arrayBS.length;i++)
       {
           for(int j=i+1;j<arrayBS.length;j++)
           {
               if(arrayBS[j]<arrayBS[i])
               {
                   int temp=arrayBS[i];
                   arrayBS[i]=arrayBS[j];
                   arrayBS[j]=temp;
               }
           }
       }
       long endTime=System.currentTimeMillis();
       long eTime=endTime-startTime;
       System.out.println("Sort time is "+eTime+" milliseconds");
   }
   //binary search
   public static int binarySearch(int[] list,int key)
   {
       int count=0;
       int l=0,r=list.length-1;
       while(l<=r)
       {
           count++;//count number of reads
           int m=(l+r)/2;
           if(list[m]==key)
               break;
           if(list[m]<key)
               l=m+1;
           else
               r=m-1;
       }
       return count;//return number of reads
   }
   //main driver
   public static void main(String[] args) throws InterruptedException
   {
       Scanner input=new Scanner(System.in);
       Random rand=new Random();
       System.out.println("Please enter the list length:");//prompt for length
       int length = input.nextInt();
       int[] arr=new int[length];
       //generating random array
       for(int i=0;i<length;i++)
           arr[i]=rand.nextInt(length);
       //generating random key
       int key=rand.nextInt(length);
      
       //displaying results
       System.out.print("Key : "+key);
       System.out.print(" Unsorted list: ");
       printArray(arr);
       System.out.println(" key "+key+" found in linear search at position: "+linearSearch(arr,key));
       bubbleSort(arr);
       System.out.print(" Sorted list: ");
       printArray(arr);
       System.out.println(" key "+key+" found in binary search after "+binarySearch(arr,key)+" reads");
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote