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
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).
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.
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.
please explain with comment codes and show sample runs
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started