Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You will write a main method and two static methods implementing binary and linear search algorithms. The linear search will step through the elements one

You will write a main method and two static methods implementing binary and linear search algorithms. The linear search will step through the elements one at a time until the correct element is found; the binary search will operate on a pre-sorted set and use this knowledge to divide the search space in half at each step.

Provided Implementation

Students are provided with the StringData class. It contains a single method: Public static String[] getData()

This method returns the complete set of possible lowercase 3 character strings (containing 17,576 entries).

// StringData class provided

import java.util.*; import java.io.File; public class StringData { private static String[] stringDataSet = null; public static String[] getData() { if (stringDataSet == null) { stringDataSet = new String[26 * 26 * 26 * 26 * 26]; char[] tempSet = new char[5]; int index = 0; for (tempSet[0] = 'a'; tempSet[0] <= 'z'; tempSet[0]++) { for (tempSet[1] = 'a'; tempSet[1] <= 'z'; tempSet[1]++) { for (tempSet[2] = 'a'; tempSet[2] <= 'z'; tempSet[2]++) { for (tempSet[3] = 'a'; tempSet[3] <= 'z'; tempSet[3]++) { for (tempSet[4] = 'a'; tempSet[4] <= 'z'; tempSet[4]++) { stringDataSet[index++] = new String(tempSet); } } } } } } return stringDataSet; } }

// end

Student Implementation

Students will implement the Analyzer class with a main method and two search methods as follows.

Search Methods

public static int linearSearch(String[] dataSet, String element) Uses a linear search to find specified element in the dataSet. Returns index of element, or -1 if not found.

public static int binarySearch(String[] dataSet, String element) Uses a binary search to find specified element in the dataSet. Returns index of element, or -1 if not found.

Driver Method (main)

This class will also have a main method which will drive the behavior of the program. The main method should complete the following steps, documenting results and answer questions in the write-up:

1) Access the data set from the StringData class (via the getData method).

2) Search for "not_here" using both algorithms. Capture the time each method requires to execute.

3) Search for "mzzzz" using both algorithms. Capture the time each method requires to execute.

4) Search for "aaaaa" using both algorithms. Capture the time each method requires to execute.

Timing your Methods

To time your methods, use Javas System.nanoTime() method. It reports the time in nanoseconds.

1) Capture the begin time using System.nanoTime().

2) Run the method.

3) Immediately after running, capture the ending time.

4) Subtract the beginning time from the ending time; display it to the screen.

NOTE: You should take a screenshot of the results for each run of each method.

**Provide the code in java

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

AutoCAD Database Connectivity

Authors: Scott McFarlane

1st Edition

0766816400, 978-0766816404

More Books

Students also viewed these Databases questions