(Solved Homework): I appreciate any help with the following Java code program. Below is a given ArrayList class and Main class in search algorithms. 

I appreciate any help with the following Java code program.

Below is a given ArrayList class and Main class in search algorithms. Please modify the existing program so it can time the sequential search and the binary search methods several times each for randomly generated values, and record the results in a table. Do not time individual searches, but groups of them. For example, time 100 searches together or 1,000 searches together. Compare the running times of these two search methods that are obtained during the experiment.

Regarding the efficiency of both search methods, what conclusion can be reached from this experiment?

Main class below:

import java.util.*;

/**

* Class to test sequential search, sorted search, and binary search algorithms

* implemented in ArrayList.

*/

public class Main

{

public static void main(String[] args)

{

Main myAppl = new Main();

}

public Main()

{

Scanner in = new Scanner(System.in);

//List creation and display

int n = 20;

ArrayList numbers = new ArrayList(n);

for (int i = 0; i < n; i++)

numbers.add((int) (Math.random() * 100));

System.out.println(“List of integers:”);

System.out.println(numbers);

//Searching with sequential search

System.out.print(“n(Sequential Search) Enter a number:”);

int x = in.nextInt();

if (numbers.sequentialSearch(x))

System.out.println(“Found!”);

else

System.out.println(“Not found!”);

//Sorting the list

numbers.quicksort();

System.out.println(“nSorted list of integers:”);

System.out.println(numbers);

//Searching with sorted search

System.out.print(“n(Sorted Search) Enter a number:”);

x = in.nextInt();

if (numbers.sortedSearch(x))

System.out.println(“Found!”);

else

System.out.println(“Not found!”);

//Searching with binary search

System.out.print(“n(Binary Search) Enter a number:”);

x = in.nextInt();

if (numbers.binarySearch(x))

System.out.println(“Found!”);

else

System.out.println(“Not found!”);

}

}

Array list below:

/**

* Class implementing an array based list. Sequential search, sorted search, and

* binary search algorithms are implemented also.

*/

public class ArrayList

