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 an algorithm for finding the first and second largest elements in a list of integers.

b) Estimate the number of comparisons used.

Short Answer

Expert verified

(a) procedure last largest (a1,a2,an:integerswithn3)

if a1<a2 then

max:=a2

next:=a1

else

max:=a1

next:=a2

for i:=3 to n

if aimax then

next:=max

max:=ai

else ifnextai<maxthen

next:=ai

returnlast,next

(b)2n-1,O(n)

Step by step solution

01

Step 1

(a) $\ text {\color{\# 4257 b 2} Let us call the algorithm "two largest" and the input of

the algorithm is a list of integers a1,a2,,an \color\{default }

\ \

\ textbf\{procedure \}two largest (a1,a2,an integers with n3)

\ \

\color{4257b2}The variable “max” will keep track of the largest number, while “next”

Will keep track of the second largest number. We initially define the first two

elements in the list as these two variables (largest of the two is “max” , while

smallest is “next”, if equal, then both values are set to the values).\color{default}

\ \

\textbf{if} a1<a2\textbf{then}

max:=a2

next:=a1

\textbf{else}

max:=a1

next:=a2

\ \

\color{#4257b2}Next we compare every ( other) element in the list with the current

Largest number. if the element is larger than or equal to the current largest

number, then we replace the maximum by this element and we move the old

maximum to second largest element\

Next, we also determine if the element if between the second largest and the

Largest element(up to now ). If this is the case, then we replace the second highest

element by this new element\color{default}

\ \

\textbf{for}i=3\textbf{to}n

\textbf{if}$a_i\geq$max\textbf{then}

next:=max

max:=a1

\textbf{else if}nextai <max\textbf{then}

next:=a1

\ \

\color{#4257b2} Finally, we return the position of the two largest

numbers.\color|{default}

\ \

\textbf{return } last, next}$

02

Step 2

Combining these steps, we then obtain the algorithm: Procedure last largest

(a1,a2,an:integerswithn3)

If a1<a2then

max=a2

next:=a1

else

max:=a1

next:=a2

for i:=3 to n

if aimaxthen

next:=max

max:=ai

else ifnextai<max then

next:=ai

returnlast,next

03

Step 3

(b) We note that the algorithm initially makes 1 comparison to check a1<a2

Next, the algorithm makes at most two comparison ( aimaxand nextai<max)

In every iteration of the for –loop. Since i ranges from 3ton, there are

n-3+1=n-2 iteration of the for –loop and thus there are 2(n-1)comparison in

for –loop.

We then note that there are 2(n-1)+1=2n-2+1=2n-1comparison in total, while

2n-1isO(n).

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

a) Describe an algorithm for locating the last occurrence of the largest number in a list of integers.

b) Estimate the number of comparisons used.

Show that if f(x)=anxn+an1xn1++a1x+a0,, where a0,a1,.....,an-1,and anare real numbers and an an0, then data-custom-editor="chemistry" f(x)is Θ(xn). Big-O, big-Theta, and big-Omega notation can be extended to functions in more than one variable. For example, the statement fx,yis O(g(x,y))means that there exist constants C, k1, and k2such that |f(x,y)|C|g(x,y)|wheneverx>k1 and x>k2.

a) Describe in detail (and in English) the steps of an algorithm that finds the maximum and minimum of a sequence of elements by examining pairs of successive elements, keeping track of a temporary maximum and a temporary minimum. Ifn is odd, both the temporary maximum and temporary minimum should initially equal the first term, and ifn is even, the temporary minimum and temporary maximum should be found by comparing the initial two elements. The temporary maximum and temporary minimum should be updated by comparing them with the maximum and minimum of the pair of elements being examined.

b) Express the algorithm described in part (a) in pseudocode.

c) How many comparisons of elements of the sequence are carried out by this algorithm? (Do not count comparisons used to determine whether the end of the sequence has been reached.) How does this compare to the number of comparisons used by the algorithm in Exercise 5?

When a list of elements is in close to the correct order, would it be better to use an insertion sort or its variation described in Exercise 50?

Express the relationship fxis Ω(g(x)) using a picture. Show the graphs of the functions f (x) and Cg(x), as well as the constant k on the real axis.

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