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:
For a total of 5 comparisons.
Right?
Easy Math Editor
This discussion board is a place to discuss our Daily Challenges and the math and science related to those challenges. Explanations are more than just a solution — they should explain the steps and thinking strategies that you used to obtain the solution. Comments should further the discussion of math and science.
When posting on Brilliant:
*italics*
or_italics_
**bold**
or__bold__
paragraph 1
paragraph 2
[example link](https://brilliant.org)
> This is a quote
\(
...\)
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
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 2(n−1)(n) 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!
Can 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 x into a sorted array A. First, x is placed at the end of A. Second, x is 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 x is the smallest element and is already at the start of the array
x is greater than or equal to the left element, so the process is finished
x is less than the left element = positions are switched
Thirdly, the cycle repeats until x meets 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(n2) is good enough.
If that answer confuses you further, let me know.