Question

In: Computer Science

There are wide applications of the searching algorithm, where given a list of objects, to find...

There are wide applications of the searching algorithm, where given a list of objects, to find whether the search target is in the list.

The intuitive solution is that we walk through the list until the search target is found or the end of the list is reached. The solution is called Linear Search.

For general purpose, let us use ListADT and define a static generic linear search method as follows:

public static <T extends Comparable<T>> int search(ListADT<T> array, T targetValue) throws EmptyCollectionException;

Please implement the linear search method, which walks through the given list and search for the targetValue until the targetValue is found or the end of the list is reached. If the targetValue is found, the method returns the index of the targetValue in the list or else it returns -1.

public static <T extends Comparable<T>> int search(ListADT<T> array, T targetValue) throws EmptyCollectionException {

}

In the hands-out or your sketchbook, please write down your code and take a snapshot and submit it.

Additionally, discuss and answer the following question:

what are the worst and average case running times for serial search?

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

Note: Worst case running time of linear search is O(n) if n is the size of ListADT and the element to search is found at last index or not found. Best case running time is O(1) if element is found at index 0. If the elements are uniformly distributed, average running time will be O(n/2)

/**

* Since you did not provide ListADT or its methods, I'm writing this

* solution based on assumptions, assuming ListADT has methods get(index)

* that returns an element at a given index and size() that returns the

* number of elements.

*/

public static <T extends Comparable<T>> int search(ListADT<T> array,

            T targetValue) throws EmptyCollectionException {

      // looping from i=0 to i=array.size()-1

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

            // getting element at i and comparing with targetValue

            if (array.get(i).equals(targetValue)) {

                  // found, returning index

                  return i;

            }

      }

      // not found, returning -1

      return -1;

}


Related Solutions

You are an analytics developer, and you need to write the searching algorithm to find the...
You are an analytics developer, and you need to write the searching algorithm to find the element. Your program should perform the following: Implement the Binary Search function. Write a random number generator that creates 1,000 elements, and store them in the array. Write a random number generator that generates a single element called searched value. Pass the searched value and array into the Binary Search function. If the searched value is available in the array, then the output is...
Using Java implement a searching algorithm to solve the following problem (please specify the searching algorithm...
Using Java implement a searching algorithm to solve the following problem (please specify the searching algorithm being used) Requirements Choose one problem with an algorithm and implement it. You should show and explain the result whatever you got. I recommend using N-Queen problem (at least N=8 or more) or any simple perfect games. For example, - N-Queen problem with hill climbing - N-Queen problem with simulated annealing - N-Queen problem with genetic algorithm - Tic-Tac-Toe with Minimax
Using Java implement a searching algorithm to solve the following problem (please specify the searching algorithm...
Using Java implement a searching algorithm to solve the following problem (please specify the searching algorithm being used) N-Queen problem with genetic algorithm Please use the N-Queen problem (at least N=8 or more) or any simple perfect games. Please provide a screenshot of output and please heavily comment the code. Thanks!
Q1. Explain why tolerance is not given for a POT resistor. Q2. List five applications where...
Q1. Explain why tolerance is not given for a POT resistor. Q2. List five applications where an LDR can be used. Q3. Discuss what factors could limit the amount of energy stored in a capacitor and explain why capacitors are not useful to store energy for long time. Q4. Explain why resistance cannot be measured when the resistor is connected to the circuit. Q5. If you connect a wire between the terminals of a source, the source is then short-circuited....
Please find and share one sorting/searching algorithm. Explain it, and discuss its efficiency with your friends!
Please find and share one sorting/searching algorithm. Explain it, and discuss its efficiency with your friends!
Write a recursive algorithm in pseudo-code to compute the “power list” of a given list of...
Write a recursive algorithm in pseudo-code to compute the “power list” of a given list of integers. Assume that the List type has members: int List.length returns the length of the list. void List.push(T n) pushes an element n to the front of the list T List.pop() pops an element from the front of the list. List$$ List$$.concat(List$$ other) returns the concatenation of this list with other. Explain in plain English the reasoning behind your algorithm. Power Lists should be...
1) Please find and share one sorting/searching algorithm. Explain it, and discuss its efficiency 2) Please...
1) Please find and share one sorting/searching algorithm. Explain it, and discuss its efficiency 2) Please find and share algorithm about red/black trees. Explain it, give 2 real world usages and discuss its efficiency 3) Please find and share one algorithm about Binary Search Trees. Explain it, and discuss it's efficiency
(Lower bound for searching algorithms) Prove: any comparison-based searching algorithm on a set of n elements...
(Lower bound for searching algorithms) Prove: any comparison-based searching algorithm on a set of n elements takes time Ω(log n) in the worst case. (Hint: you may want to read Section 8.1 of the textbook for related terminologies and techniques.)
a) List some applications which find batch-mode processing useful. Justify your answer. b) List some applications...
a) List some applications which find batch-mode processing useful. Justify your answer. b) List some applications which find interactive-mode processing useful. Justify your answer
Given the following list of objects and a fixed bin size of 70: L = [16,...
Given the following list of objects and a fixed bin size of 70: L = [16, 40, 47, 26, 48, 31, 30, 24, 36, 17] What is the lower bound on the number of bins required? Group of answer choices
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT