let list=1200 element. Note: java programmimg, use given code. 3a. Use the binary search 3b. Use the sequential search Print the number of comparison in step 3(a) and 3(b). If the item is found in the list, print its position. import java.util.*; public class Problem5 { staticScannerconsole = newScanner(System.in); finalstaticintSIZE = 1000; publicstaticvoidmain(String[] args) { Integer[] intList = newInteger[SIZE]; SearchSortAlgorithms intSearchObject = new SearchSortAlgorithms(); //code } public interface SearchSortADT { publicintseqSearch(T[] list, intstart, intlength, TsearchItem); publicintbinarySearch(T[] list, intstart, intlength, TsearchItem); publicvoidbubbleSort(Tlist[], intlength); publicvoidselectionSort(T[] list, intlength); publicvoidinsertionSort(T[] list, intlength); publicvoidquickSort(T[] list, intlength); publicvoidheapSort(T[] list, intlength); } public class SearchSortAlgorithms implements SearchSortADT{ privateintcomparisons; publicintnoOfComparisons(){ // finishthis } publicvoidinitializeNoOfComparisons() { //code } publicintseqSearch(T[] list, intstart, intlength, TsearchItem) { intloc; booleanfound = false; for (loc = start; loc < length; loc++) { if (list[loc].equals(searchItem)) { found = true; break; } } if (found) return loc; else return -1; } publicintbinarySearch(T[] list, intstart, intlength, TsearchItem) { intfirst = start; intlast = length - 1; intmid = -1; booleanfound = 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; } publicintbinSeqSearch15(T[] list, intstart, intlength, TsearchItem) { intfirst = 0; intlast = length - 1; intmid = -1; booleanfound = 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 returnseqSearch(list, first, last, searchItem); } publicvoidbubbleSort(Tlist[], intlength) { for (intiteration = 1; iteration < length; iteration++) { for (intindex = 0; index < length - iteration; index++) { Comparable compElem = (Comparable) list[index]; if (compElem.compareTo(list[index + 1]) > 0) { Ttemp = list[index]; list[index] = list[index + 1]; list[index + 1] = temp; } } } } publicvoidselectionSort(T[] list, intlength) { for (intindex = 0; index < length - 1; index++) { intminIndex = minLocation(list, index, length - 1); swap(list, index, minIndex); } } privateintminLocation(T[] list, intfirst, intlast) { intminIndex = first; for (intloc = first + 1; loc <= last; loc++) { Comparable compElem = (Comparable) list[loc]; if (compElem.compareTo(list[minIndex]) < 0) minIndex = loc; } return minIndex; } privatevoidswap(T[] list, intfirst, intsecond) { Ttemp; temp = list[first]; list[first] = list[second]; list[second] = temp; } publicvoidinsertionSort(T[] list, intlength) { for (intfirstOutOfOrder = 1; firstOutOfOrder < length; firstOutOfOrder ++) { Comparable compElem = (Comparable) list[firstOutOfOrder]; if (compElem.compareTo(list[firstOutOfOrder - 1]) < 0) { Comparable temp = (Comparable) list[firstOutOfOrder]; intlocation = firstOutOfOrder; do { list[location] = list[location - 1]; location--; } while (location > 0 && temp.compareTo(list[location - 1]) < 0); list[location] = (T) temp; } } } publicvoidquickSort(T[] list, intlength) { recQuickSort(list, 0, length - 1); } privateintpartition(T[] list, intfirst, intlast) { Tpivot; intsmallIndex; swap(list, first, (first + last) / 2); pivot = list[first]; smallIndex = first; for (intindex = 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; } privatevoidrecQuickSort(T[] list, intfirst, intlast) { if (first < last) { intpivotLocation = partition(list, first, last); recQuickSort(list, first, pivotLocation - 1); recQuickSort(list, pivotLocation + 1, last); } } publicvoidheapSort(T[] list, intlength) { buildHeap(list, length); for (intlastOutOfOrder = length - 1; lastOutOfOrder >= 0; lastOutOfOrder--) { Ttemp = list[lastOutOfOrder]; list[lastOutOfOrder] = list[0]; list[0] = temp; heapify(list, 0, lastOutOfOrder - 1); } } privatevoidheapify(T[] list, intlow, inthigh) { intlargeIndex; 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; } privatevoidbuildHeap(T[] list, intlength) { for (intindex = length / 2 - 1; index >= 0; index--) heapify(list, index, length - 1); } }
let list=1200 element. Note: java programmimg, use given code. 3a. Use the binary search 3b. Use the sequential search Print the number of comparison in step 3(a) and 3(b). If the item is found in the list, print its position. import java.util.*; public class Problem5 { staticScannerconsole = newScanner(System.in); finalstaticintSIZE = 1000; publicstaticvoidmain(String[] args) { Integer[] intList = newInteger[SIZE]; SearchSortAlgorithms intSearchObject = new SearchSortAlgorithms(); //code } public interface SearchSortADT { publicintseqSearch(T[] list, intstart, intlength, TsearchItem); publicintbinarySearch(T[] list, intstart, intlength, TsearchItem); publicvoidbubbleSort(Tlist[], intlength); publicvoidselectionSort(T[] list, intlength); publicvoidinsertionSort(T[] list, intlength); publicvoidquickSort(T[] list, intlength); publicvoidheapSort(T[] list, intlength); } public class SearchSortAlgorithms implements SearchSortADT{ privateintcomparisons; publicintnoOfComparisons(){ // finishthis } publicvoidinitializeNoOfComparisons() { //code } publicintseqSearch(T[] list, intstart, intlength, TsearchItem) { intloc; booleanfound = false; for (loc = start; loc < length; loc++) { if (list[loc].equals(searchItem)) { found = true; break; } } if (found) return loc; else return -1; } publicintbinarySearch(T[] list, intstart, intlength, TsearchItem) { intfirst = start; intlast = length - 1; intmid = -1; booleanfound = 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; } publicintbinSeqSearch15(T[] list, intstart, intlength, TsearchItem) { intfirst = 0; intlast = length - 1; intmid = -1; booleanfound = 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 returnseqSearch(list, first, last, searchItem); } publicvoidbubbleSort(Tlist[], intlength) { for (intiteration = 1; iteration < length; iteration++) { for (intindex = 0; index < length - iteration; index++) { Comparable compElem = (Comparable) list[index]; if (compElem.compareTo(list[index + 1]) > 0) { Ttemp = list[index]; list[index] = list[index + 1]; list[index + 1] = temp; } } } } publicvoidselectionSort(T[] list, intlength) { for (intindex = 0; index < length - 1; index++) { intminIndex = minLocation(list, index, length - 1); swap(list, index, minIndex); } } privateintminLocation(T[] list, intfirst, intlast) { intminIndex = first; for (intloc = first + 1; loc <= last; loc++) { Comparable compElem = (Comparable) list[loc]; if (compElem.compareTo(list[minIndex]) < 0) minIndex = loc; } return minIndex; } privatevoidswap(T[] list, intfirst, intsecond) { Ttemp; temp = list[first]; list[first] = list[second]; list[second] = temp; } publicvoidinsertionSort(T[] list, intlength) { for (intfirstOutOfOrder = 1; firstOutOfOrder < length; firstOutOfOrder ++) { Comparable compElem = (Comparable) list[firstOutOfOrder]; if (compElem.compareTo(list[firstOutOfOrder - 1]) < 0) { Comparable temp = (Comparable) list[firstOutOfOrder]; intlocation = firstOutOfOrder; do { list[location] = list[location - 1]; location--; } while (location > 0 && temp.compareTo(list[location - 1]) < 0); list[location] = (T) temp; } } } publicvoidquickSort(T[] list, intlength) { recQuickSort(list, 0, length - 1); } privateintpartition(T[] list, intfirst, intlast) { Tpivot; intsmallIndex; swap(list, first, (first + last) / 2); pivot = list[first]; smallIndex = first; for (intindex = 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; } privatevoidrecQuickSort(T[] list, intfirst, intlast) { if (first < last) { intpivotLocation = partition(list, first, last); recQuickSort(list, first, pivotLocation - 1); recQuickSort(list, pivotLocation + 1, last); } } publicvoidheapSort(T[] list, intlength) { buildHeap(list, length); for (intlastOutOfOrder = length - 1; lastOutOfOrder >= 0; lastOutOfOrder--) { Ttemp = list[lastOutOfOrder]; list[lastOutOfOrder] = list[0]; list[0] = temp; heapify(list, 0, lastOutOfOrder - 1); } } privatevoidheapify(T[] list, intlow, inthigh) { intlargeIndex; 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; } privatevoidbuildHeap(T[] list, intlength) { for (intindex = 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...
Related questions
Question
let list=1200 element.
Note: java programmimg, use given code.
3a. Use the binary search 3b. Use the sequential search
- Print the number of comparison in step 3(a) and 3(b). If the item is found in the list, print its position.
import java.util.*;
public class Problem5
{
staticScannerconsole = newScanner(System.in);
finalstaticintSIZE = 1000;
publicstaticvoidmain(String[] args)
{
Integer[] intList = newInteger[SIZE];
SearchSortAlgorithms<Integer> intSearchObject
= new SearchSortAlgorithms<Integer>();
//code
}
public interface SearchSortADT<T>
{
publicintseqSearch(T[] list, intstart, intlength, TsearchItem);
publicintbinarySearch(T[] list, intstart, intlength, TsearchItem);
publicvoidbubbleSort(Tlist[], intlength);
publicvoidselectionSort(T[] list, intlength);
publicvoidinsertionSort(T[] list, intlength);
publicvoidquickSort(T[] list, intlength);
publicvoidheapSort(T[] list, intlength);
}
public class SearchSortAlgorithms<T> implements SearchSortADT<T>{
privateintcomparisons;
publicintnoOfComparisons(){
// finishthis
}
publicvoidinitializeNoOfComparisons()
{
//code
}
publicintseqSearch(T[] list, intstart, intlength, TsearchItem)
{
intloc;
booleanfound = false;
for (loc = start; loc < length; loc++)
{
if (list[loc].equals(searchItem))
{
found = true;
break;
}
}
if (found)
return loc;
else
return -1;
}
publicintbinarySearch(T[] list, intstart, intlength, TsearchItem)
{
intfirst = start;
intlast = length - 1;
intmid = -1;
booleanfound = 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;
}
publicintbinSeqSearch15(T[] list, intstart, intlength, TsearchItem)
{
intfirst = 0;
intlast = length - 1;
intmid = -1;
booleanfound = 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
returnseqSearch(list, first, last, searchItem);
}
publicvoidbubbleSort(Tlist[], intlength)
{
for (intiteration = 1; iteration < length; iteration++)
{
for (intindex = 0; index < length - iteration;
index++)
{
Comparable<T> compElem =
(Comparable<T>) list[index];
if (compElem.compareTo(list[index + 1]) > 0)
{
Ttemp = list[index];
list[index] = list[index + 1];
list[index + 1] = temp;
}
}
}
}
publicvoidselectionSort(T[] list, intlength)
{
for (intindex = 0; index < length - 1; index++)
{
intminIndex = minLocation(list, index, length - 1);
swap(list, index, minIndex);
}
}
privateintminLocation(T[] list, intfirst, intlast)
{
intminIndex = first;
for (intloc = first + 1; loc <= last; loc++)
{
Comparable<T> compElem = (Comparable<T>) list[loc];
if (compElem.compareTo(list[minIndex]) < 0)
minIndex = loc;
}
return minIndex;
}
privatevoidswap(T[] list, intfirst, intsecond)
{
Ttemp;
temp = list[first];
list[first] = list[second];
list[second] = temp;
}
publicvoidinsertionSort(T[] list, intlength)
{
for (intfirstOutOfOrder = 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];
intlocation = firstOutOfOrder;
do
{
list[location] = list[location - 1];
location--;
}
while (location > 0 &&
temp.compareTo(list[location - 1]) < 0);
list[location] = (T) temp;
}
}
}
publicvoidquickSort(T[] list, intlength)
{
recQuickSort(list, 0, length - 1);
}
privateintpartition(T[] list, intfirst, intlast)
{
Tpivot;
intsmallIndex;
swap(list, first, (first + last) / 2);
pivot = list[first];
smallIndex = first;
for (intindex = 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;
}
privatevoidrecQuickSort(T[] list, intfirst, intlast)
{
if (first < last)
{
intpivotLocation = partition(list, first, last);
recQuickSort(list, first, pivotLocation - 1);
recQuickSort(list, pivotLocation + 1, last);
}
}
publicvoidheapSort(T[] list, intlength)
{
buildHeap(list, length);
for (intlastOutOfOrder = length - 1; lastOutOfOrder >= 0;
lastOutOfOrder--)
{
Ttemp = list[lastOutOfOrder];
list[lastOutOfOrder] = list[0];
list[0] = temp;
heapify(list, 0, lastOutOfOrder - 1);
}
}
privatevoidheapify(T[] list, intlow, inthigh)
{
intlargeIndex;
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;
}
privatevoidbuildHeap(T[] list, intlength)
{
for (intindex = length / 2 - 1; index >= 0; index--)
heapify(list, index, length - 1);
}
}
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by step
Solved in 2 steps with 1 images
Recommended textbooks for you
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 Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
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 Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
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
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY