The document discusses insertion sort, describing it as the simplest sorting technique. It has a linear time complexity of O(n) for nearly sorted or small data sets, but a quadratic time complexity of O(n^2) for large or reverse sorted data. Insertion sort is an in-place, stable sorting algorithm that requires only constant O(1) auxiliary space. While having a higher time complexity than other sorts for large data sets, insertion sort remains useful for sorting small data sets or as the base case in more complex sorting algorithms due to its simplicity and in-place implementation.
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Intro to Sorting + Insertion Sort
1. What do sorts do?
● Take this: [6, 24, 10, 76, 35, 0, 37]
– Make it into this: [0, 6, 10, 24, 35, 37, 76]
● Or this: [“dolphin”, “yak”, “caribou”]
– Into: [“caribou”, “dolphin”, “yak”]
● How do you sort?
2. Which sort should you use?
● Factors
– No sort is good at everything, there will always be
tradeoffs depending on the qualities of your data
and your machine's circumstances
● http://www.sorting-algorithms.com
– Helpful overview of the different sorts, their
runtimes, properties, and best applications for each
of the major sorting methods
3. Which sort should you use?
● Memory usage
– Some sorts are “in place” and do not require much
more memory to run
– Other sorts use space liberally in recursion to make an
easier workload for the computer
– Pick two of the three:
● Fast runtime
● Low memory overhead
● Ease of coding/implementation
4. Which sort should you use?
● Stability
– Unstable sorts may change the relative order of keys of
the same value
– Stable sorts do not change the relative order of keys of
the same value
– So...?
● Consider this scenario
– You sort a list of flights by departure time
– You then sort that list by destination
– If the second sort was stable, you would result with an array of flights
sorted by destination ordered by increasing departure times
5. Which sort should you use?
● Adaptive or not?
– Efficiency changes depending on presortedness
(yes that's a word now) of the input array
– Types of presortedness to consider
● Pure random
● Nearly sorted
● Reverse sorted
● Few unique keys
– Many of the same values
6. The Unicorn Sort
● O(1) worst case runtime
● In place
● Stable
● Not adaptive
● Doesn't exist
8. The littlest sort that could... kinda
● Simplest sort technique
– No tricky recursion
– Only one operation to consider
– When mentally sorting, humans
naturally tend to use some sort of
insertion-like method
9. The littlest sort that could... kinda
● Adaptive
– Efficient for small data sets
– Data sets that are already close to
being sorted create the best case
scenario and quickest runtime
– Reverse order, on the other hand, is
bad news bears
10. The littlest sort that could... kinda
● Stable
– Does not change the relative order of
keys
● In-Place
– No extra memory usage from
recursion
– “Low overhead”
11. The littlest sort that could... kinda
● Summary!
– Simple to implement
– Efficient at small data sets
● Often used as the recursive base
case for other sorts
– Stable
– No (well, barely any) extra memory
usage
12. The littlest sort that could... kinda
● Runtime!
– Best case ~> already sorted array
● O(n) comparisons, O(1) swaps
– Worst case ~> reverse sorted array
● O(n**2) comparisons, O(n**2) swaps
● Literally swapping every element in the array
– Average ~> O(n**2) comparisons and swaps
● BAD for large data sets
● Space!
– O(n) total, O(1) for auxiliary operations
13. Show me the stats
● require 'benchmark'
– puts Benchmark.measure { block }
● Insertion sort, are you good at sorting?
– 10,000 elements, 1..10,000 already sorted
● 0.029761 seconds
– 10,000 elements, 1..10,000 reverse sorted
● 2.324039 seconds
– 10,000 elements, random values up to 10,000
● 1.259223 seconds
– 20,000 elements, random values up to 20,000
● 4.691325 seconds
– 30,000 elements, random values up to 30,000
● 10.571314 seconds
15. Bro, do you even logic?
● For each n in a range of the numbers 1..(array.size-1)
– Pull position n out of the array destructively
● Create value_to_insert variable ~> (array[n])
● Create insertion_index variable ~> (n)
– While the value of n > 0 and the value_to_insert is smaller than
the value at the position before insertion_index in the array
● Looking at the value before array[n]...
– Is it bigger than array[n]?
● insertion_index should be decremented
● “while” code runs again
– Is it smaller than array[n]?
● The “while” loop is broken
– value_to_insert should be inserted at insertion_index
● Each loop finished; return the array
16. Need a copy of this?
● http://www.slideshare.net/nicholascase520/intro
-to-sorting-insertion-sort