Finish the Java code Use the binary and sequential search algorithm to search list  import java.util.*; public class Problem{ static Scanner console = new Scanner(System.in); final static int SIZE = 1000; public static void main(String[] args){ Integer[] intList = new Integer[SIZE]; SearchSortAlgorithms intSearchObject = new SearchSortAlgorithms(); } } public interface SearchSortADT{ public int seqSearch(T[] list, int start, int length, T searchItem); public int binarySearch(T[] list, int start, int length, T searchItem); public void bubbleSort(T list[], int length); public void selectionSort(T[] list, int length); public void insertionSort(T[] list, int length); public void quickSort(T[] list, int length); public void heapSort(T[] list, int length); }   public class SearchSortAlgorithms implements SearchSortADT{ private int comparisons; public int noOfComparisons(){ //finish this method } public void initializeNoOfComparisons(){ //finish this method } 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; } 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 compElem = (Comparable) 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; } 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 compElem = (Comparable) 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); } public void bubbleSort(T list[], int length){ for (int iteration = 1; iteration < length; iteration++){ for (int index = 0; index < length - iteration; index++){ Comparable compElem = (Comparable) list[index]; if (compElem.compareTo(list[index + 1]) > 0){ T temp = list[index]; list[index] = list[index + 1]; list[index + 1] = temp; } } } } 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); } } private int minLocation(T[] list, int first, int last) { int minIndex = first; for (int loc = first + 1; loc <= last; loc++){ Comparable compElem = (Comparable) list[loc]; if (compElem.compareTo(list[minIndex]) < 0) minIndex = loc; } return minIndex; } private void swap(T[] list, int first, int second){ T temp; temp = list[first]; list[first] = list[second]; list[second] = temp; } public void insertionSort(T[] list, int length){ for (int firstOutOfOrder = 1; firstOutOfOrder < length; firstOutOfOrder ++){ Comparable compElem = (Comparable) list[firstOutOfOrder]; if (compElem.compareTo(list[firstOutOfOrder - 1]) < 0){ Comparable temp = (Comparable) 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; } } } public void quickSort(T[] list, int length){ recQuickSort(list, 0, length - 1); } 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 compElem = (Comparable) list[index]; if (compElem.compareTo(pivot) < 0){ smallIndex++; swap(list, smallIndex, index); } } swap(list, first, smallIndex); return smallIndex; } 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); } } 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); } } private void heapify(T[] list, int low, int high){ int largeIndex; Comparable temp = (Comparable) list[low]; largeIndex = 2 * low + 1; while (largeIndex <= high){ if (largeIndex < high) { Comparable compElem = (Comparable) list[largeIndex]; if (compElem.compareTo(list[largeIndex + 1]) < 0) largeIndex = largeIndex + 1; } if (temp.compareTo(list[largeIndex]) > 0) break; else{ list[low] = list[largeIndex]; low = largeIndex; largeIndex = 2 * low + 1; } } list[low] = (T) temp; } private void buildHeap(T[] list, int length){ for (int index = length / 2 - 1; index >= 0; index--) heapify(list, index, length - 1); } }

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Finish the Java code

Use the binary and sequential search algorithm to search list 

import java.util.*;
public class Problem{
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);
public int binarySearch(T[] list, int start, int length, T searchItem);
public void bubbleSort(T list[], int length);
public void selectionSort(T[] list, int length);
public void insertionSort(T[] list, int length);
public void quickSort(T[] list, int length);
public void heapSort(T[] list, int length);
}

 

public class SearchSortAlgorithms<T> implements SearchSortADT<T>{
private int comparisons;
public int noOfComparisons(){
//finish this method
}
public void initializeNoOfComparisons(){
//finish this method
}
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;
}
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;
}
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);
}
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;
}
}
}
}
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);
}
}
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;
}
private void swap(T[] list, int first, int second){
T temp;
temp = list[first];
list[first] = list[second];
list[second] = temp;
}
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;
}
}
}
public void quickSort(T[] list, int length){
recQuickSort(list, 0, length - 1);
}
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;
}
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);
}
}
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);
}
}
private void heapify(T[] list, int low, int high){
int largeIndex;
Comparable<T> temp =
(Comparable<T>) list[low];
largeIndex = 2 * low + 1;
while (largeIndex <= high){
if (largeIndex < high) {
Comparable<T> compElem =
(Comparable<T>) list[largeIndex];
if (compElem.compareTo(list[largeIndex + 1]) < 0)
largeIndex = largeIndex + 1;
}
if (temp.compareTo(list[largeIndex]) > 0)
break;
else{
list[low] = list[largeIndex];
low = largeIndex;
largeIndex = 2 * low + 1;
}
}
list[low] = (T) temp;
}
private void buildHeap(T[] list, int length){
for (int index = length / 2 - 1; index >= 0; index--)
heapify(list, index, length - 1);
}
}

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY