Question

Java Programming Write a program to find the number of comparison using binarySearch and the sequentialSearch...

Java Programming

Write a program to find the number of comparison using binarySearch and the sequentialSearch algorithms as follows:

Suppose list is an array of 2500 elements.

1. Use a random number generator to fill list;

2. Use a sorting algorithm to sort list;

3. Search list for some items as follows:

a) Use the binary search algorithm to search list (please work on SearchSortAlgorithms.java and modify the algorithm to count the number of comparisons)

b) Use the sequential search algorithm to search list (please work on SearchSortAlgorithms.java and modify the algorithm to count the number of comparisons)

4. Print the number of comparison in step 3(a) and 3(b). If the item is found in the list, print its position.

(Note: use the java code provided to complete the above tasks)

----------------------------------------------

import java.util.*;

public class Problem51

{

static Scanner console = new Scanner(System.in);

final static int SIZE = 1000;

public static void main(String[] args)

{

Integer[] intList = new Integer[SIZE];

SearchSortAlgorithms<Integer> intSearchObject

= new SearchSortAlgorithms<Integer>();

}

--------------------------------------------------

public interface SearchSortADT<T>
{
public int seqSearch(T[] list, int start, int length, T searchItem);
//Sequantial search algorithm.
//Postcondition: If searchItem is found in the list,
// it returns the location of searchItem;
// otherwise it returns -1.

public int binarySearch(T[] list, int start, int length, T searchItem);
//Binary search algorithm.
//Precondition: The list must be sorted.
//Postcondition: If searchItem is found in the list,
// it returns the location of searchItem;
// otherwise it returns -1.

public void bubbleSort(T list[], int length);
//Bubble sort algorithm.
//Postcondition: list objects are in ascending order.

public void selectionSort(T[] list, int length);
//Selection sort algorithm.
//Postcondition: list objects are in ascending order.

public void insertionSort(T[] list, int length);
//Insertion sort algorithm.
//Postcondition: list objects are in ascending order.

public void quickSort(T[] list, int length);
//Quick sort algorithm.
//Postcondition: list objects are in ascending order.

public void heapSort(T[] list, int length);
//Heap sort algorithm.
//Postcondition: list objects are in ascending order.
}

----------------------------------------------------

public class SearchSortAlgorithms<T> implements SearchSortADT<T>
{
private int comparisons;

public int noOfComparisons()
{
// finish this method
}

public void initializeNoOfComparisons()
{
// finish this method
}

//Sequantial search algorithm.
//Postcondition: If searchItem is found in the list,
// it returns the location of searchItem;
// otherwise it returns -1.
public int seqSearch(T[] list, int start, int length, T searchItem)
{
int loc;
boolean found = false;

for (loc = start; loc < length; loc++)
{
if (list[loc].equals(searchItem))
{
found = true;
break;
}
}

if (found)
return loc;
else
return -1;
} //end seqSearch

//Binary search algorithm.
//Precondition: The list must be sorted.
//Postcondition: If searchItem is found in the list,
// it returns the location of searchItem;
// otherwise it returns -1.
public int binarySearch(T[] list, int start, int length, T searchItem)
{
int first = start;
int last = length - 1;
int mid = -1;

boolean found = false;

while (first <= last && !found)
{
mid = (first + last) / 2;

Comparable<T> compElem = (Comparable<T>) list[mid];

if (compElem.compareTo(searchItem) == 0)
found = true;
else
{
if (compElem.compareTo(searchItem) > 0)
last = mid - 1;
else
first = mid + 1;
}
}

if (found)
return mid;
else
return -1;
}//end binarySearch

public int binSeqSearch15(T[] list, int start, int length, T searchItem)
{
int first = 0;
int last = length - 1;
int mid = -1;

boolean found = false;

while (last - first > 15 && !found)
{
mid = (first + last) / 2;

Comparable<T> compElem = (Comparable<T>) list[mid];

comparisons++;

if (compElem.compareTo(searchItem) ==0)
found = true;
else
{
if (compElem.compareTo(searchItem) > 0)
last = mid - 1;
else
first = mid + 1;
}
}

if (found)
return mid;
else
return seqSearch(list, first, last, searchItem);
}

//Bubble sort algorithm.
//Postcondition: list objects are in ascending order.
public void bubbleSort(T list[], int length)
{
for (int iteration = 1; iteration < length; iteration++)
{
for (int index = 0; index < length - iteration;
index++)
{
Comparable<T> compElem =
(Comparable<T>) list[index];

if (compElem.compareTo(list[index + 1]) > 0)
{
T temp = list[index];
list[index] = list[index + 1];
list[index + 1] = temp;
}
}
}
}//end bubble sort

//Selection sort algorithm.
//Postcondition: list objects are in ascending order.
public void selectionSort(T[] list, int length)
{
for (int index = 0; index < length - 1; index++)
{
int minIndex = minLocation(list, index, length - 1);

swap(list, index, minIndex);
}
}//end selectionSort

//Method to determine the index of the smallest item in
//list between the indices first and last..
//This method is used by the selection sort algorithm.
//Postcondition: Returns the position of the smallest
// item.in the list.
private int minLocation(T[] list, int first, int last)
{
int minIndex = first;

for (int loc = first + 1; loc <= last; loc++)
{
Comparable<T> compElem = (Comparable<T>) list[loc];

if (compElem.compareTo(list[minIndex]) < 0)
minIndex = loc;
}

return minIndex;
}//end minLocation

//Method to swap the elements of the list speified by
//the parameters first and last..
//This method is used by the algorithms selection sort
//and quick sort..
//Postcondition: list[first] and list[second are
//swapped..
private void swap(T[] list, int first, int second)
{
T temp;

temp = list[first];
list[first] = list[second];
list[second] = temp;
}//end swap

//Insertion sort algorithm.
//Postcondition: list objects are in ascending order.
public void insertionSort(T[] list, int length)
{
for (int firstOutOfOrder = 1; firstOutOfOrder < length;
firstOutOfOrder ++)
{
Comparable<T> compElem =
(Comparable<T>) list[firstOutOfOrder];

if (compElem.compareTo(list[firstOutOfOrder - 1]) < 0)
{
Comparable<T> temp =
(Comparable<T>) list[firstOutOfOrder];

int location = firstOutOfOrder;

do
{
list[location] = list[location - 1];
location--;
}
while (location > 0 &&
temp.compareTo(list[location - 1]) < 0);

list[location] = (T) temp;
}
}
}//end insertionSort

//Quick sort algorithm.
//Postcondition: list objects are in ascending order.
public void quickSort(T[] list, int length)
{
recQuickSort(list, 0, length - 1);
}//end quickSort

//Method to partition the list between first and last.
//The pivot is choosen as the middle element of the list.
//This method is used by the recQuickSort method.
//Postcondition: After rearranging the elements,
// according to the pivot, list elements
// between first and pivot location - 1,
// are smaller the the pivot and list
// elements between pivot location + 1 and
// last are greater than or equal to pivot.
// The position of the pivot is also
// returned.
private int partition(T[] list, int first, int last)
{
T pivot;

int smallIndex;

swap(list, first, (first + last) / 2);

pivot = list[first];
smallIndex = first;

for (int index = first + 1; index <= last; index++)
{
Comparable<T> compElem = (Comparable<T>) list[index];

if (compElem.compareTo(pivot) < 0)
{
smallIndex++;
swap(list, smallIndex, index);
}
}

swap(list, first, smallIndex);

return smallIndex;
}//end partition

//Method to sort the elements of list between first
//and last using quick sort algorithm,
//Postcondition: list elements between first and last
// are in ascending order.
private void recQuickSort(T[] list, int first, int last)
{
if (first < last)
{
int pivotLocation = partition(list, first, last);
recQuickSort(list, first, pivotLocation - 1);
recQuickSort(list, pivotLocation + 1, last);
}
}//end recQuickSort

//Heap sort algorithm.
//Postcondition: list objects are in ascending order.
public void heapSort(T[] list, int length)
{
buildHeap(list, length);

for (int lastOutOfOrder = length - 1; lastOutOfOrder >= 0;
lastOutOfOrder--)
{
T temp = list[lastOutOfOrder];
list[lastOutOfOrder] = list[0];
list[0] = temp;
heapify(list, 0, lastOutOfOrder - 1);
}//end for
}//end heapSort

//Method to the restore the heap in the list between
//low and high.
//This method is used by the heap sort algorithm and
//the method buildHeap.
//Postcondition: list elemets between low and high are
// in a heap.
private void heapify(T[] list, int low, int high)
{
int largeIndex;

Comparable<T> temp =
(Comparable<T>) list[low]; //copy the root
//node of
//the subtree

largeIndex = 2 * low + 1; //index of the left child

while (largeIndex <= high)
{
if (largeIndex < high)
{
Comparable<T> compElem =
(Comparable<T>) list[largeIndex];

if (compElem.compareTo(list[largeIndex + 1]) < 0)
largeIndex = largeIndex + 1; //index of the
//largest child
}

if (temp.compareTo(list[largeIndex]) > 0) //subtree
//is already in a heap
break;
else
{
list[low] = list[largeIndex]; //move the larger
//child to the root
low = largeIndex; //go to the subtree to
//restore the heap
largeIndex = 2 * low + 1;
}
}//end while

list[low] = (T) temp; //insert temp into the tree,
//that is, list
}//end heapify


//Method to convert an array into a heap.
//This method is used by the heap sort algorithm
//Postcondition: list elements are in a heap.
private void buildHeap(T[] list, int length)
{
for (int index = length / 2 - 1; index >= 0; index--)
heapify(list, index, length - 1);
}//end buildHeap
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the required code for you. Made proper changes in SearchSortAlgorithms class, and performed a thorough demonstration of the comparison between sequential and binary searches in Problem51.java, explained well using comments. Thanks.

EDIT: I’m getting troubles submitting the answer without losing the format. Showing character limit exceeded error. So I have to paste it as a plain text, which will cause the loss of code formatting and indentations. Sorry for the trouble. If you are using eclipse ,copy the code and press ctrl+shift+F to format the code

// SearchSortAlgorithms.java

public class SearchSortAlgorithms<T> implements SearchSortADT<T> {

private int comparisons;

public int noOfComparisons() {

return comparisons;

// finish this method

}

public void initializeNoOfComparisons() {

comparisons=0;

}

// Sequantial search algorithm.

// Postcondition: If searchItem is found in the list,

// it returns the location of searchItem;

// otherwise it returns -1.

public int seqSearch(T[] list, int start, int length, T searchItem) {

int loc;

boolean found = false;

for (loc = start; loc < length; loc++) {

/**

* incrementing the number of comparisons/steps needed

*/

comparisons++;

if (list[loc].equals(searchItem)) {

found = true;

break;

}

}

if (found)

return loc;

else

return -1;

} // end seqSearch

// Binary search algorithm.

// Precondition: The list must be sorted.

// Postcondition: If searchItem is found in the list,

// it returns the location of searchItem;

// otherwise it returns -1.

public int binarySearch(T[] list, int start, int length, T searchItem) {

int first = start;

int last = length - 1;

int mid = -1;

boolean found = false;

while (first <= last && !found) {

/**

* incrementing the number of comparisons/steps needed

*/

comparisons++;

mid = (first + last) / 2;

Comparable<T> compElem = (Comparable<T>) list[mid];

if (compElem.compareTo(searchItem) == 0)

found = true;

else {

if (compElem.compareTo(searchItem) > 0)

last = mid - 1;

else

first = mid + 1;

}

}

if (found)

return mid;

else

return -1;

}// end binarySearch

public int binSeqSearch15(T[] list, int start, int length, T searchItem) {

int first = 0;

int last = length - 1;

int mid = -1;

boolean found = false;

while (last - first > 15 && !found) {

mid = (first + last) / 2;

Comparable<T> compElem = (Comparable<T>) list[mid];

comparisons++;

if (compElem.compareTo(searchItem) == 0)

found = true;

else {

if (compElem.compareTo(searchItem) > 0)

last = mid - 1;

else

first = mid + 1;

}

}

if (found)

return mid;

else

return seqSearch(list, first, last, searchItem);

}

// Bubble sort algorithm.

// Postcondition: list objects are in ascending order.

public void bubbleSort(T list[], int length) {

for (int iteration = 1; iteration < length; iteration++) {

for (int index = 0; index < length - iteration; index++) {

Comparable<T> compElem = (Comparable<T>) list[index];

if (compElem.compareTo(list[index + 1]) > 0) {

T temp = list[index];

list[index] = list[index + 1];

list[index + 1] = temp;

}

}

}

}// end bubble sort

// Selection sort algorithm.

// Postcondition: list objects are in ascending order.

public void selectionSort(T[] list, int length) {

for (int index = 0; index < length - 1; index++) {

int minIndex = minLocation(list, index, length - 1);

swap(list, index, minIndex);

}

}// end selectionSort

// Method to determine the index of the smallest item in

// list between the indices first and last..

// This method is used by the selection sort algorithm.

// Postcondition: Returns the position of the smallest

// item.in the list.

private int minLocation(T[] list, int first, int last) {

int minIndex = first;

for (int loc = first + 1; loc <= last; loc++) {

Comparable<T> compElem = (Comparable<T>) list[loc];

if (compElem.compareTo(list[minIndex]) < 0)

minIndex = loc;

}

return minIndex;

}// end minLocation

// Method to swap the elements of the list speified by

// the parameters first and last..

// This method is used by the algorithms selection sort

// and quick sort..

// Postcondition: list[first] and list[second are

// swapped..

private void swap(T[] list, int first, int second) {

T temp;

temp = list[first];

list[first] = list[second];

list[second] = temp;

}// end swap

// Insertion sort algorithm.

// Postcondition: list objects are in ascending order.

public void insertionSort(T[] list, int length) {

for (int firstOutOfOrder = 1; firstOutOfOrder < length; firstOutOfOrder++) {

Comparable<T> compElem = (Comparable<T>) list[firstOutOfOrder];

if (compElem.compareTo(list[firstOutOfOrder - 1]) < 0) {

Comparable<T> temp = (Comparable<T>) list[firstOutOfOrder];

int location = firstOutOfOrder;

do {

list[location] = list[location - 1];

location--;

} while (location > 0 && temp.compareTo(list[location - 1]) < 0);

list[location] = (T) temp;

}

}

}// end insertionSort

// Quick sort algorithm.

// Postcondition: list objects are in ascending order.

public void quickSort(T[] list, int length) {

recQuickSort(list, 0, length - 1);

}// end quickSort

// Method to partition the list between first and last.

// The pivot is choosen as the middle element of the list.

// This method is used by the recQuickSort method.

// Postcondition: After rearranging the elements,

// according to the pivot, list elements

// between first and pivot location - 1,

// are smaller the the pivot and list

// elements between pivot location + 1 and

// last are greater than or equal to pivot.

// The position of the pivot is also

// returned.

private int partition(T[] list, int first, int last) {

T pivot;

int smallIndex;

swap(list, first, (first + last) / 2);

pivot = list[first];

smallIndex = first;

for (int index = first + 1; index <= last; index++) {

Comparable<T> compElem = (Comparable<T>) list[index];

if (compElem.compareTo(pivot) < 0) {

smallIndex++;

swap(list, smallIndex, index);

}

}

swap(list, first, smallIndex);

return smallIndex;

}// end partition

// Method to sort the elements of list between first

// and last using quick sort algorithm,

// Postcondition: list elements between first and last

// are in ascending order.

private void recQuickSort(T[] list, int first, int last) {

if (first < last) {

int pivotLocation = partition(list, first, last);

recQuickSort(list, first, pivotLocation - 1);

recQuickSort(list, pivotLocation + 1, last);

}

}// end recQuickSort

// Heap sort algorithm.

// Postcondition: list objects are in ascending order.

public void heapSort(T[] list, int length) {

buildHeap(list, length);

for (int lastOutOfOrder = length - 1; lastOutOfOrder >= 0; lastOutOfOrder--) {

T temp = list[lastOutOfOrder];

list[lastOutOfOrder] = list[0];

list[0] = temp;

heapify(list, 0, lastOutOfOrder - 1);

}// end for

}// end heapSort

// Method to the restore the heap in the list between

// low and high.

// This method is used by the heap sort algorithm and

// the method buildHeap.

// Postcondition: list elemets between low and high are

// in a heap.

private void heapify(T[] list, int low, int high) {

int largeIndex;

Comparable<T> temp = (Comparable<T>) list[low]; // copy the root

// node of

// the subtree

largeIndex = 2 * low + 1; // index of the left child

while (largeIndex <= high) {

if (largeIndex < high) {

Comparable<T> compElem = (Comparable<T>) list[largeIndex];

if (compElem.compareTo(list[largeIndex + 1]) < 0)

largeIndex = largeIndex + 1; // index of the

// largest child

}

if (temp.compareTo(list[largeIndex]) > 0) // subtree

// is already in a heap

break;

else {

list[low] = list[largeIndex]; // move the larger

// child to the root

low = largeIndex; // go to the subtree to

// restore the heap

largeIndex = 2 * low + 1;

}

}// end while

list[low] = (T) temp; // insert temp into the tree,

// that is, list

}// end heapify

// Method to convert an array into a heap.

// This method is used by the heap sort algorithm

// Postcondition: list elements are in a heap.

private void buildHeap(T[] list, int length) {

for (int index = length / 2 - 1; index >= 0; index--)

heapify(list, index, length - 1);

}// end buildHeap

}

// Problem51.java

import java.util.*;

public class Problem51

{

static Scanner console = new Scanner(System.in);

final static int SIZE = 1000;//size of array

final static int NUM_SEARCH = 20; // number of search operations

final static int MAX_VALUE = 1000;// maximum value of an element

public static void main(String[] args)

{

Integer[] intList = new Integer[SIZE];

SearchSortAlgorithms<Integer> intSearchObject = new SearchSortAlgorithms<Integer>();

/**

* Defining a random number generator and filling the array

*/

Random random = new Random();

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

/**

* generating and adding a number between 0 and MAX_VALUE

*/

intList[i] = random.nextInt(MAX_VALUE);

}

/**

* sorting the list, so that the binary search will work

*/

intSearchObject.selectionSort(intList, intList.length);

int searchItem, index;

/**

* performs the search for NUM_SEARCH number of times using the two

* techniques

*/

for (int i = 0; i < NUM_SEARCH; i++) {

/**

* generating a random search value between 0 and MAX_VALUE

*/

searchItem = random.nextInt(MAX_VALUE);

/**

* using binary search

*/

System.out.println("\nUsing binary search to find " + searchItem);

intSearchObject.initializeNoOfComparisons();// resetting number of

// comparisons

index = intSearchObject.binarySearch(intList, 0, intList.length,

searchItem);

if (index != -1) {

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

} else {

System.out.println("Item not found!");

}

System.out.println("Number of comparisons required: "

+ intSearchObject.noOfComparisons());

/**

* using sequential search

*/

System.out.println("\nUsing sequential search to find "

+ searchItem);

intSearchObject.initializeNoOfComparisons();// resetting number of

// comparisons

index = intSearchObject.seqSearch(intList, 0, intList.length,

searchItem);

if (index != -1) {

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

} else {

System.out.println("Item not found!");

}

System.out.println("Number of comparisons required: "

+ intSearchObject.noOfComparisons());

}

}

}

// SearchSortADT.java

public interface SearchSortADT<T> {

public int seqSearch(T[] list, int start, int length, T searchItem);

// Sequantial search algorithm.

// Postcondition: If searchItem is found in the list,

// it returns the location of searchItem;

// otherwise it returns -1.

public int binarySearch(T[] list, int start, int length, T searchItem);

// Binary search algorithm.

// Precondition: The list must be sorted.

// Postcondition: If searchItem is found in the list,

// it returns the location of searchItem;

// otherwise it returns -1.

public void bubbleSort(T list[], int length);

// Bubble sort algorithm.

// Postcondition: list objects are in ascending order.

public void selectionSort(T[] list, int length);

// Selection sort algorithm.

// Postcondition: list objects are in ascending order.

public void insertionSort(T[] list, int length);

// Insertion sort algorithm.

// Postcondition: list objects are in ascending order.

public void quickSort(T[] list, int length);

// Quick sort algorithm.

// Postcondition: list objects are in ascending order.

public void heapSort(T[] list, int length);

// Heap sort algorithm.

// Postcondition: list objects are in ascending order.

}

/*OUTPUT*/

Using binary search to find 977

Item found at index 978

Number of comparisons required: 9

Using sequential search to find 977

Item found at index 978

Number of comparisons required: 979

Using binary search to find 988

Item found at index 991

Number of comparisons required: 10

Using sequential search to find 988

Item found at index 991

Number of comparisons required: 992

Using binary search to find 812

Item found at index 803

Number of comparisons required: 7

Using sequential search to find 812

Item found at index 803

Number of comparisons required: 804

Using binary search to find 894

Item not found!

Number of comparisons required: 10

Using sequential search to find 894

Item not found!

Number of comparisons required: 1000

Using binary search to find 928

Item not found!

Number of comparisons required: 10

Using sequential search to find 928

Item not found!

Number of comparisons required: 1000

Using binary search to find 103

Item found at index 100

Number of comparisons required: 7

Using sequential search to find 103

Item found at index 100

Number of comparisons required: 101

Using binary search to find 100

Item not found!

Number of comparisons required: 10

Using sequential search to find 100

Item not found!

Number of comparisons required: 1000

Using binary search to find 435

Item not found!

Number of comparisons required: 10

Using sequential search to find 435

Item not found!

Number of comparisons required: 1000

Using binary search to find 635

Item not found!

Number of comparisons required: 10

Using sequential search to find 635

Item not found!

Number of comparisons required: 1000

Using binary search to find 297

Item not found!

Number of comparisons required: 10

Using sequential search to find 297

Item not found!

Number of comparisons required: 1000

Using binary search to find 498

Item not found!

Number of comparisons required: 10

Using sequential search to find 498

Item not found!

Number of comparisons required: 1000

Using binary search to find 526

Item not found!

Number of comparisons required: 10

Using sequential search to find 526

Item not found!

Number of comparisons required: 1000

Using binary search to find 829

Item not found!

Number of comparisons required: 10

Using sequential search to find 829

Item not found!

Number of comparisons required: 1000

Using binary search to find 686

Item found at index 681

Number of comparisons required: 10

Using sequential search to find 686

Item found at index 681

Number of comparisons required: 682

Using binary search to find 836

Item found at index 828

Number of comparisons required: 9

Using sequential search to find 836

Item found at index 827

Number of comparisons required: 828

Using binary search to find 960

Item not found!

Number of comparisons required: 10

Using sequential search to find 960

Item not found!

Number of comparisons required: 1000

Using binary search to find 380

Item found at index 380

Number of comparisons required: 10

Using sequential search to find 380

Item found at index 380

Number of comparisons required: 381

Using binary search to find 172

Item found at index 170

Number of comparisons required: 6

Using sequential search to find 172

Item found at index 170

Number of comparisons required: 171

Using binary search to find 975

Item found at index 976

Number of comparisons required: 7

Using sequential search to find 975

Item found at index 976

Number of comparisons required: 977

Using binary search to find 964

Item found at index 968

Number of comparisons required: 5

Using sequential search to find 964

Item found at index 968

Number of comparisons required: 969

Add a comment
Know the answer?
Add Answer to:
Java Programming Write a program to find the number of comparison using binarySearch and the sequentialSearch...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • //Generic interface that describes various searching and sorting //algorithms. Note that the type parameter is unbounded....

