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); } }
Finish the Java code
Use the binary and sequential search
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);
}
}
Step by step
Solved in 2 steps