intro to sorting + insertion sort
TRANSCRIPT
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?
Which sort should you use?
FactorsNo 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.comHelpful overview of the different sorts, their runtimes, properties, and best applications for each of the major sorting methods
Which sort should you use?
Memory usageSome 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
Which sort should you use?
StabilityUnstable 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 scenarioYou 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
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 considerPure random
Nearly sorted
Reverse sorted
Few unique keysMany of the same values
The Unicorn Sort
O(1) worst case runtime
In place
Stable
Not adaptive
Doesn't exist
Insertion Sort
The littlest sort that could... kinda
Simplest sort techniqueNo tricky recursion
Only one operation to consider
When mentally sorting, humans naturally tend to use some sort of insertion-like method
The littlest sort that could... kinda
AdaptiveEfficient 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
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
The littlest sort that could... kinda
Summary!Simple to implement
Efficient at small data setsOften used as the recursive base case for other sorts
Stable
No (well, barely any) extra memory usage
The littlest sort that could... kinda
Runtime!Best case ~> already sorted arrayO(n) comparisons, O(1) swaps
Worst case ~> reverse sorted arrayO(n**2) comparisons, O(n**2) swaps
Literally swapping every element in the array
Average ~> O(n**2) comparisons and swapsBAD for large data sets
Space!O(n) total, O(1) for auxiliary operations
Show me the stats
require 'benchmark'puts Benchmark.measure { block }
Insertion sort, are you good at sorting?10,000 elements, 1..10,000 already sorted0.029761 seconds
10,000 elements, 1..10,000 reverse sorted2.324039 seconds
10,000 elements, random values up to 10,0001.259223 seconds
20,000 elements, random values up to 20,0004.691325 seconds
30,000 elements, random values up to 30,00010.571314 seconds
OH BOY! TIME TO IMPLEMENT
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 destructivelyCreate 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 arrayLooking 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
Need a copy of this?
http://www.slideshare.net/nicholascase520/intro-to-sorting-insertion-sort