Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions 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
Check Mark
Knowledge Booster
Background pattern image
Computer Science
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
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education