/examples/beautiful_code/quicksort_runtime.coffee

http://github.com/jashkenas/coffee-script · CoffeeScript · 13 lines · 9 code · 2 blank · 2 comment · 1 complexity · c5134dd26f5296bbfd80f9a7aa6fbacb MD5 · raw file

  1. # Beautiful Code, Chapter 3.
  2. # Produces the expected runtime of Quicksort, for every integer from 1 to N.
  3. runtime = (N) ->
  4. [sum, t] = [0, 0]
  5. for n in [1..N]
  6. sum += 2 * t
  7. t = n - 1 + sum / n
  8. t
  9. console.log runtime(3) is 2.6666666666666665
  10. console.log runtime(5) is 7.4
  11. console.log runtime(8) is 16.92142857142857