With regard to the Find Largest Algorithm of Figure 2.14, if the numbers in our
ID: 3760752 • Letter: W
Question
With regard to the Find Largest Algorithm of Figure 2.14, if the numbers in our list were not unique and therefore the largest number could occur more than once, would the algorithm find the first occurence? The last occurrence? Every occurence? Explain precisely how this algorithm would behave when presented with this new condition.
Figure 2.14
Get a value for n, the size of the list
Get values for A1, A2, . . . , An, the list to be searched
Set the value of largest so far to A1
Set the value of location to 1
Set the value of i to 2
While (i less than or equal to n) do
If Ai > largest so far then
Set largest so far to Ai
Set location to i
Add 1 to the value of i
End of the loop
Print out the values of largest so far and location
Stop
Explanation / Answer
The condition given to check largest in the above algorithm is If Ai > largest so far then. Means it reassigns largest number and index only if the new largest is found but not equal value is found
So if largest number occurs more than once, it points to first occurence only.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.