Skip to content

Commit

Permalink
Merge branch 'develop' into hc-default
Browse files Browse the repository at this point in the history
  • Loading branch information
courtneycb committed Nov 19, 2019
2 parents 9b3cfbe + e62af4d commit 2a87dbc
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions csfieldguide/chapters/content/en/algorithms/sections/sorting.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,27 @@ What about 20? If you knew how many comparisons it would take to sort 1000 boxes
# Answer for box analysis

For a list of 8 objects (like in the interactive) it should take 7 comparisons to find the lightest, 6 to find the next lightest, 5 to find the next, then 4, then 3, then 2, and then 1 to sort the final two boxes.
In total this is $ 7+6+5+4+3+2+1 = 28 $ comparisons.
If there had been 9 boxes it would have taken $ 8+7+6+5+4+3+2+1 = 36 $ comparisons.
In total this is \( 7+6+5+4+3+2+1 = 28 \) comparisons.
If there had been 9 boxes it would have taken \( 8+7+6+5+4+3+2+1 = 36 \) comparisons.
20 boxes will take 190. Going from 1000 boxes up to 1001 will require 1000 extra comparisons, even though only 1 box has been added.
Selection sort will always take $ (n\times(n-1))/2 $ comparisons to sort *n* items into order.
Selection sort will always take \( \frac{n(n-1)}{2} \) comparisons to sort *n* items into order.

For example: To calculate the number of comparisons required for 20 boxes, using $ (n\times(n-1))/2 $ where *n* = 20:
For example: To calculate the number of comparisons required for 20 boxes, using \( \frac{n(n-1)}{2} \) where *n* = 20:

$$
\( \frac{20(20-1)}{2} \)

(20*(20-1))/2
\( = \frac{(20 \times 19)}{2} \)

= (20*19)/2
\( = \frac{380}{2} \)

= 380/2
\( = 190 \) comparisons

= 190 comparisons
$$

Some students may recognise this formula as Gauss' trick (see [the anecdotes about Gauss on Wikipedia](https://en.wikipedia.org/wiki/Carl_Friedrich_Gauss#Anecdotes).
One way of expressing this trick for the above example is that 20 boxes would require summing the numbers $ 1+2+3+...+17+18+19 $ . If we write the numbers backwards ( $ 19+18+17+...3+2+1 $ ) then it would be the same sum. Now if we add these two lists together, pairing up the corresponding numbers, we get $ (1+19)+(2+18)+(3+17)+...+(17+3)+(18+2)+(19+1) $ .
Each pair in this sum adds up to 20, and there are 19 pairs, so adding the two lists together is just 20x19. Since both lists add up to the same amount, the original sum is a half of that, or $ 20x19/2 $ , which is 190 comparisons, which is what we got from the formula above.
Some students may recognise this formula as Gauss' trick (see [the anecdotes about Gauss on Wikipedia](https://en.wikipedia.org/wiki/Carl_Friedrich_Gauss#Anecdotes)).
One way of expressing this trick for the above example is that 20 boxes would require summing the numbers \( 1+2+3+...+17+18+19 \).
If we write the numbers backwards ( \( 19+18+17+...3+2+1 \) ) then it would be the same sum.
Now if we add these two lists together, pairing up the corresponding numbers, we get \( (1+19)+(2+18)+(3+17)+...+(17+3)+(18+2)+(19+1) \).
Each pair in this sum adds up to 20, and there are 19 pairs, so adding the two lists together is just \( 20 \times 19 \).
Since both lists add up to the same amount, the original sum is a half of that, or \( \frac{(20 \times 19)}{2} \), which is 190 comparisons, which is what we got from the formula above.
If students can follow this reasoning then they can easily work out the comparisons needed for a large number of boxes, and the don't have to use the "magic" formula given above.
There's a visual explanation in [this video](http://www.numberphile.com/videos/one_to_million.html) and more examples on [this page](http://nzmaths.co.nz/gauss-trick-staff-seminar).

Expand Down

0 comments on commit 2a87dbc

Please sign in to comment.