KEMBAR78
Intro to Sorting + Insertion Sort | ODP
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?
● 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
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
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
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
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 technique
– No 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
● 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
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 sets
● Often 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 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
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
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 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
Need a copy of this?
● http://www.slideshare.net/nicholascase520/intro
-to-sorting-insertion-sort

Intro to Sorting + Insertion Sort

  • 1.
    What do sortsdo? ● 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 shouldyou 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 shouldyou 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 shouldyou 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 shouldyou 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
  • 7.
  • 8.
    The littlest sortthat 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 sortthat 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 sortthat could... kinda ● Stable – Does not change the relative order of keys ● In-Place – No extra memory usage from recursion – “Low overhead”
  • 11.
    The littlest sortthat 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 sortthat 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 thestats ● 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
  • 14.
    OH BOY! TIMETO IMPLEMENT
  • 15.
    Bro, do youeven 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 copyof this? ● http://www.slideshare.net/nicholascase520/intro -to-sorting-insertion-sort