{

/**

* Default constructor. Sets length to 0, initializing the list as an empty

* list. Default size of array is 20.

*/

public ArrayList()

{

SIZE = 20;

list = new int[SIZE];

length = 0;

}

/**

* Determines whether the list is empty

*

* @return true if the list is empty, false otherwise

*/

public boolean isEmpty()

{

return length == 0;

}

/**

* Prints the list elements.

*/

public void display()

{

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

System.out.print(list[i] + ” “);

System.out.println();

}

/**

* Adds the element x to the end of the list. List length is increased by 1.

*

* @param x element to be added to the list

*/

public void add(int x)

{

if (length == SIZE)

System.out.println(“Insertion Error: list is full”);

else

{

list[length] = x;

length++;

}

}

/**

* Removes the element at the given location from the list. List length is

* decreased by 1.

*

* @param pos location of the item to be removed

*/

public void removeAt(int pos)

{

for (int i = pos; i < length – 1; i++)

list[i] = list[i + 1];

length–;

}

//Implementation of methods in the lab exercise

/**

* Non default constructor. Sets length to 0, initializing the list as an

* empty list. Size of array is passed as a parameter.

*

* @param size size of the array list

*/

public ArrayList(int size)

{

SIZE = size;

list = new int[SIZE];

length = 0;

}

/**

* Returns the number of items in the list (accessor method).

*

* @return the number of items in the list.

*/

public int getLength()

{

return length;

}

/**

* Returns the size of the list (accessor method).

*

* @return the size of the array

*/

public int getSize()

{

return SIZE;

}

/**

* Removes all of the items from the list. After this operation, the length

* of the list is zero.

*/

public void clear()

{

length = 0;

}

/**

* Replaces the item in the list at the position specified by location.

*

* @param location location of the element to be replaced

* @param item value that will replace the value at location

*/

public void replace(int location, int item)

{

if (location < 0 || location >= length)

System.out.println(“Error: invalid location”);

else

list[location] = item;

}

/**

* Adds an item to the list at the position specified by location.

*

* @param location location where item will be added.

* @param item item to be added to the list.

*/

public void add(int location, int item)

{

if (location < 0 || location >= length)

System.out.println(“Error: invalid position”);

else if (length == SIZE)

System.out.println(“Error: Array is full”);

else

{

for (int i = length; i > location; i–)

list[ i] = list[ i – 1];

list[location] = item;

length++;

}

}

/**

* Deletes an item from the list. All occurrences of item in the list will

* be removed.

*

* @param item element to be removed.

*/

public void remove(int item)

{

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

if (list[i] == item)

{

removeAt(i);

i–; //onsecutive values won’t be all removed; that’s why i– is here

}

}

/**

* Returns the element at location

*

* @param location position in the list of the item to be returned

* @return element at location

*/

public int get(int location)

{

int x = -1;

if (location < 0 || location >= length)

System.out.println(“Error: invalid location”);

else

x = list[location];

return x;

}

/**

* Makes a deep copy to another ArrayList object.

*

* @return Copy of this ArrayList

*/

public ArrayList copy()

{

ArrayList newList = new ArrayList(this.SIZE);

newList.length = this.length;

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

newList.list[i] = this.list[i];

return newList;

}

/**

* Bubble-sorts this ArrayList

*/

public void bubbleSort()

{

for (int i = 0; i < length – 1; i++)

for (int j = 0; j < length – i – 1; j++)

if (list[j] > list[j + 1])

{

//swap list[j] and list[j+1]

int temp = list[j];

list[j] = list[j + 1];

list[j + 1] = temp;

}

}

/**

* Quick-sorts this ArrayList.

*/

public void quicksort()

{

quicksort(0, length – 1);

}

/**

* Recursive quicksort algorithm.

*

* @param begin initial index of sublist to be quick-sorted.

* @param end last index of sublist to be quick-sorted.

*/

private void quicksort(int begin, int end)

{

int temp;

int pivot = findPivotLocation(begin, end);

// swap list[pivot] and list[end]

temp = list[pivot];

list[pivot] = list[end];

list[end] = temp;

pivot = end;

int i = begin,

j = end – 1;

boolean iterationCompleted = false;

while (!iterationCompleted)

{

while (list[i] < list[pivot])

i++;

while ((j >= 0) && (list[pivot] < list[j]))

j–;

if (i < j)

{

//swap list[i] and list[j]

temp = list[i];

list[i] = list[j];

list[j] = temp;

i++;

j–;

} else

iterationCompleted = true;

}

//swap list[i] and list[pivot]

temp = list[i];

list[i] = list[pivot];

list[pivot] = temp;

if (begin < i – 1)

quicksort(begin, i – 1);

if (i + 1 < end)

quicksort(i + 1, end);

}

/*

* Computes the pivot location.

*/

private int findPivotLocation(int b, int e)

{

return (b + e) / 2;

}

/*The methods listed below are new additions to the ArrayList class

* of Week 4*/

/**

* This method returns a string representation of the array list elements.

* Classes with this method implemented can get its objects displayed in a

* simple way using System.out.println. For example, if list is an ArrayList

* object, it can be printed by using

*

* System.out.println(list);

*

* @return a string representation of the array list elements.

*/

public String toString()

{

String s = “”;

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

s += list[i] + ” “;

return s;

}

/**

* Determines if an item exists in the array list using sequential (linear)

* search.

*

* @param x item to be found.

* @return true if x is found in the list, false otherwise.

*/

public boolean sequentialSearch(int x)

{

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

if (list[i] == x)

return true;

return false;

}

/**

* Determines if an item exists in the array list using sorted search. List

* must be sorted.

*

* @param x item to be found.

* @return true if x is found in the list, false otherwise.

*/

public boolean sortedSearch(int x)

{

//The list must ne sorted to invoke this method!

int i = 0;

while (i < length && list[i] < x)

i++;

if (i < length && list[i] == x)

return true; // x is in the array

else

return false; // x is not in the array

}

/**

* Determines if an item exists in the array list using binary search. List

* must be sorted.

*

* @param x item to be found.

* @return true if x is found in the list, false otherwise.

*/

public boolean binarySearch(int x)

{

int first = 0, last = length – 1, pivot;

boolean found = false;

while (first <= last && !found)

{

pivot = (first + last) / 2;

if (list[pivot] == x)

found = true;

else if (x < list[pivot])

last = pivot – 1;

else

first = pivot + 1;

}

if (found)

return true;

else

return false;

}

private static int SIZE; //size of the array that stores the list items

private int[] list; //array to store the list items

private int length; //amount of items in the list

}

