Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The question was to complete to code below. Complete the body of the static method ( indexOf ) with the linear search algorithm in the

The question was to complete to code below.

Complete the body of the static method ( indexOf ) with the linear search algorithm in the LinearSearch class.?

It must implement the sequential or linear search approach?

It required to use for loop to implement the algorithm.

if comments left out pleas add comments to the code below.

this is example of Algorithm 3.

max = element [0]

min = max

for (each index i)

find max

find min

return max-min

image text in transcribed

package search;

/****************************************************************************

*

This class demonstrates the use of a linear search algorithm.

* Algorithm:

* A target value is compared with each index value.

* The target is checked against each element in the array.

* (a) if target matches a value in an index, return the index

* of the value.

* (b) if a match is not found it returns -1.

****************************************************************************/

public class LinearSearch {

/******************************************************

* returns the index at which the given target value

* first appears in the given input array, or -1 if

* not found.

* @param data array of integers

* @param target value

******************************************************/

public static int indexOf(int[] data, int target) {

System.out.println("target value: " + target);

return 0;

}

/******************************************************

* runs the program. Tests an array of integers.

******************************************************/

public static void main(String[] args) {

int[] data = {2, 3, 1, 5, 8, 6};

print(data);

int index = indexOf(data, 3);

showLocation(index);

index = indexOf(data, 7);

showLocation(index);

index = indexOf(data, 8);

showLocation(index);

}

/*********************************************************

* prints contents of data.

*********************************************************/

public static void print(int[] data) {

System.out.print(" ");

for(int i = 0; i

System.out.print("[" + i + "]");

}

System.out.println();

String result = "[ " + data[0];

for(int i = 1; i

result += ", " + data[i];

}

result += " ]";

System.out.println(result);

System.out.println();

}

/********************************************************

* formats and prints out the target location in

* array if found.

********************************************************/

public static void showLocation(int index) {

if (index == -1) {

System.out.println("no match found ");

}else {

System.out.println("match found at index: " + index);

}

System.out.println();

}

}

Linear Search (Sequential Search) Searching is the array, for example, finding out if a certain score is included in a list of scores. Perhaps the simplest way to search an array is use an iterative loop over its elements and check each element to see if it is the same as the target element. s of looking for a specific element in a data structure such as an Linear Search Algorithm The linear search approach: a) compare the target element (called target) sequentially with each element in the array b) continue to compare until the target matches an element in the array, or the array is exhausted without a match being found. if a match is made, the linear search returns the index of the element in the array that matches the target. if no match is found, the search returns-1. i. ii. Exercise 2 For this exercise, you will implement and run a sequentia/linear search algorithm. Open the search package of L5 SearchLab, and complete the body of the static method indexof with the linear search algorithm in the Linearsearch class. This method accepts an array of integer values and a target search parameter and returns the index location of the target value, if found. Note: You must implement the sequential/linear search approach as shown above, and you are required to use a for loop to implement the algorithm. Use the following data (Given): intl1 data -12, 3, 1, 5,8, 6 Test for the following values: 3, 7, 8 Use empirical analysis to compare the run times for the worst possible case of a linear search for large n with algorithm 3 in the last section. Note: You will have to make your own code and modifications to your EXCEL file. What do you observe? Algorithm 3 Range$3 Description: This algorithm uses a loop to find the largest value and smallest value in the array, compute their difference and return this difference. max = element[0] min max for (each index i) find max. find min. return max - min

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_2

Step: 3

blur-text-image_step3

Ace Your Homework with AI

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

Get Started

Students also viewed these Databases questions