small medium large xlarge

03 Jul 2015, 11:33
Lefteris K. (1 post)

Hello all.

I’d like to make a comment regarding the algorithm in pages 225-227.

The given algorithm works if the second minimum is AFTER the first minimum. If the second minimum is BEFORE the first minimum then the algorithm is wrong because the index of the second minimum won’t change.

I believe there should be a check for this in the algorithm.



24 Sep 2015, 13:57
Jennifer Campbell (17 posts)

Hi Lefteris,

Is this an example of the case that you are referring to?

find_two_smallest([5, 4, 3, 5, 2, 6])

In that list, the smallest value is at index 4 and the second smallest is earlier in the list at index 2.

I believe the issue that you are referring to has to do with how the indices change once the smallest item has been removed. We account for that in the code on page 227 under the comment # Fix min2 in case it was affected by the reinsertion.

If I’m misunderstanding your question, please feel free to ask another and provide an example.

Jen Campbell

You must be logged in to comment