Expert Answer

 MODIFIED MAIN CLASS TO CALCULATE THE RUNNING TIME OF BOTH THE SORTING METHOD

import java.util.*;

/**

* Class to test sequential search, sorted search, and binary search algorithms

* implemented in ArrayList.

*/

public class Main

{

public static void main(String[] args)

{

Main myAppl = new Main();

}

public Main()

{

Scanner in = new Scanner(System.in);

//List creation with a size of 1000

int n = 1000;

ArrayList numbers = new ArrayList(n);

for (int i = 0; i < n; i++)

numbers.add((int) (Math.random() * 2000));

//System.out.println(“List of integers:”);

//System.out.println(numbers);

//Searching with sequential search

for(int j=1; j<=50; j = j+5){

System.out.println(“nnTesting with “+ (100*(j)) + ” elements”);

long sTime = System.currentTimeMillis();

for(int i=0; i<100*(j); i++){

int x = (int) (Math.random() * 2000); // generating a random number between 0 to 2000 and searching with sequential search

numbers.sequentialSearch(x);

}

long eTime = System.currentTimeMillis();

long elapsedT = eTime – sTime;

System.out.print(“nTime spent on Sequential Search for a set of “+100*(j)+” elements = ” + elapsedT+”ns”);

//Searching with binary search

sTime = System.nanoTime();

for(int i=0; i<100*(j); i++){

int x = (int) (Math.random() * 2000); // generating a random number between 0 to 2000 and searching with binary search

numbers.binarySearch(x);

}

eTime = System.nanoTime();

elapsedT = eTime – sTime;

System.out.print(“nTime Spent on Binary Search for a set of “+100*(j)+” elements = “+elapsedT+”ns”);

}

System.out.println(“n”);

}}

OUTPUT

iWriteHomework
Order NOW for a 10% Discount
Pages (550 words)
Approximate price: -

Why Us?

Top Quality and Well-Researched Papers

All ourbpapers are written from scratch. In fact, Clients who ask for paraphrasing services are highly discouraged. We have writers ready to craft any paper from scratch and deliver quality ahead of time.

Professional and Experienced Academic Writers

Our writers keeps you posted on your papers progress - providing you with paper outline/draft. You are also at liberty to communicate directly with your writer.

Free Unlimited Revisions

If you think we missed something, send your order for a free revision. You have 10 days to submit the order for review after you have received the final document. You can do this yourself after logging into your personal account or by contacting our support.

Prompt Delivery and 100% Money-Back-Guarantee

All papers are always delivered on time, in many cases quite ahead of time. In case we need more time to master your paper, we may contact you regarding the deadline extension. In case you cannot provide us with more time, a 100% refund is guaranteed.

Original & Confidential

We use several writing tools checks to ensure that all documents you receive are free from plagiarism. Our editors carefully review all quotations in the text. We also promise maximum confidentiality in all of our services.

24/7 Customer Support

Our support agents are available 24 hours a day 7 days a week and committed to providing you with the best customer experience. Get in touch whenever you need any assistance.

Try it now!

Calculate the price of your order

Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

Our Services

No need to work on your paper at night. Sleep tight, we will cover your back. We offer all kinds of writing services.

Essays

Essay Writing Service

No matter what kind of academic paper you need and how urgent you need it, you are welcome to choose your academic level and the type of your paper at an affordable price. We take care of all your paper needs and give a 24/7 customer care support system.

Admissions

Admission Essays & Business Writing Help

An admission essay is an essay or other written statement by a candidate, often a potential student enrolling in a college, university, or graduate school. You can be rest assurred that through our service we will write the best admission essay for you.

Reviews

Editing Support

Our academic writers and editors make the necessary changes to your paper so that it is polished. We also format your document by correctly quoting the sources and creating reference lists in the formats APA, Harvard, MLA, Chicago / Turabian.

Reviews

Revision Support

If you think your paper could be improved, you can request a review. In this case, your paper will be checked by the writer or assigned to an editor. You can use this option as many times as you see fit. This is free because we want you to be completely satisfied with the service offered.

× Contact Live Agents