PageRenderTime 5ms CodeModel.GetById 1ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 4runtime = (N) ->
 5  [sum, t] = [0, 0]
 6  for n in [1..N]
 7    sum += 2 * t
 8    t = n - 1 + sum / n
 9  t
10
11console.log runtime(3) is 2.6666666666666665
12console.log runtime(5) is 7.4
13console.log runtime(8) is 16.92142857142857