Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Programming Write a program to find the number of comparison using binarySe

ID: 3716641 • Letter: J

Question

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
}

Explanation / Answer

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(" Using 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(" Using 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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote