Skip to content

Instantly share code, notes, and snippets.

@Rosuav
Created August 13, 2016 03:38
Show Gist options
  • Save Rosuav/69a2b5b1fe45feca2b6a9001d7220d1c to your computer and use it in GitHub Desktop.
Save Rosuav/69a2b5b1fe45feca2b6a9001d7220d1c to your computer and use it in GitHub Desktop.
findNthElement halves the size of the array at each step, so it's a classic
example of an O(log n) algorithm.
findElements calls findNthElement, so its complexity must be at best O(log n);
we can generally assume that toFind will be smaller than array (you won't be
asking for duplicate elements with this), so it gets dropped from the Big O,
and we describe findElements as O(log n).
isOdd does one floating-point division and one comparison, no matter what
number is provided. It's O(1), constant time.
triangleNumbers is about as ridiculous as the dumb Fibonacci algorithm that
floats around in most examples of recursion. It's O(stupid) and you don't
really need anything more than that. Tip: Don't call this with any number
greater than about 20 unless you're prepared to wait for a while.
sampleAutocorrelationMatrix goes through the array twice, in nested loops,
so it takes time and memory according to the size of the result matrix -
that is, O(n^2) in the size of the original vector.
doubleArray steps through the array linearly, in linear time. O(n).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment