Which sort do I use again?
● Memory requirements?
● Data set size?
– Preorderedness?
● Matters if algorithm is adaptive or not
● Do you care if the sort is stable or not?
● When in doubt, look at and trust in Ruby's
(Rubinius') .sort method
– What does .sort do?
Merge Sort
Hail to the king, baby
● Most predictable sort
– Not adaptive
● Doesn't care what size the data set
is or how preordered it is
– Average case == Best case == Worst
case
Hail to the king, baby
● Rubinius' standard sort algorithm
– Insertion sort is used as a sub-
method when the recursion splits it
enough
● (arrays of size 7 or smaller)
Hail to the king, baby
● Only stable algorithm of comparable
average runtime
– Does not change the relative order of
keys
– Quicksort has comparable runtime in
most cases but is an unstable sort
– Since Ruby uses merge sort with
insertion sort natively, .sort will always
be a stable sort
OMG IT HAS A DOWNSIDE
● Extra memory required
– O(n) extra space for auxiliary
operations
● Recursively split into components
for resorting
– In-place merge sorts do exist, but are
time-consuming and complex to
implement
Show me the stats
● 10,000 elements, 1..10,000 already sorted
– 0.029761 seconds ~> Insertion sort
– 0.018062 seconds ~> Merge sort
● 10,000 elements, 1..10,000 reverse sorted
– 2.324039 seconds ~> Insertion sort
– 0.019173 seconds ~> Merge sort
● 10,000 elements, random values up to 10,000
– 1.259223 seconds ~> Insertion sort
– 0.028239 seconds ~> Merge sort
● 20,000 elements, random values up to 20,000
– 4.691325 seconds ~> Insertion sort
– 0.060080 seconds ~> Merge sort
● 30,000 elements, random values up to 30,000
– 10.571314 seconds ~> Insertion sort
– 0.086877 seconds ~> Merge sort
OH BOY! TIME TO IMPLEMENT
Bro, do you even logic?
● First method: “Mergesort” (one param: array)
– Return the array if it is only one element, else...
– Split the array into two halves, left and right recursively
– Return results of secondary “merge” method on those recursive splits
● Secondary method: “Merge” (two params: left array, right array)
– Create a new array for return later
– Until left or right array is empty
●
If the first element of the left array is less than the first element of the right array ~> shift the
left array's first value into the new array for return
● Else, shift the right array's first value into the new array for return
– Return the result of the return array, left, and right added together (since there
will always be at least one straggler in either the left or right array)
Want this slide deck?
● http://www.slideshare.net/nicholascase520/mer
ge-sort-35704317
● Also on the Canvas discussions board

Merge sort

  • 1.
    Which sort doI use again? ● Memory requirements? ● Data set size? – Preorderedness? ● Matters if algorithm is adaptive or not ● Do you care if the sort is stable or not? ● When in doubt, look at and trust in Ruby's (Rubinius') .sort method – What does .sort do?
  • 2.
  • 3.
    Hail to theking, baby ● Most predictable sort – Not adaptive ● Doesn't care what size the data set is or how preordered it is – Average case == Best case == Worst case
  • 4.
    Hail to theking, baby ● Rubinius' standard sort algorithm – Insertion sort is used as a sub- method when the recursion splits it enough ● (arrays of size 7 or smaller)
  • 5.
    Hail to theking, baby ● Only stable algorithm of comparable average runtime – Does not change the relative order of keys – Quicksort has comparable runtime in most cases but is an unstable sort – Since Ruby uses merge sort with insertion sort natively, .sort will always be a stable sort
  • 6.
    OMG IT HASA DOWNSIDE ● Extra memory required – O(n) extra space for auxiliary operations ● Recursively split into components for resorting – In-place merge sorts do exist, but are time-consuming and complex to implement
  • 7.
    Show me thestats ● 10,000 elements, 1..10,000 already sorted – 0.029761 seconds ~> Insertion sort – 0.018062 seconds ~> Merge sort ● 10,000 elements, 1..10,000 reverse sorted – 2.324039 seconds ~> Insertion sort – 0.019173 seconds ~> Merge sort ● 10,000 elements, random values up to 10,000 – 1.259223 seconds ~> Insertion sort – 0.028239 seconds ~> Merge sort ● 20,000 elements, random values up to 20,000 – 4.691325 seconds ~> Insertion sort – 0.060080 seconds ~> Merge sort ● 30,000 elements, random values up to 30,000 – 10.571314 seconds ~> Insertion sort – 0.086877 seconds ~> Merge sort
  • 8.
    OH BOY! TIMETO IMPLEMENT
  • 9.
    Bro, do youeven logic? ● First method: “Mergesort” (one param: array) – Return the array if it is only one element, else... – Split the array into two halves, left and right recursively – Return results of secondary “merge” method on those recursive splits ● Secondary method: “Merge” (two params: left array, right array) – Create a new array for return later – Until left or right array is empty ● If the first element of the left array is less than the first element of the right array ~> shift the left array's first value into the new array for return ● Else, shift the right array's first value into the new array for return – Return the result of the return array, left, and right added together (since there will always be at least one straggler in either the left or right array)
  • 10.
    Want this slidedeck? ● http://www.slideshare.net/nicholascase520/mer ge-sort-35704317 ● Also on the Canvas discussions board