The question is: "In the worst case for a list with 10 distinct elements, how many comparisons are made?"

Why isn't the comparison with the sorted number and the blank left space counted as an individual comparison? I thought that was considered a possible comparison scenario in insertion sorts.

For example, sorting 3, 2, 1:

- 1st round: 2 < 3, (blank) 2 = 2 comparisons
- 2nd round: 1 < 3, 1 < 2, (blank) 1 = 3 comparisons

For a total of 5 comparisons.

Right?

No vote yet

1 vote

×

Problem Loading...

Note Loading...

Set Loading...

Easy Math Editor

`*italics*`

or`_italics_`

italics`**bold**`

or`__bold__`

boldNote: you must add a full line of space before and after lists for them to show up correctlyparagraph 1

paragraph 2

`[example link](https://brilliant.org)`

`> This is a quote`

Remember to wrap math in \( ... \) or \[ ... \] to ensure proper formatting.`2 \times 3`

`2^{34}`

`a_{i-1}`

`\frac{2}{3}`

`\sqrt{2}`

`\sum_{i=1}^3`

`\sin \theta`

`\boxed{123}`

## Comments

Sort by:

TopNewestCan you explain what is this blank comparison that you are referring to?

Log in to reply

Sure! So when Insertion Sort was introduced, this was what I was told:

Insertion Sort method will place a single element

xinto a sorted arrayA. First,xis placed at the end ofA. Second,xis compared to the element on the left, there are 3 possible scenarios:There is no element to the left, so the process is finished because

xis the smallest element and is already at the start of the arrayxis greater than or equal to the left element, so the process is finishedxis less than the left element = positions are switchedThirdly, the cycle repeats until

xmeets scenario 1 or 2.The blank comparison I'm referring to is scenario 1, when there is no left element.

Log in to reply

You are indeed correct. Insertion sort involves repeating the procedure above for every element of the input array.

Now, it is possible that you might get a slightly different number of comparisons based on what you define to be comparisons. However, we are interested in getting a rough approximation of how this number depends on the size of input. So, usually just noticing that the number of comparisons is \(O(n^2)\) is good enough.

If that answer confuses you further, let me know.

Log in to reply

The only way I can justify this is that blank comparisons are not considered individual comparisons because they do not cause the cycle to repeat. In mind, the computer would perform the blank comparison and just be like "oh, nothing to compare, I'm all good" and thus this minor comparison was just to check for another number. So since, there were no numbers to compare, it is not a full comparison.

Does this make sense? I'm trying to justify the \[\frac{(n-1)(n)}{2}\] formula. (Also, if anyone can link the theory to that formula, that would be great!)

Log in to reply

The formula is sum of first \(n-1\) natural numbers. It is also the number of ways to choose 2 things from a set of \(n\) things, see combinations

Log in to reply

Thanks!

Log in to reply