Java: Write a program to find the number of comparison using sequentialSearch and binarySearch algorithms using an array of 1200 elements. Use a random number generator to fill list by using a sorting algorithm to sort list and search list for the binary search algorithm to search list. Use the sequential search algorithm to search list and print the number of comparisons. If the item is found in the list, print its position. public class SearchSortAlgorithms implements SearchSortADT{ private int comparisons; public int noOfComparisons(){ return comparisons; } public void initializeNoOfComparisons(){ comparisons = 0; } 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); } }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Java: Write a program to find the number of comparison using sequentialSearch and binarySearch algorithms using an array of 1200 elements. Use a random number generator to fill list by using a sorting algorithm to sort list and search list for the binary search algorithm to search list. Use the sequential search algorithm to search list and print the number of comparisons. If the item is found in the list, print its position.

public class SearchSortAlgorithms<T> implements SearchSortADT<T>{
private int comparisons;
public int noOfComparisons(){
return comparisons;
}
public void initializeNoOfComparisons(){
comparisons = 0;
}
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
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Arrays
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education