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

Assignment 3 (3) - Word A Karlyle ones-0 File Home Insert Draw Design Layout Ref

ID: 3890913 • Letter: A

Question

Assignment 3 (3) - Word A Karlyle ones-0 File Home Insert Draw Design Layout References Mailings Review View Nitro Pro Tell me what you want to do Share You can find this class in the book Ch.14. Use the very same implementation if required. [10 points] 3.3 Searcher Implement Searcher class. This class should have two static methods LinearSeacch) and BiySearch). You can find both methods in Ch.14, although they are implemented in separate classes. You will need to group these functions in Searcher class and convert them to static. Do additional changes to avoid compile errors (this step should not take much sditted). 20 points 3.4 hw3 This class has the main() function. Write main) to follow the algorithm given above. Use the main class given bellow public static void nain(strinaii res) throws Exception f /create a large array of random nunbers, size 18806080e, rangc [e-1800 [10 points) /fcreate a snall array of random numbers, size 5, range [e-2808] rDeclare a stontatch object srsteneRATATantti. %-29s%-2B5%-295M",'", "INDEX", "TINE ELAPSED"); 1e points [1e points Sxatan sut.nciutinr"using Linear Search..."); First Loop: 1.sets each element in snall array as key value. 2. Page 1 of 2 501 words Sign in with your work or school account to use add-ins assigned to you. + 100%

Explanation / Answer

PROGRAM CODE:

package util;

import java.util.Random;

public class Searcher {

public static int LinearSearch(int array[], int element)

{

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

{

if(array[i] == element)

return i;

}

return -1;

}

public static int BinarySearch(int array[], int element)

{

int start = 0;

int end = array.length - 1;

while (start <= end) {

int mid = (start + end) / 2;

if (element == array[mid]) {

return mid;

}

if (element < array[mid]) {

end = mid - 1;

} else {

start = mid + 1;

}

}

return -1;

}

public static void main(String[] args) throws Exception{

Random rand = new Random();

int largeArray[] = new int[100000000];

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

{

largeArray[i] = rand.nextInt(1001);

}

int smallArray[] = new int[5];

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

{

smallArray[i] = rand.nextInt(2001);

}

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

{

int index = LinearSearch(largeArray, smallArray[i]);

System.out.printf(" %-20s%-20s%-20s ", "", index, "TIME ELAPSED");

}

//Add a stop watch object here

//This is not a standard class

//sorting array

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

{

for(int j=0; j<largeArray.length; j++)

{

if(largeArray[i] > largeArray[j])

{

int temp = largeArray[i];

largeArray[i] = largeArray[j];

largeArray[j] = temp;

}

}

}

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

{

int index = LinearSearch(largeArray, smallArray[i]);

System.out.printf(" %-20s%-20s%-20s ", "", index, "TIME ELAPSED");

}

}

}