-
Notifications
You must be signed in to change notification settings - Fork 28
Search and Select
Jinho D. Choi edited this page Aug 24, 2016
·
7 revisions
- Create a package
edu.emory.mathcs.cs323.select
. - Create an abstract class
AbstractSelect
underselect
. - Make a generic type
T extends
Comparable<T>
. - Declare an abstract method
select
. -
@param list
a list of unsorted keys. -
@param k
the k'th maximum key to search. -
@return
the k'th maximum key in the list. -
@throws
IllegalArgumentException
if the size of the list is smaller thank
.
- Create a class
DumbSelect
underselect
. - Extend
AbstractSelect
. - Make a generic type
T extends
[Comparable<T>
] - Override the
select
method in the class. - Make a copy of the input list.
- Find and remove the maximum key from the copied list.
- Repeat this procedure
k
times, and return the last maximum key.
- Create a class
SmartSelect
underselect
. - Extend
AbstractSelect
. - Make a generic type
T extends
[Comparable<T>
] - Override the
select
method in the class. - Create a new empty list,
maxK
. - For each key in the input list,
- Add the key to
maxK
. - If the size of
maxK
is greater thank
, remove the minimum key inmaxK
.
- Add the key to
- Return the minimum key in
maxK
.
- Assess
LinearSearch
andBinarySearch
using: -
SearchTest#testAccuracy()
. - Assess
DumbSelect
andSmartSelect
using: -
SelectTest#testAccuracy()
. - Compare the speeds of
LinearSearch
andBinarySearch
using: -
SearchTest#compareSpeed()
. - Compare the speeds of
DumbSelect
andSmartSelect
using: -
SelectTest#compareSpeed()
.
- Imagine that the following condition didn't exist in
BinarySearch
(lines 45-46).
if (beginIndex > endIndex)
return -1;
- Give a list of integers containing more than one key, sorted in ascending order, that would make
BinarySearch
throw a different kind of exception or error. Explain why this would be thrown.
Copyright © 2014-2017 Emory University - All Rights Reserved.