Advertisement
Guest User

Untitled

a guest
Dec 19th, 2013
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.61 KB | None | 0 0
  1. N = 10000000
  2.  
  3. def pushDown(h, pos, n)
  4.   while 2 * pos + 1 < n
  5.     j = 2 * pos + 1
  6.     if j + 1 < n && h[j + 1] > h[j]
  7.       j += 1
  8.     end
  9.     if h[pos] >= h[j]
  10.       break
  11.     end
  12.     h[pos], h[j] = h[j], h[pos]
  13.     pos = j
  14.   end
  15. end
  16.  
  17. def main
  18.   start = Time.now
  19.  
  20.   h = Array.new(N) { |i| i }
  21.  
  22.   (N / 2).downto(0) do |i|
  23.     pushDown(h, i, N)
  24.   end
  25.  
  26.   n = N
  27.   while n > 1
  28.     h[0], h[n - 1] = h[n - 1], h[0]
  29.     n -= 1
  30.     pushDown(h, 0, n)
  31.   end
  32.  
  33.   N.times do |i|
  34.     if h[i] != i
  35.       raise 'h[i] != i'
  36.     end
  37.   end
  38.  
  39.   print("Done in #{((Time.now - start) * 1000).to_i}\n")
  40. end
  41.  
  42. main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement