Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

a) Describe, using English, an algorithm for finding the largest integer in a list ofnintegers.

b.) Express this algorithm in pseudocode.

c.) How many comparisons does the algorithm use?

Short Answer

Expert verified

a) The variable “max” will keep track of the largest integer in the set.

We first set the maximum “max” as the first integer in the list.Then the second element in the list is compared to the “max”, is larger, then the value of “max” is redefined as the value of this element. Then the next element in the list is compared to the “max”, if it is larger, then the value of this element and so on.

When we have checked all integers in the list, then the algorithm will return the value “max” which contains the largest integer.

b) procedure largest(a1,a2,.....,: integers with n1)

max:=a1

for i:=2ton

if ai>max thenmax:=ai

return

c)n-1 comparisons

Step by step solution

01

(a) Step 1: Describe, using English, an algorithm for finding the largest integer in a list of n integers

The variable “max” will keep track of the largest integer in the set.

We first set the maximum “max” as the first integer in the list.Then the second element in the list is compared to the “max”, is larger, then the value of “max” is redefined as the value of this element.Then the next element in the list is compared to the “max”, if it is larger, then the value of this element and so on.

When we have checked all integers in the list, then the algorithm will return the value “max” which contains the largest integer.

02

(b) Step 2: What are the different ways to describe algorithms

We call the algorithm “largest” and the input is a list of integers.

procedurelargest ( a1,a2,.....,an : integers with n1)

We first set the maximum “max” as the first integer in the list.

max:=a1

Then the second element in the list is compared to the “max”, if it is larger, then the value of “max” is redefined as the value of this element.Then the next element in the list is compared to the “max’, if it is larger, then the value of “max”, is redefined as the value of the element and so on.

for i=2to n

if ai>max thenmax=ai

When we have checked all integers in the list, then the algorithm will return the value “max”, which contains the largest integer.

return

Combaining all these steps, we then obtain the algorithm:

procedure largest( a1,a2,.....,an: integers with n1)

max=a1

for i:=2ton

if ai>max thenmax:=ai

return max

03

(c) Step 3: How many comparisons does the algorithm use

The algorithm only makes a comparison in the stepif ai>max,thus comparison is made in every iteration of the for-loop.

Since i can take on values from 2 to n , the for-loop has n-1iterations and thus n-1comparisons are made in the algorithm.

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

See all solutions

Recommended explanations on Math Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free