Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have shared with the input codes COMP108W05App.java and COMP108W05.java (a) Background i. In COMP108W05.java, some methods are already implemented: printArray(), seqSearch () and findMin().

I have shared with the input codes image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
COMP108W05App.java and COMP108W05.java (a) Background i. In COMP108W05.java, some methods are already implemented: printArray(), seqSearch () and findMin(). You are going to implement a number of searching algorithms. ii. For simplicity, the programs have been hard-coded so that the array contains 20 integers. Try finding some integers that are in the array and some that are not in the array. The array contains 15,25,10,30,35,20,5,60,80,65,75,70,100,55,90,45,50, 85,95,40 The sorted array contains 5,10,15,20,25,30,35,40,45,50,55,60,65,70,75, 80,85,90,95,100 (b) Task 1 - Binary Search i. The sequential search algorithm has been implemented in the method seqSearch() to check if key is in the array data[] of size n. The method also counts how many comparisons are made. ii. Implement in the method binarySearch() of COMP108W05.java the binary search algorithm to determine if key is in the array datal] of size n. You can refer to the pseudo code in lecture notes. Mind that the lecture notes assume numbers are stored in A[1],A[2],, Mind that the lecture notes assume numbers are stored in A[1],A[2],, A[n] but the program assumes numbers are stored in data[0], data[1], , data [n1], which are already sorted in ascending order. iii. Compile, run, and test your program to see if it determines correctly whether a number is in the array. iv. Update the binarySearch() method to also count the number of comparisons, i.e., it should also count how many values in the array has been compared with. v. The number of comparisons should be between 1 and 5 . If you follow the logic in the lecture notes, the output should be: (c) Task 2 - Finding max/min i. Read the method findMin() to understand its logic. ii. Fill in the method findMax() to find the largest number. It should look very similar to the find min() method. Test if it outputs the largest number in the array hard-coded in the program. (c) Task 2 - Finding max/min i. Read the method findMin() to understand its logic. ii. Fill in the method findMax() to find the largest number. It should look very similar to the find min() method. Test if it outputs the largest number in the array hard-coded in the program. iii. The method findSecondMax() aims to find both the largest and the second largest numbers in the array. Fill in findSecondMax() the code to do so. import java. io. *: class COMP108W05App \{ private static Scanner keyboardInput = new Scanner (System.in); /I main program public static void main(String[] args) throws Exception \{ int key =0,n=20; int [ ] data ={15,425,10,30,35,20,5,60,80,65,75,70,100,55, 90,45,50,85,95,4} int[] sortData ={5,10,15,20,25,30,35,40,45,50,55,60,65, 70,75,80,85,90,95,100} System. out.print("Content of data array: "); COMP108We5. printArray (data, n); System. out, print("Content of sorted data array (for binary search): "); COMP108W05. printArray (sortData, n); /I get an integer input key to search I/ note: there is no error checking try \{ System.out. print("Enter an integer to search: "); key = keyboardinput, nextint (); catch (Exception e) \{ \} 3 if ( data [i]== key) \{ found = true; \} else \{ i=1+1; \} count = count +1 3 System. out. print ("The number " + key + " is "); if (found = false) System.out.print("not "); System.out.print ln ("found by sequential search and the number of. 3 If data[] is an array in ascending order, n is size of array, key is the number we want to find /I You can assume that data[] is already sorted static void binarysearch(int[] data, int n, int key) \{ 3 // print the smallest number in the array of size n static void findmin(int [] data, int n ) \{ int 1 , min; min=data[0]; i=1; while (i<>

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Securing SQL Server Protecting Your Database From Attackers

Authors: Denny Cherry

1st Edition

1597496251, 978-1597496254

More Books

Students also viewed these Databases questions

Question

Find 11 and Vo in the circuit shown. 12 V +

Answered: 1 week ago