Invitation to Computer Science 8th Edition

Published by Cengage Learning
ISBN 10: 1337561916
ISBN 13: 978-1-33756-191-4

Chapter 3 - Exercises - Page 140: 5b

Answer

For the positions of the two middle elements in a list with an even number $n$ of elements: $\left[\frac{n}{2} \text { and } \frac{n}{2}+1\right]$ and their average $\left[\frac{n+1}{2}\right]$

Work Step by Step

We do the same with lists that consist of an even number of elements $n .$ We know that even numbers are divisible by $2 .$ So there isn't just one distinct middle element, but two. The positions of the two middle elements are the required number of comparisons to find them. The average of these two numbers is their sum divided by 2. Example $\quad\bullet$ For $n=16$ the positions of the two middle elements are 8 and $9 .$ $\quad$$\quad$ Average $=8.5$ $\quad$For $n=40$ the positions of the two middle elements are 20 and $21 .$ $\quad$$\quad$Average $=20.5$ $\quad$$\bullet$ For $n=56$ the positions of the two middle elements are 28 and $29 .$ $\quad$$\quad$Average $=28.5$ The values for the two middle elements can be found using the expressions: $\frac{n}{2}$ and $\frac{n}{2}+1$ Let's find an expression for the average of the two values for the middle elements: $\frac{\frac{n}{2}+\frac{n}{2}+1}{2}=\frac{\frac{2 \cdot n}{2}+1}{2}=\frac{n+1}{2}$
Update this answer!

You can help us out by revising, improving and updating this answer.

Update this answer

After you claim an answer you’ll have 24 hours to send in a draft. An editor will review the submission and either publish your submission or provide feedback.