Question
MAKE AN JAVA PROGRAMMING WITH VIEWING THE EXAMPLE I PROVIDED FOR YOU , You should carry on with the exercises from unit 3 (the CleverSearcher
MAKE AN JAVA PROGRAMMING WITH VIEWING THE EXAMPLE I PROVIDED FOR YOU ,
You should carry on with the exercises from unit 3 (the CleverSearcher class). Recall that a searcher is given an array and an index,k
say, and that it must nd and return the k th largest element from the array. The idea behind the clever searcher can be described by the following pseudocode:
1 .take the first k elements from the main array
and put them in a small array; sort the small array;
for
each remaining element in the main array
if (it is smaller than the smallest element of the small array)
throw it away;
else
remove the current smallest element of the small array;
place the element into the correct position in the small array;
The idea is that the small array contains the largest
k entries. so far in the main array, in ascending order. Therefore, when you reach the end of the main array, the small array contains the largest k entries from the whole main array.
Since these entries are in ascending order, the rst, and smallest entry is the k th largest entry in the whole of the main array.. Most of this is releatively straightforward to code. The trickiest bit is probably placing the new entry into the correct position in the small array. The simplest way to do this is to overwrite the rst (and smallest) entry, replacing it with
the new, bigger, element (this e ectively does the emove the current smallest element" bit), and then sort the small array again:
smallArray[0] = newValue;
Arrays.sort(smallArray);
However, we can probably improve on this in terms of eciency. We know
that the small array is nearly sorted (only the rst entry is (possibly) out of
place). So we could get it into the right place by doing a sort of bubble sort,
stopping either when the next entry is larger, or when we reach the end of the
array. For example, with the new value shown in red, and in bold font:
I am also showing the example how could the programming work
Adding 5
1,2,3,6,8
5,2,3,6,8
2,5,3,6,8
2,3,5,6,8
Adding 9
1,2,3,6,8
9,2,3,6,8
2,9,3,6,8
2,3,9,6,8
2,3,6,9,8
2,3,6,8,9
I am saying to make an java program
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started