    //Generic interface that describes various searching and sorting //algorithms. Note that the type parameter is unbounded. However, //for these algorithms to work correctly, the data objects must //be compared using the method compareTo and equals. //In other words, the classes implementing the list objects //must implement the interface Comparable. The type parameter T //is unbounded because we would like to use these algorithms to //work on an array of objects as well as on objects of the classes //UnorderedArrayList and...

  • Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. E...

    Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. Execute the sort algorithms against the same list, recording information for the total number of comparisons and total execution time for each algorithm. Try several different lists, including at least one that is already in sorted order. ---------------------------------------------------------------------------------------------------------------- /** * Sorting demonstrates sorting and searching on an...

  • I am currently using eclipse to write in java. A snapshot of the output would be...

    I am currently using eclipse to write in java. A snapshot of the output would be greatly appreciated to verify that the program is indeed working. Thanks in advance for both your time and effort. Here is the previous exercise code: /////////////////////////////////////////////////////Main /******************************************* * Week 5 lab - exercise 1 and exercise 2: * * ArrayList class with search algorithms * ********************************************/ import java.util.*; /** * Class to test sequential search, sorted search, and binary search algorithms * implemented in...

  • USE JAVA PROGRAMMING Create a program that would collect list of persons using double link list...

    USE JAVA PROGRAMMING Create a program that would collect list of persons using double link list and use a Merge Sort to sort the object by age. Create a class called Person : name and age Create methods that add, and delete Person from the link list Create a method that sorts the persons' objects by age. package mergesort; public class MergeSortExample { private static Comparable[] aux; // auxiliary array for merges public static void sort(Comparable[] a) { aux =...

  • C++. Difficulty with quickSort function. Code will not run quickSort function. The code I'm having trouble...

    C++. Difficulty with quickSort function. Code will not run quickSort function. The code I'm having trouble with is in bold. -------------------------------------------------------------------------------------------------driverProgram.cpp #include #include #include #include #include "quickSort.cpp" using namespace std; int main() { const int MIN_SIZE = 4; //Array size const int SIZE = 25; int theArray[SIZE] = {11, 22, 33, 44, 55, 66, 77, 88, 99, 12, 13, 14, 15, 16, 17, 18, 19, 18, 19, 20, 21, 22, 23, 24, 25}; cout << "List of 25 items: ";...

  • Using Merge Sort: (In Java) (Please screenshot or copy your output file in the answer) In...

    Using Merge Sort: (In Java) (Please screenshot or copy your output file in the answer) In this project, we combine the concepts of Recursion and Merge Sorting. Please note that the focus of this project is on Merging and don't forget the following constraint: Programming Steps: 1) Create a class called Art that implements Comparable interface. 2) Read part of the file and use Merge Sort to sort the array of Art and then write them to a file. 3)...

  • This project is divided into 3 parts: Part 1. Create a new project and download the arrayList and...

    This project is divided into 3 parts: Part 1. Create a new project and download the arrayList and unorderedArrayList templates that are attached. Create a header file for your unorderedSet template and add it to the project. An implementation file will not be needed since the the new class will be a template. Override the definitions of insertAt, insertEnd, and replaceAt in the unorderedSet template definition. Implement the template member functions so that all they do is verify that the...

  • The file Sorting.java contains the Sorting class from Listing 9.9 in the text. This class implements...

    The file Sorting.java contains the Sorting class from Listing 9.9 in the text. This class implements both the selection sort and the insertion sort algorithms for sorting any array of Comparable objects in ascending order. In this exercise, you will use the Sorting class to sort several different types of objects. 1. The file Numbers.java reads in an array of integers, invokes the selection sort algorithm to sort them, and then prints the sorted array. Save Sorting.java and Numbers.java to...

  • Answer please Exercice 3(25+ 20 pts); Sorting 1-Sorting is a classic subject in computer science. There...

    Answer please Exercice 3(25+ 20 pts); Sorting 1-Sorting is a classic subject in computer science. There are many sorting algorithms a. Complete the below code b.Add comments to the below code to explain each statement c. Write a main test then give the output screen of it 2- a-Write the following two generic methods using the below code. The first method sorts the elements using the Comparable interface and the second uses the Comparator interface. public static <E extends Comparable<E>>...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT